Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Optimizer Reference Manual
Light LogoDark Logo

Concepts

Features

Reference

Gurobi
Back to top

R API - Solving a Model#

gurobi(model,params=NULL)#

This function optimizes the given model. The algorithm used for theoptimization depends on the model type (simplex or barrier for acontinuous model; branch-and-cut for a MIP model). Upon successfulcompletion it will return alist variable containing solutioninformation.

Please consultVariables and Constraints sectionin the reference manual for a discussion of some of the practical issuesassociated with solving a precisely defined mathematical model usingfinite-precision floating-point arithmetic.

Parameters:
  • model – The modellist must contain a valid Gurobi model. See themodel argument section for more information.

  • params – The paramslist, when provided, contains a list of modified Gurobiparameters. See theparams argument section for moreinformation.

Example:
result<-gurobi(model,params)if(result$status=='OPTIMAL'){print(result$objval)print(result$x)}else{cat('Optimization returned status:',formatC(result$status),'\n')}
Returns:

The optimization result

Thegurobi function returns alist, with the variousresults of the optimization stored in its named components. The specificresults that are available depend on the type of model that was solved,the parameters used, and the status of the optimization. The followingis a list of named components that might be available in the returnedresult. We will discuss the circumstances under which each will beavailable after presenting the list.

Model named components

status

The status of the optimization, returned as a string. The desiredresult isOPTIMAL, which indicates that anoptimal solution to the model was found. Other status are possible,for example if the model has no feasible solution or if you set aGurobi parameter that leads to early solver termination. See theStatus Code section for further informationon the Gurobi status codes.

objval

The objective value of the computed solution. Note that formulti-objective modelsresult$objval will be avector, whereresult$objval[[i]] stores the value formodel$multiobj[[i]].

objbound

Best available bound on solution (lower bound for minimization, upperbound for maximization).

objboundc

The best unrounded bound on the optimal objective. In contrast toobjbound, this attribute does not take advantage of objectiveintegrality information to round to a tighter bound. For example, ifthe objective is known to take an integral value and the current bestbound is 1.5,objbound will return 2.0 whileobjboundc willreturn 1.5.

mipgap

Current relative MIP optimality gap; computed as\(\vert ObjBound-ObjVal\vert/\vert ObjVal\vert\) (where\(ObjBound\) and\(ObjVal\) are the MIP objective bound andincumbent solution objective, respectively). ReturnsGRB_INFINITYwhen an incumbent solution has not yet been found, when no objectivebound is available, or when the current incumbent objective is 0.This is only available for mixed-integer problems.

runtime

The elapsed wall-clock time (in seconds) for the optimization.

work

The work (in work units) spent on the optimization. As opposed to theruntime in seconds, the work is deterministic. This means that on thesame hardware and with the same parameter and attribute settings,solving the same model twice will lead to exactly the same amount ofwork in each of the two solves. One work unit corresponds veryroughly to one second, but this greatly depends on the hardware onwhich Gurobi is running and on the model that has been solved.

itercount

Number of simplex iterations performed.

baritercount

Number of barrier iterations performed.

nlbaritercount

Number of NL barrier iterations performed.

pdhgitercount

Number of NL barrier iterations performed.

nodecount

Number of branch-and-cut nodes explored.

maxvio

Value of the maximal (unscaled) violation of the returned solution.

farkasproof

Magnitude of infeasibility violation in Farkas infeasibility proof.Only available if the model was found to be infeasible. Please refertoAttribute section in the reference manualfor details.

Variable named components

x

The computed solution. This vector contains one entry for each columnofA.

rc

Variable reduced costs for the computed solution. This vectorcontains one entry for each column ofA.

vbasis

Variable basis status values for the computed optimal basis. Yougenerally should not concern yourself with the contents of thisvector. If you wish to use an advanced start later, you would simplycopy thevbasis andcbasis named components into thecorresponding named components for the next model. Thisvector contains one entry for each column ofA.

unbdray

