help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] Calculate sqrt of var


From: Nigel Galloway
Subject: Re: [Help-glpk] Calculate sqrt of var
Date: Tue, 22 Apr 2014 03:55:35 -0700

Andrew,

The following model estimates the sqrt of a variable. If you think it
will 
enhance glpk's examples, then please add it.

/*Estimate the square root of a variable

  address@hidden
  April 18th., 2014.
*/

param V  :=  2; /*variable <= 2**(V-1)**2*/
param T  :=  10; /*Tolerance of result 2**V/2**T*/
param Dx :=  2*(V+T-1);
param Dy :=  V+T-1;

var value;
var result;
var mySQRT;
var gn {x in 0 .. Dx, y in 0 .. Dy, z in 1 .. 2}, binary;

i1 : value = 2; /*set variable value*/

gn1 {x in 0 .. Dx, y in 0 .. Dy : x > Dy+y or x-y < 0} : gn[x,y,1] = 0;
gn2 {x in 0 .. Dx, y in 0 .. Dy : x <= Dy+y and x-y >= 0} :
    sum {z in 1..2} gn[x,y,z] = gn[x-y,0,1] + gn[y,0,1];
gn3 {x in 0 .. Dx, y in 0 .. Dy : x <= Dy+y and x-y >= 0} : gn[x,y,1] <=
gn[x,y,2];

r2 : result = sum{x in 0 .. Dx} sum{y in 0 .. Dy} gn[x,y,1]*2**(x-2*T);
r3 : result - ceil(2**V)/2**(T) <= value;
r4 : result + ceil(2**V)/2**(T) >= value;
r5 : mySQRT = sum{y in 0 .. Dy} gn[y,0,1]*2**(y-T);

solve;

printf "Approximate Square Root of %f is %f (%f)\n",value, mySQRT,
result;

end;

As configured it calculates sqrt(2):
Approximate Square Root of 2.000000 is 1.413086 (1.996812)

Changing value to 973.5, V to 5, and T to 4 results in:
Approximate Square Root of 973.500000 is 31.187500 (972.660156)


-- 
  Nigel Galloway
  address@hidden

On Tue, Apr 8, 2014, at 05:11 PM, Meketon, Marc wrote:
> My guess is that fee[m] is a const/param, so can you rewrite your
> constraint to be the following?
> 
> fee[m]*fee[m] = 1000 + (100 + (sum {n in N} (n mod 47))) * (sum {l > in
> L} prod[l,m]);
> 
> -----Original Message-----
> From: address@hidden
> [mailto:address@hidden On Behalf
> Of Andrew Makhorin
> Sent: Tuesday, April 08, 2014 3:34 PM
> To: Tiago Costa
> Cc: address@hidden
> Subject: Re: [Help-glpk] Calculate sqrt of var
> 
> On Tue, 2014-04-08 at 18:46 +0100, Tiago Costa wrote:
> > Hi,
> >
> > I'm trying to apply the following restriction:
> >
> > subject to calc2 {m in M} :
> >      fee[m] = sqrt(1000 + (100 + (sum {n in N} (n mod 47))) * (sum {l
> > in L} prod[l,m]));
> >
> > However since prod[l,m] is a var, I get "argument for sqrt has invalid
> > type".
> >
> > I've read in the documentation that you can only calculate the sqrt of
> > constants/params/etc so is there a workaround to calculate the sqrt of
> > a var?
> >
> > Thanks
> > --
> > Tiago Costa
> >
> 
> Only linear constraints are allowed while sqrt makes the constraint
> non-linear. However, using sqrt *below* the solve statement, i.e. when
> all the variables have been computed, is allowed.
> 
> 
> 
> _______________________________________________
> Help-glpk mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-glpk
> 
> This e-mail and any attachments may be confidential or legally
> privileged. If you received this message in error or are not the intended
> recipient, you should destroy the e-mail message and any attachments or
> copies, and you are prohibited from retaining, distributing, disclosing
> or using any information contained herein.  Please inform us of the
> erroneous delivery by return e-mail. Thank you for your cooperation.
> 
> _______________________________________________
> Help-glpk mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-glpk

-- 
http://www.fastmail.fm - The professional email service




reply via email to

[Prev in Thread] Current Thread [Next in Thread]