[Top][All Lists]

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

Re: [Help-glpk] power of: calculate 2^x from variable x

From: Michael Hennebry
Subject: Re: [Help-glpk] power of: calculate 2^x from variable x
Date: Wed, 8 Jun 2011 09:28:42 -0500 (CDT)
User-agent: Alpine 1.00 (DEB 882 2007-12-20)

On Tue, 7 Jun 2011, malekro wrote:

i am trying to calculate 2^x from a variable x. i already spent several
hours and with the help of archived posts i thought i have the solution.
unfortunately i cannot figure out why the code below returns an empty result
or malforms the x variable, i think the idea is correct.

set R := {0..3};
set C := {0..2};

# the given numbers that need to be powered by 2
var x{R}, integer;

# temporary table, each row will later be used for multiplication with 2^c
var t{R,C}, binary;

# the desired solution 2^x
var s{R}, integer;

# set the x-th cell in temporary row to 1, rest 0
s.t. c1{r in R}: sum{c in C} t[r,c] = 1; # exactly one 1 in row
s.t. c3{r in R, c in C}: c * t[r,c] = x[r]; # set the x[r]-th cell to 1

|R|*|C| constraints with two terms each.
Probably not what you want.
Drop them and the rest should be sufficient.

s.t. c2{r in R}: sum{c in C} c * t[r,c] = x[r]; # verification so t[r,0] !=
1 if x[r] > 0

# multiply the c-th cell of the temporary row with 2^c
s.t. c4{r in R}: s[r] = sum{c in C} (2 ** c) * t[r,c];

Michael   address@hidden
"Pessimist: The glass is half empty.
Optimist:   The glass is half full.
Engineer:   The glass is twice as big as it needs to be."

reply via email to

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