Unbounded ray. Provides a vector that, when added to any feasiblesolution, yields a new solution that is also feasible but improvesthe objective. Only available if the model is found to be unbounded.This vector contains one entry for each column ofA.

Linear constraint named components

slack

The constraint slack for the computed solution. This vector containsone entry for each row ofA.

pi

Dual values for the computed solution (also known asshadowprices). This vector contains one entry for each row ofA.

cbasis

Constraint basis status values for the computed optimal basis. Thisvector contains one entry for each row ofA.

farkasdual

Farkas infeasibility proof. Only available if the model was found tobe infeasible. Please refer toAttributesection in the reference manual for details.

Quadratic constraint named components

qcslack

The quadratic constraint slack in the current solution. Thisvector contains one entry for each quadratic constraint.

qcpi

The dual values associated with the quadratic constraints. Thisvector contains one entry for each quadratic constraint.

Solution Pool named components

pool

When multiple solutions are found during the optimization call, thesesolutions are returned in this named component. A list of lists. Whenpresent, each list has the following named components:

objval

Stores the objective value of the\(i\)-th solution inresult$pool[[i]]$objval. Note thatwhen the model is a multi-objective model, instead of a singlevalue,result$pool[[i]]$objval[j]stores the value of the\(j\)-th objective function for the\(i\)-th solution.

poolnx

Stores the\(i\)-th solution inresult$pool[[i]]$poolnx. This vectorcontains one entry for each column ofA.

Note that to query the number of solutions stored, you can query thelength ofresult$pool.

poolobjbound

For single-objective MIP optimization problems, this value gives abound on the best possible objective of an undiscovered solution. Thedifference between this value andobjbound is that the formergives an objective bound for undiscovered solutions, while the lattergives a bound for any solution.

Multi-objective named components

objnpass

The index of the optimization pass in which objective functionkwas processed is stored inresult$objnpass[[k]]. Ifthe objective was not processed (e.g. a time limit was reachedduring some prior optimization pass), that value is 0.

multiobj

When solving a multi-objective problem, certain solving attributesare returned in this named component. When present, each list hasthe following named components:

objpassnitercount

The simplex iteration count for the optimization passp isstored inresult$multiobj[[p]]$objpassnitercount.

objpassnmipgap

The MIP gap for the optimization passp is stored inresult$multiobj[[p]]$objpassnmipgap.

objpassnnodecount

The number of explored nodes for the optimization passp isstored inresult$multiobj[[p]]$objpassnnodecount.

objpassnbound

The objective bound for the optimization passp is stored inresult$multiobj[[p]]$objpassnbound.

objpassnval

The objective value for the optimization passp is stored inresult$multiobj[[p]]$objpassnval.

objpassnopennodecount

The number of unexplored nodes for the optimization passp isstored inresult$multiobj[[p]]$objpassnopennodecount.

objpassnruntime

The runtime for the optimization passp is stored inresult$multiobj[[p]]$objpassnruntime.

objpassnstatus

The status for the optimization passp is stored inresult$multiobj[[p]]$objpassnstatus.

objpassnwork

The work (in work units) spent on the optimization passpis stored inresult$multiobj[[p]]$objpassnwork.

Note that to query the number of optimization passes stored, you canquery the length ofresult$multiobj.

What is Available When

Thestatus named component will be present in all cases. Itindicates whether Gurobi was able to find a proven optimal solution tothe model. In cases where a solution to the model was found, optimal orotherwise, theobjval andx named components will be present.

For linear and quadratic programs, if a solution is available, then thepi andrc named components will also be present. For models withquadratic constraints, if the parameterqcpdual is set to 1, thenamed componentqcpi will be present. If the final solution is abasic solution (computed by simplex), thenvbasis andcbasiswill be present. If the model is an unbounded linear program and theInfUnbdInfo parameter is set to 1, the namedcomponentunbdray will be present. Finally, if the model is aninfeasible linear program and theInfUnbdInfo parameteris set to 1, the named componentsfarkasdual andfarkasproofwill be set.

