Simple use of Gurobi: Python API

Foreword:

  1. The CSDN blog and the public account "ManTou Mantou" simultaneously publish blog posts and tweets, all of which are learned and exchanged, and the level is limited~
  2. There is a delay in CSDN communication, usually one or two days to see the comments and private messages of fans, and only three private messages can be sent~
  3. The official account is not only operated by ManTou, everyone can pay attention to the content you want to follow~

Gurobi : Python API

Beep beep: ManTou is also used to see, please correct me

Gurobi Reference Manual

Gurobi installs with the software the instruction manual, refman and example, the file path is: Gurobi installation path\gurobi903\win64\docs.
You can refer to the official manual to use Gurobi

Key function use

  1. Model(name=“”)
  • name is the name of the model, the return value is a model object, initially there are no variables and constraints
  • Calling method:model1 = Model()
  1. Model.addVar(lb, ub, obj, vtype, name,column)
  • lb is the lower limit, ub is the upper limit, obj is the optimization coefficient of the target, vtype is the variable type, name is the name of the variable, and column is the constraint and optimization coefficient in which the variable participates.
  • All parameters are optional, if not specified is the default value
  • Variable types are GRB.CONTINUOUS, GRB.BINARY, GRB.INTEGER, GRB.SEMICONT, or GRB.SEMIINT
  1. Model.addConstr( lhs, sense=None, rhs=None, name="" )
  • lhs is the right side of the constraint, rhs is the left side of the constraint, sense is the type of constraint, there are (GRB.LESS_EQUAL, GRB.EQUAL, or GRB.GREATER_EQUAL
  • name is the name of the constraint
  1. Model.addConstrs( generator, name="" )
  • generator is a Python expression. This expression is a bit more intuitive than 2, and the return value is of tupledict type
  • 例:model.addConstrs(x[i] + x[j] <= 1 for i in range(5) for j in range(5))
  • Note: Generator expressions can only have one comparison relationship.
  1. Model.update()
  • Update the modified model
  1. Model.setObjective(expr, sense)
  • Set the optimization function of the model
  • expr is the optimization target expression, sense is the optimization type, and the optimization types are GRB.MINIMIZE and GRB.MAXIMIZE. If you omit sense, you can use the ModelSense function to specify the optimization type.
  1. Model.write(filename)
  • Write the optimized model, solution vector, base vector, starting vector or parameter settings to a file. The file types are .mps, .rew, .lp and .rlp, or only a part of the saved model, see refman for details
  1. Model.getVars()
  • Return all variables in the model

For more complex usage, please refer to fefman and examples

Guess you like

Origin blog.csdn.net/qq_40678163/article/details/109406991