Welcome, Guest. Please Login
YaBB - Yet another Bulletin Board
 
  HomeHelpSearchLogin  
 
Constraints (Read 2571 times)
Miguel Lacerda
YaBB Newbies
*
Offline


Feed your monkey!

Posts: 36
Natl Univ of Ireland, Galway
Gender: male
Constraints
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, " ");
 



Is there a way around this?

Thanks,
Miguel
Back to top
 
 
IP Logged
 
Sergei
YaBB Administrator
*****
Offline


Datamonkeys are forever...

Posts: 1658
UCSD
Gender: male
Re: Constraints
Reply #1 - Dec 8th, 2009 at 10:49am
 
Hey Miguel,

There is no direct way to enforce

Code:
x:<y;
 



The hack to do this is to introduce a third variable, as done in the code I pasted below:

Code:
function userFunction (x,y)
{
return Exp(-(x-2)^2-(y-3)^2);
}
a = 5;
b = 1;
c := b*a;
b :< 1;
Optimize (res, userFunction (a,c));
fprintf (stdout, " ", res, " ");
 



Sergei
Back to top
 

Associate Professor
Division of Infectious Diseases
Division of Biomedical Informatics
School of Medicine
University of California San Diego
WWW WWW  
IP Logged
 
Miguel Lacerda
YaBB Newbies
*
Offline


Feed your monkey!

Posts: 36
Natl Univ of Ireland, Galway
Gender: male
Re: Constraints
Reply #2 - 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;
 



Any suggestions?

Thanks in advance!
Miguel
Back to top
 
 
IP Logged
 
Sergei
YaBB Administrator
*****
Offline


Datamonkeys are forever...

Posts: 1658
UCSD
Gender: male
Re: Constraints
Reply #3 - 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;
}

 



Sergei
Back to top
 

Associate Professor
Division of Infectious Diseases
Division of Biomedical Informatics
School of Medicine
University of California San Diego
WWW WWW  
IP Logged
 
Miguel Lacerda
YaBB Newbies
*
Offline


Feed your monkey!

Posts: 36
Natl Univ of Ireland, Galway
Gender: male
Re: Constraints
Reply #4 - 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
Back to top
 
 
IP Logged