For mixed integer problems, no dual information (i.e.pi,slack,rc,vbasis,cbasis,qcslack,qcpi,ubdray orfarkasdual) is ever available. When multiple solutions are found,thepool andpoolobjbound named components will be present.Depending on thestatus named component value, the named componentsnodecount,objbound,objbundc andmipgap will beavailable.

For continuous and mixed-integer models, under normal execution, thenamed componentsruntime,work,itercount,baritercount,nlbaritercount, andpdhgitercount will be available.

gurobi_iis(model,params=NULL)#

Compute an Irreducible Inconsistent Subsystem (IIS).

An IIS is a subset of the constraints and variable bounds with thefollowing properties:

  • It is still infeasible, and

  • If a single constraint or bound is removed, the subsystem becomesfeasible.

Note that an infeasible model may have multiple IISs. The one returnedby Gurobi is not necessarily the smallest one; there may exist otherswith fewer constraints or bounds.

You can obtain information about the outcome of the IIS computation fromthe returned IIS result (described below). Note that this method can beused to compute IISs for both continuous and MIP models.

Parameters:
  • model – The modellist must contain a valid Gurobi model. See themodel argument section for more information.

  • params – The paramslist, when provided, contains a list of modified Gurobiparameters. See theparams argument section for moreinformation.

Example:
model<-gurobi_read('examples/data/klein1.mps')iis<-gurobi_iis(model)
Returns:

Thegurobi_iis() function returns alist, with various resultsstored in its named components. The specific results that are availabledepend on the type of model.

The returnedlist will always contain the followingnamedcomponents:

minimal

A logical scalar that indicates whether the computed IIS is minimal.It will normally be true, but it may be false if the IIS computationwas stopped early (due to a time limit or a user interrupt).

Arows

A logical vector that indicates whether a linear constraint appearsin the computed IIS.

lb

A logical vector that indicates whether a lower bound appears in thecomputed IIS.

ub

A logical vector that indicates whether a upper bound appears in thecomputed IIS.

If your model contains general constraints, the returnedlist willalso contain the followingnamedcomponents:

genconmax

A logical vector that indicates whether a general MAXconstraint appears in the computed IIS.

genconmin

A logical vector that indicates whether a general MINconstraint appears in the computed IIS.

genconand

A logical vector that indicates whether a general ANDconstraint appears in the computed IIS.

genconor

A logical vector that indicates whether a general ORconstraint appears in the computed IIS.

genconabs

A logical vector that indicates whether a general ABSconstraint appears in the computed IIS.

genconind

A logical vector that indicates whether a general INDICATORconstraint appears in the computed IIS.

genconpwl

A logical vector that indicates whether a general piecewise-linearfunction constraint appears in the computed IIS.

Deprecated since version 13.0:Usingfunction constraints is deprecated since version 13.0.Please usenonlinear constraints instead.

genconpoly

A logical vector that indicates whether a polynomial functionconstraint appears in the computed IIS.

genconexp

A logical vector that indicates whether a natural exponentialfunction constraint appears in the computed IIS.

genconexpa

A logical vector that indicates whether a exponential functionconstraint appears in the computed IIS.

genconlog

A logical vector that indicates whether a natural logarithmicfunction constraint appears in the computed IIS.

genconloga

A logical vector that indicates whether a logarithmic functionconstraint appears in the computed IIS.

genconlogistic

A logical vector that indicates whether a logistic functionconstraint appears in the computed IIS.

genconpow

A logical vector that indicates whether a power functionconstraint appears in the computed IIS.

genconsin

A logical vector that indicates whether a SIN functionconstraint appears in the computed IIS.

genconcos

A logical vector that indicates whether a COS functionconstraint appears in the computed IIS.

gencontan

A logical vector that indicates whether a TAN functionconstraint appears in the computed IIS.

If your model contains SOS constraints, the returnedlist will alsocontain the followingnamedcomponent:

sos

A logical vector that indicates whether an SOS constraint appears inthe computed IIS

If your model contains quadratic constraints, the returnedlist willalso contain the followingnamedcomponent:

quadcon

A logical vector that indicates whether a quadraticconstraint appears in the computed IIS.

