0

so i am trying to optimize my quadratic equation in python using gurobi library and my objective equation have summation in it. summation equation is showing error most probably because it cannot accept the variable integer and need constant integer.is there any way to put summation equation in objective function?

my code goes like this:

from gurobipy import *
import sympy as sy

op1=sy.Symbol('op1')
op2=sy.Symbol('op2')

# Create a model
quadratic_model = Model('quadratic')

# Define decision variables
n = quadratic_model.addVar(vtype=GRB.INTEGER, lb=0, name='n')
t = quadratic_model.addVar(vtype=GRB.INTEGER, lb=0, name='t')
temp = quadratic_model.addVar(vtype=GRB.INTEGER, lb=0, name='temp')

# Define objective function
obj_fn = (1256*80/12.5)*n*t + 25*5*n*t + sy.summation(5*t*50*(365//n)*op1,(op1,1,n)) + sy.summation(5*24*op2,(op2,1,365))
quadratic_model.setObjective(obj_fn, GRB.MINIMIZE)

# Add constraints
quadratic_model.addConstr(50*n*t >= 4320)
quadratic_model.addConstr(n>=1)
quadratic_model.addConstr(t>=1)
quadratic_model.addConstr(n<=6*30/4)            

# Solve model
quadratic_model.setParam('NonConvex', 2)
quadratic_model.optimize()

# Print results
for v in quadratic_model.getVars():
    print('---------------------------------------')
    print('%s : %g' % (v.varName, v.x)) 

print("minimized solution for the equation is:", quadratic_model.objVal)`

i am trying to just run this code with summation in optimization equation and there were 2 things that i tried as mentioned below

  1. i have tried to eliminate 365//n by using variable temp and adding constraint that temp == 365//n and iam still not able to go par this problem.
  2. have tried using for loop also and then the problem become even more absurd as it shows error : module 'gurobipy' has no attribute 'INTEGER' which makes no sense since i have run quadratic gurobipy earlier by defining variables and didn't got such problem

1 Answers1

0
  1. You can't mix gurobi with sympy (sympy does not understand gurobi variables).

  2. The second summation is a constant (7625520). You can remove it from the objective.

  3. The first summation can be rewritten as:

    t*125*floor(365/n)*n*(n+1) 
    

We can do this in pieces

   x1=365/n                      : n*x1=365
   x2=floor(365/n)               : x2 <= x1, x2 >= x1-0.9999, x2 integer
   x3=t*125*floor(365/n)         : x3 = t*125*x2
   x4=t*125*floor(365/n)*n       : x4 = x3*n
   x5=t*125*floor(365/n)*n*(n+1) : x5=x4*(n+1)

Note: I used here that sy.summation(op1,(op1,1,n))=n*(n+1)/2

Check my math before using this.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39