3

I want to solve linear programming with Python. The model is:

Maximizing x1 + x2  
S.t:  
    x1 + x2 <=1  
    0<= x1 , x2 <=1

So I tried this:

from gekko import GEKKO

model = GEKKO(remote=False)
x1 = model.Var(0.2 , lb=0 , ub=1)
x2 = model.Var(0.2 , lb=0 , ub=1)

model.Equation = (sum(x1 , x2) <=1)

model.Maximize(sum(x1 , x2))

But I got:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8024/1372822120.py in <module>
      6 x2 = model.Var(0.2 , lb=0 , ub=1)
      7 
----> 8 model.Equation = (sum(x1 , x2) <=1)
      9 
     10 model.Maximize(sum(x1 , x2))

~\Anaconda3\envs\Python3.10\lib\site-packages\gekko\gk_variable.py in __getitem__(self, key)
     78         return len(self.value)
     79     def __getitem__(self,key):
---> 80         return self.value[key]
     81     def __setitem__(self,key,value):
     82         self.value[key] = value

~\Anaconda3\envs\Python3.10\lib\site-packages\gekko\gk_operators.py in __getitem__(self, key)
    145 
    146     def __getitem__(self,key):
--> 147         return self.value[key]
    148 
    149     def __setattr__(self, name, value):

TypeError: 'float' object is not subscriptable
10
  • 1
    Please edit your question to include the entire error message. Commented Feb 24, 2022 at 19:26
  • 4
    simply change sum(x1 , x2) to x1 + x2 Commented Feb 24, 2022 at 19:26
  • sum() takes an iterable and a start argument Commented Feb 24, 2022 at 19:27
  • But... the code and error in the traceback is different from what you've shown in the question Commented Feb 24, 2022 at 19:29
  • That set of inequalities has an infinite number of solutions. Not sure what you're expecting from this. Commented Feb 24, 2022 at 19:29

1 Answer 1

3

As I mentioned in the comment section, simply changing sum(x1 , x2) to x1 + x2 should solve the issue. Also, you should try Solving the model with model.solve()! so:

from gekko import GEKKO

model = GEKKO(remote=False)
x1 = model.Var(0.2 , lb=0 , ub=1)
x2 = model.Var(0.2 , lb=0 , ub=1)

model.Equation = (x1 + x2 <=1)

model.Maximize(x1 + x2)
model.solve()

If you want to know about the optimal solution and avoid the complete report of Gekko, you can set model.solve(disp=False) and then try:

x1[0] , x2[0]

this gives me:

(1.0, 1.0)

Then whereas your objective function is x1 + x2, You can get the optimal value of the objective function by:

x1[0] + x2[0]
>>> 2.0
Sign up to request clarification or add additional context in comments.

7 Comments

Right, there's still a sum(x1,x2) in there.
What result did it produce? I don't really want to install all of that.
@TimRoberts I did. Out of curiosity. It gives a very long print out and in the end The final value of the objective function is -2.00000001947265. Have no idea what that means...
@TimRoberts I'll write about it in continue! Edited.
But, but, but (1.0,1.0) violates the constraint that x1+x2 <= 1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.