HyPhy message board | |
http://www.hyphy.org/cgi-bin/hyphy_forums/YaBB.pl
Methodology Questions >> How to >> Constraints http://www.hyphy.org/cgi-bin/hyphy_forums/YaBB.pl?num=1260289318 Message started by Miguel Lacerda on Dec 8th, 2009 at 8:21am |
Title: Constraints Post by Miguel Lacerda on Dec 8th, 2009 at 8:21am
Hey Sergei,
Is it possible to constrain one variable subject to the value of another variable to be optimised in HyPhy? For example, suppose I wish to maximise the function f(x,y) = exp{-(x-2)^2-(y-3)^2} w.r.t (x, y) s.t. x>y. In the code below, HyPhy uses the initial value of a, rather than the optmised value in the constraint b :< a and hence gives x = 2, y=3. [code] function userFunction (x,y) { return Exp(-(x-2)^2-(y-3)^2); } a = 5; b = 5; b :< a; Optimize (res, userFunction (a,b)); fprintf (stdout, " ", res, " "); [/code] Is there a way around this? Thanks, Miguel |
Title: Re: Constraints Post by Sergei on Dec 8th, 2009 at 10:49am
Hey Miguel,
There is no direct way to enforce Code (] x:<y; [/code):
Sergei |
Title: Re: Constraints Post by Miguel Lacerda on Dec 8th, 2009 at 1:42pm
Thanks for this!
My actual problem is slightly more complicated though, and I am not sure how to resolve it using your trick above. I have two variables to be optimised, say x and y. The variable x is constrained to [0,1], while both the left and right limits of y depend on x; y is defined on [x, min(x/f,1)] for some fraction f. So... y=a*x for a>1 and y=b*min(x/f,1) for b<1 Hence a=b*min(x/f,1)/x > 1 while b < 1 I thought about optimising b :< 1 and setting a := Max(b*Min(x/f,1)/x, 1) but I'm sure this isn't correct since I am not actually optimising b such that a is appropriately constrained too (the Max command is just a fix for when the optimised value of b fails to satisfy the constraint on a). [code] global x = 0.5; x :> 0; x :< 1; global b = 0.5; b:<1; global y := Max((b/x)*Min(x/f__,1),1)*x; [/code] Any suggestions? Thanks in advance! Miguel |
Title: Re: Constraints Post by Sergei on Dec 8th, 2009 at 3:59pm
Hi Miguel,
If you are doing the optimization on a user function, then you can check for parameter bounds inside and return a really small (e.g. -1e100) value if bound checks fail. You can also do something along the lines of (not ideal, but should get the job done). [code] y:=map(y2,x,f__); function map (y_aux,x,frac) { if (y_aux >= x) { if (y_aux <= Min(x/frac,1)) { return y_aux; } return Min(x/frac,1); } return x; } [/code] Sergei |
Title: Re: Constraints Post by Miguel Lacerda on Dec 9th, 2009 at 12:00pm
Thanks! The constraints are enforced, although I'm actually optimising the built-in LikelihoodFunction(), rather than a user function - hope the optimisation routine still behaves!
Thanks again for the help! Miguel |
HyPhy message board » Powered by YaBB 2.5.2! YaBB Forum Software © 2000-2024. All Rights Reserved. |