To write the result of the IIS computationinto an .ilp file format, set theResultFileparameter before calling thegurobi function.

Example:
params$resultfile<-'infeas_submodel.ilp'result<-gurobi(model,params)
gurobi_feasrelax(model,relaxobjtype,minrelax,penalties,params=NULL)#

This function computes a feasibility relaxation for the inputmodelargument. The feasibility relaxation is a model that, when solved,minimizes the amount by which the solution violates the bounds andlinear constraints of the original model. You must provide a penalty toassociate with relaxing each individual bound or constraint (through thepenalties argument). These penalties are interpreted in differentways, depending on the value of therelaxobjtype argument.

For an example of how this function transforms a model, and more detailsabout the variables and constraints created, please seethis section.

Parameters:
  • model – The modellist must contain a valid Gurobi model. See themodel argument section for more information.

  • relaxobjtype – The approach used to impose penalties on violations.If you specifyrelaxobjtype=0, the objective for the feasibilityrelaxation is to minimize the sum of the weighted magnitudes of thebound and constraint violations.If you specifyrelaxobjtype=1, the objective for the feasibilityrelaxation is to minimize the weighted sum of the squares of the boundand constraint violations.If you specifyrelaxobjtype=2, the objective for the feasibilityrelaxation is to minimize the weighted count of bound and constraintviolations.In all cases, the weights are taken frompenalties$lb,penalties$ub andpenalties$rhs. Youcan provide the special penalty valueInf to indicate that thecorresponding bound or constraint cannot be relaxed.

  • minrelax – Theminrelax argument is a boolean that controls the type offeasibility relaxation that is created. Ifminrelax=FALSE,optimizing the returned model gives a solution that minimizes the costof the violation. Ifminrelax=TRUE, optimizing the returned modelfinds a solution that minimizes the original objective, but only fromamong those solutions that minimize the cost of the violation. Note thatgurobi_feasrelax must solve an optimization problem to find theminimum possible relaxation whenminrelax=TRUE, which can be quiteexpensive.

  • penalties

    Thepenalties argument is a list of lists, having the followingoptional named components (default: allInf):

    • lb Penalty for violating each lower bound. There should beas many values as variables into the model. Note that artificialvariables may have been created automatically by Gurobi forrange constraints.

    • ub Penalty for violating each upper bound. There should beas many values as variables into the model. Note that artificialvariables may have been created automatically by Gurobi forrange constraints.

    • rhs Penalty for violating each constraint. There should beas many values as constraints into the model.

    To give an example, if a constraint withpenalties.rhs valuepis violated by 2.0, it would contribute2*p to the feasibilityrelaxation objective forrelaxobjtype=0,2*2*p forrelaxobjtype=1, andp forrelaxobjtype=2.

  • params – The paramslist, when provided, contains a list of modified Gurobiparameters. See theparams argument section for moreinformation.

Returns:

Alist containing two named components:result$model, a list variable, as described in themodel argument section.result$feasobj, a scalar. Ifminrelax==TRUE thisis the relaxation problem objective value, 0.0 otherwise.

Example:
penalties<-list()model<-gurobi_read('stein9.mps')penalties$lb<-rep(1,length(model$lb))penalties$ub<-rep(1,length(model$ub))penalties$rhs<-rep(1,length(model$rhs))feasrelaxresult<-gurobi_feasrelax(model,0,FALSE,penalties)
gurobi_relax(model,params=NULL)#

Create the relaxation of a MIP model. Transforms integer variables intocontinuous variables, and removes SOS and general constraints.

Parameters:
  • model – The modellist must contain a valid Gurobi model. See themodel argument section for more information.

  • params – The paramslist, when provided, contains a list of modified Gurobiparameters. See theparams argument section for moreinformation.

Returns:

Amodel list variable, as described in themodelparameter section.

Example:
model<-gurobi_read('stein9.mps')relaxed<-gurobi_relax(model)

Help and Feedback

On this page

[8]ページ先頭

©2009-2025 Movatter.jp