adfdsfsdf
adfdsfsdf

Reputation: 55

set equation for binary variable in GEKKO optimization problem by m.if3

I am trying to have a array with size 52*6 as my changing variable. Also, I would like to define x[:,0] as [0,1] binary variable. I would like to have a constraint such that for any x[i,0] =1, a = 5; otherwise a = 0. The following is part of my code, it turns into a meaningless trivial solution. I am guessing I am not using m.if3 correctly, please help me for checking, thank you so much.

x = m.Array(m.Var,[52,6])
x[:,0]= m.Var(integer=True,lb=0,ub=1)
x[:,1:6] = m.Var(integer=True,lb=0,ub=30)

for i in range(0,52):
    a = m.if3(-x[i,0],5,0)
    m.Equation(sum(x[i,1:6]) == a)

Upvotes: 2

Views: 104

Answers (1)

John Hedengren
John Hedengren

Reputation: 14346

The a = m.if3(-x[i,0],5,0) function should have a switching point that is away from the solution. The numerical solvers treat x[i,0]=1e-8 the same as x[i,0]=-1e-8 so it may be on one side or the other of the switch point. If you need to use m.if3() then set the switch point away from the one solution such as a = m.if3(-x[i,0]+0.5,5,0) so that the values of -x[i,0]+0.5 are between -0.5 and 0.5.

A better way to formulate this problem is to use the x[i,0] value as the binary switch that defines a such as:

a = [None]*n
for i in range(n):
    b = x[i,0] # renaming to b
    # simplified: a[i] = m.Intermediate(b*5)
    a[i] = m.Intermediate(b*5+(1-b)*0)
    m.Equation(sum(x[i,1:])==a[i])

Here is a complete version that demonstrates the switching.

from gekko import GEKKO
m = GEKKO(remote=False)
# define array of integer variables (nx6)
n = 3 # for testing, change to 52 otherwise
x = m.Array(m.Var,(n,6),lb=0,ub=5,integer=True)
# change upper bound to 1 for binary variables
for xi in x[:,0]:
    xi.upper = 1
a = [None]*n
for i in range(n):
    b = x[i,0]
    a[i] = m.Intermediate(b*5+(1-b)*0)
    m.Equation(sum(x[i,1:])==a[i])
m.Equation(sum(a)==5*2)
m.options.SOLVER=1
m.solve()
print('a')
print(a)
print('x')
print(x)

With a Solution:

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   1.339999999618158E-002 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------
 

a
[[5.0], [0.0], [5.0]]
x
[[[1.0] [1.0] [1.0] [1.0] [1.0] [1.0]]
 [[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
 [[1.0] [0.0] [2.0] [0.0] [1.0] [2.0]]]

Upvotes: 2

Related Questions