-1

I am trying to model and solve an optimization problem, with python and gurobi optimizer. It is my first experience to solve a problem using optimizer. firstly I wrote a really big problem and add all variables and constraints, step by step. But there was problem(S) in that. so I reduce the problem to the small version, again and again. After all, now I have a very simple code:

from gurobipy import *

m = Model('net')
x = m.addVar(name = 'x')
y = m.addVar(name = 'y')
m.addConstr(x >= 0 and x <= 9000, name = 'flow0')
m.addConstr(y >= 0 and y <= 1000, name = 'flow1')
m.addConstr(y + x == 9990, name = 'total_flow')

m.setObjective(x *(4 + 0.6*(x/9000)) + (y * (4 + 0.6*(y/1000))), GRB.MINIMIZE)

solo = m.optimize()

if solo:
    print ('find!!!')

It actually is a simple network flow problem (for a graph with two nodes and two edges) I want to calculate the flow of each edge (x and y). Obviously the flow of each edge cant be negative and cant be bigger than edge capacity(x(capa) = 9000, y(capa) = 1000). and the third constraint shows the the total flow limitation on both edges. Finally, the objective function has to minimize the equation.

Now I have some question on this code:

  1. why 'solo' is (None)?
  2. How can I print solution variables. I used getAttr() function. but I couldn't find out the role of variables name (x, y or flow0, flow1)

3.Ive got this result. But I really cant understand this!!!! for example: what dose it calculate in each iteration??!

Tnx in advance, and excuse for my simple question...

image of results!!!!

sali
  • 33
  • 11
  • 1. Function `optimize()` [does not return anything](http://www.gurobi.com/documentation/8.0/refman/py_model_optimize.html). – DYZ Aug 05 '18 at 18:14
  • I though solve() function in CPLEX return solution and optimize() is like that. tnx – sali Aug 05 '18 at 18:49

1 Answers1

0

The optimize() method always returns None, see print(help(m.optimize)). The status of your model after calling this method is stored in m.status while the solution values are stored in the .X attribute for each variable (assumed the model was solved to optimality). To access them you can use m.getVars():

# your model ...
m.optimize()

if m.status = GRB.OPTIMAL:
    for var in m.getVars():
        print(var.VarName, var.X)

Your posted log shows for each iteration of the barrier method (also known as interior point method) the objective value. See here for a detailed overview.

joni
  • 6,840
  • 2
  • 13
  • 20