1

I am using the MIP optimization library to solve a problem, but I can't correctly declare the objective function. I wrote the code before using Gurobi to solve and I am just trying to translate the code over. I am having trouble rewriting the objective function with the correct syntax, any help would be very much appreciated, thank you!!

from mip import *
from mip import xsum, minimize
import pandas as pd
import numpy as np
import scipy as sp
m = Model()
m = Model(sense=MINIMIZE, solver_name=GRB)  
n = 80
# #x is charging, discharging variable
x = [m.add_var(name='x', var_type = INTEGER, lb=-1.5, ub = 1.5) for i in range(n)]
# #Y is SOC variable
Y = [m.add_var(name='Y', var_type = CONTINUOUS, lb=0, ub = 100) for i in range(n+1)]
# # Add constraint: SOC[start]=50, initial SOC
m += Y[0] == 50 , 'c1'
# #Final targeted SOC
m += Y[n] >= 65 , 'c2'
# # This is the constrain defines relationship between SOCs and charging steps. 3.75% SOC increase decrease are for 15min steps 
m += (Y[i+1]-Y[i] == 3.75*x[i] for i in range(n)), 'c0'
step=80
f1load=[44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48]
fload=f1load[0:step+1]
i1load=[40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]
iload=i1load[0:step+1]
(iload)-np.array(fload)
load1
#This command converts array to list so we can use it as a list in the rest of the code
load2=load1.tolist()
load=load2
# #Objective function. 6 comes from capacity of inverter. The line below is the Gurobi version of the obj function, that works. 
# obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
m.objective = xsum((load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])) for i in range (n))

Full stack trace of error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-31-ca98b7470c5c> in <module>
      2 # obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
      3 # m.objective = minimize(xsum(c[i]*x[i] for i in range(n)))
----> 4 m.objective = minimize(xsum((load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])) for i in range (n)))
      5 # m.objective = minimize(xsum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n))))

~/opt/anaconda3/lib/python3.7/site-packages/mip/model.py in xsum(terms)
   1321     """
   1322     result = LinExpr()
-> 1323     for term in terms:
   1324         result.add_term(term)
   1325     return result

<ipython-input-31-ca98b7470c5c> in <genexpr>(.0)
      2 # obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
      3 # m.objective = minimize(xsum(c[i]*x[i] for i in range(n)))
----> 4 m.objective = minimize(xsum((load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i])) for i in range (n)))
      5 # m.objective = minimize(xsum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n))))

~/opt/anaconda3/lib/python3.7/site-packages/mip/entities.py in __mul__(self, other)
    137 
    138     def __mul__(self: "LinExpr", other: Union[int, float]) -> "LinExpr":
--> 139         assert isinstance(other, (int, float))
    140         result = self.copy()
    141         result.__const *= other

AssertionError:
1
  • 1
    Never used that lib (besides running an example), but (load[i+1]-(6*x[i])) * (load[i+1]-(6*x[i]) clearly is a multiplication of some non-trivial objects, where this library does not support overloaded operators. It's basically expr * expr which quickly dives into computer algebra (although limited here to affine expressions). 1) You probably need to out-multiply this by hand to form the flat expression. 2) This looks quadratic and i don't think this library supports non-linear problems. Gurobi might solve it as MIQP/MISOCP, but this lib is probably called MIP for a reason Commented Feb 23, 2020 at 12:16

1 Answer 1

0

As @sascha mentioned, MIP library supports only Mixed Integer 'Linear' problems only but your objective is a non-linear. Just to make it run, you can make the objective function linear. Moreover you can try Pyomo (free), AMPL (commercial), MiniZinc (free), GAMS (commercial) or Python interface of Gurobi etc to model and solve non-linear problems. Please let us know what did work in your case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.