简体   繁体   中英

how to change gurobi to pyomo solver

Since it is a secondary constraint problem from grobi to pyomo in python, i want to change it, so I'm having trouble not knowing how to do it.

from gurobipy import *
import pandas as pd
import numpy as np
import csv
from pyomo.environ import SolverFactory
opt = SolverFactory("optimizer", solver_io="python")
opt.solve(model, tee=True)
opt.options['NonConvex'] = 2

after that, i inserted data from excel using pandas.

model=Model("optimizer")
model.params.NonConvex=2

and ran the gurobi program. if the error message is

untimeError: Attempting to use an unavailable solver.

The SolverFactory was unable to create the solver "optimizer"
and returned an UnknownSolver object.  This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").

The original solver was created with the following parameters:
    executable: optimizer
    solver_io: python
    type: optimizer
    _args: ()
    options: {}

https://pyomo.readthedocs.io/en/stable/working_models.html?highlight=options#sending-options-to-the-solver

I thought that I would change variously referring to this site, there are many things I don't understand. for example,

results = optimizer.solve(instance, options="threads=4", tee=True)

do I need to change both the constants and variables of formulation to "optimizer.solve?" Is it only dictionary notation to change? Don't need to change the int type or list? also, should I change everything () to instance?

s={(1:2,2:3,3:5,4:5)}
d={(1,1):1(1,2):2(1:3):3(1:4):4
  (1,1):3(1,2):4(1:3):5(1:4):6}

How do I change it with s,d above? is there anything i need to add to "options="=dict name=dict number"? for example, if there are constraint expressions or objective functions, please let me know. because I'm a beginner, it would be helpful if you could tell me as much detail as possible.

It seems that you're mixing up different approaches

  1. gurobipy allows you to model and solve problems using a Python API
  2. Pyomo is a modeling language and allows you to model your optimization problem and then solve it using gurobi or another solver (glpk, cbc, mosek, etc.).

in your code

from pyomo.environ import SolverFactory
opt = SolverFactory("optimizer", solver_io="python")

This is a Pyomo statement which tells to Pyomo that you will use the optimizer solver (which as the RuntimeError told you, didn't exist)

Your code here

model=Model("optimizer")
model.params.NonConvex=2

tells me that you're using the Gurobi API in python for modeling your problem.

You need to choose model your problem whether using gurobipy or pyomo. From both you can solve using gurobi, but do not mix both aproaches.

Since in model=Model('optimizer') do you already have your model with gurobipy, if you got a license and gurobi installed in your PC, you just need to call model.optimize() to solve the problem

PS: The following code just told to Pyomo that you are using the solver optimizer solver (previously declared in the SolverFactory line) to solve the program called instance , using 4 threads from your computer CPU and that you want to see the log in screen

results = optimizer.solve(instance, options="threads=4", tee=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM