Documentation
¶
Overview ¶
Package goop provides a general interface for solving mixed integer linear optimization problems using a variety of back-end solvers.
Index ¶
- Constants
- type Constr
- type ConstrSense
- type Expr
- type K
- type LinearExpr
- func (e *LinearExpr) Coeffs() []float64
- func (e *LinearExpr) Constant() float64
- func (e *LinearExpr) Eq(other Expr) *Constr
- func (e *LinearExpr) GreaterEq(other Expr) *Constr
- func (e *LinearExpr) LessEq(other Expr) *Constr
- func (e *LinearExpr) Mult(c float64) Expr
- func (e *LinearExpr) NumVars() int
- func (e *LinearExpr) Plus(other Expr) Expr
- func (e *LinearExpr) Vars() []uint64
- type Model
- func (m *Model) AddBinaryVar() *Var
- func (m *Model) AddBinaryVarMatrix(rows, cols int) [][]*Var
- func (m *Model) AddBinaryVarVector(num int) []*Var
- func (m *Model) AddConstr(constr *Constr)
- func (m *Model) AddVar(lower, upper float64, vtype VarType) *Var
- func (m *Model) AddVarMatrix(rows, cols int, lower, upper float64, vtype VarType) [][]*Var
- func (m *Model) AddVarVector(num int, lower, upper float64, vtype VarType) []*Var
- func (m *Model) Optimize(solver solvers.Solver) (*Solution, error)
- func (m *Model) SetObjective(e Expr, sense ObjSense)
- func (m *Model) SetTimeLimit(dur time.Duration)
- func (m *Model) ShowLog(shouldShow bool)
- type ObjSense
- type Objective
- type Solution
- type Var
- func (v *Var) Coeffs() []float64
- func (v *Var) Constant() float64
- func (v *Var) Eq(other Expr) *Constr
- func (v *Var) GreaterEq(other Expr) *Constr
- func (v *Var) ID() uint64
- func (v *Var) LessEq(other Expr) *Constr
- func (v *Var) Lower() float64
- func (v *Var) Mult(c float64) Expr
- func (v *Var) NumVars() int
- func (v *Var) Plus(e Expr) Expr
- func (v *Var) Type() VarType
- func (v *Var) Upper() float64
- func (v *Var) Vars() []uint64
- type VarType
Examples ¶
Constants ¶
const ( Zero = K(0) One = K(1) )
Integer constants represnting commonly used numbers. Makes for better readability
const ( SenseEqual ConstrSense = '=' SenseLessThanEqual = '<' SenseGreaterThanEqual = '>' )
Different constraint senses conforming to Gurobi's encoding.
const ( Continuous VarType = 'C' Binary = 'B' Integer = 'I' )
Multiple common variable types have been included as constants that conform to Gurobi's encoding.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Constr ¶
type Constr struct {
// contains filtered or unexported fields
}
Constr represnts a linear constraint of the form x <= y, x >= y, or x == y. Constr uses a left and right hand side expressions along with a constraint sense (<=, >=, ==) to represent a generalized linear constraint
type ConstrSense ¶
type ConstrSense byte
ConstrSense represents if the constraint x <= y, x >= y, or x == y. For easy integration with Gurobi, the senses have been encoding using a byte in the same way Gurobi encodes the constraint senses.
type Expr ¶
type Expr interface { // NumVars returns the number of variables in the expression NumVars() int // Vars returns a slice of the Var ids in the expression Vars() []uint64 // Coeffs returns a slice of the coefficients in the expression Coeffs() []float64 // Constant returns the constant additive value in the expression Constant() float64 // Plus adds the current expression to another and returns the resulting // expression Plus(e Expr) Expr // Mult multiplies the current expression to another and returns the // resulting expression Mult(c float64) Expr // LessEq returns a less than or equal to (<=) constraint between the // current expression and another LessEq(e Expr) *Constr // GreaterEq returns a greater than or equal to (>=) constraint between the // current expression and another GreaterEq(e Expr) *Constr // Eq returns an equality (==) constraint between the current expression // and another Eq(e Expr) *Constr }
Expr represents a linear general expression of the form c0 * x0 + c1 * x1 + ... + cn * xn + k where ci are coefficients and xi are variables and k is a constant. This is a base interface that is implemented by single variables, constants, and general linear expressions.
func NewExpr ¶
NewExpr returns a new expression with a single additive constant value, c, and no variables. Creating an expression like sum := NewExpr(0) is useful for creating new empty expressions that you can perform operatotions on later
func NewLinearExpr ¶
NewLinearExpr returns a new expression with a single additive constant value, c, and no variables.
func Sum ¶
Sum returns the sum of the given expressions. It creates a new empty expression and adds to it the given expressions.
func SumCol ¶
SumCol returns the sum of all variables in a single specified column of a variable matrix.
type K ¶
type K float64
K is a constant expression type for an MIP. K for short ¯\_(ツ)_/¯
func (K) Coeffs ¶
Coeffs returns a slice of the coefficients in the expression. For constants, this is always nil
func (K) Constant ¶
Constant returns the constant additive value in the expression. For constants, this is just the constants value
func (K) GreaterEq ¶
GreaterEq returns a greater than or equal to (>=) constraint between the current expression and another
func (K) LessEq ¶
LessEq returns a less than or equal to (<=) constraint between the current expression and another
func (K) Mult ¶
Mult multiplies the current expression to another and returns the resulting expression
func (K) NumVars ¶
NumVars returns the number of variables in the expression. For constants, this is always 0
type LinearExpr ¶
type LinearExpr struct {
// contains filtered or unexported fields
}
LinearExpr represents a linear general expression of the form c0 * x0 + c1 * x1 + ... + cn * xn + k where ci are coefficients and xi are variables and k is a constant
func (*LinearExpr) Coeffs ¶
func (e *LinearExpr) Coeffs() []float64
Coeffs returns a slice of the coefficients in the expression
func (*LinearExpr) Constant ¶
func (e *LinearExpr) Constant() float64
Constant returns the constant additive value in the expression
func (*LinearExpr) Eq ¶
func (e *LinearExpr) Eq(other Expr) *Constr
Eq returns an equality (==) constraint between the current expression and another
func (*LinearExpr) GreaterEq ¶
func (e *LinearExpr) GreaterEq(other Expr) *Constr
GreaterEq returns a greater than or equal to (>=) constraint between the current expression and another
func (*LinearExpr) LessEq ¶
func (e *LinearExpr) LessEq(other Expr) *Constr
LessEq returns a less than or equal to (<=) constraint between the current expression and another
func (*LinearExpr) Mult ¶
func (e *LinearExpr) Mult(c float64) Expr
Mult multiplies the current expression to another and returns the resulting expression
func (*LinearExpr) NumVars ¶
func (e *LinearExpr) NumVars() int
NumVars returns the number of variables in the expression
func (*LinearExpr) Plus ¶
func (e *LinearExpr) Plus(other Expr) Expr
Plus adds the current expression to another and returns the resulting expression
func (*LinearExpr) Vars ¶
func (e *LinearExpr) Vars() []uint64
Vars returns a slice of the Var ids in the expression
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model represents the overall constrained linear optimization model to be solved. Model contains all the variables associated with the optimization problem, constraints, objective, and parameters. New variables can only be created using an instantiated Model.
Example (Simple) ¶
This example shows how goop can be used to solve a simple MIP:
// maximize x + y + 2 z // subject to x + 2 y + 3 z <= 4 // x + y >= 1 // x, y, z binary
MIP being modelled is the same as in http://www.gurobi.com/documentation/7.5/examples/mip1_cpp_cpp.html
package main import ( "fmt" "github.com/mit-drl/goop" "github.com/mit-drl/goop/solvers" ) func main() { // Instantiate a new model m := goop.NewModel() // Add your variables to the model x := m.AddBinaryVar() y := m.AddBinaryVar() z := m.AddBinaryVar() // Add your constraints m.AddConstr(goop.Sum(x, y.Mult(2), z.Mult(3)).LessEq(goop.K(4))) m.AddConstr(goop.Sum(x, y).GreaterEq(goop.One)) // Set a linear objective using your variables obj := goop.Sum(x, y, z.Mult(2)) m.SetObjective(obj, goop.SenseMaximize) // Optimize the variables according to the model sol, err := m.Optimize(solvers.NewLPSolveSolver()) // Check if there is an error from the solver. No error should be returned // for this model if err != nil { panic("Should not have an error") } // Print out the solution fmt.Println("x =", sol.Value(x)) fmt.Println("y =", sol.Value(y)) fmt.Println("z =", sol.Value(z)) }
Output: x = 1 y = 0 z = 1
func NewModel ¶
func NewModel() *Model
NewModel returns a new model with some default arguments such as not to show the log and no time limit.
func (*Model) AddBinaryVar ¶
AddBinaryVar adds a binary variable to the model and returns said variable.
func (*Model) AddBinaryVarMatrix ¶
AddBinaryVarMatrix adds a matrix of binary variables to the model and returns the resulting slice.
func (*Model) AddBinaryVarVector ¶
AddBinaryVarVector adds a vector of binary variables to the model and returns the slice.
func (*Model) AddVar ¶
AddVar adds a variable of a given variable type to the model given the lower and upper value limits. This variable is returned.
func (*Model) AddVarMatrix ¶
AddVarMatrix adds a matrix of variables of a given type to the model with lower and upper value limits and returns the resulting slice.
func (*Model) AddVarVector ¶
AddVarVector adds a vector of variables of a given variable type to the model. It then returns the resulting slice.
func (*Model) Optimize ¶
Optimize optimizes the model using the given solver type and returns the solution or an error.
func (*Model) SetObjective ¶
SetObjective sets the objective of the model given an expression and objective sense.
func (*Model) SetTimeLimit ¶
SetTimeLimit sets the solver time limit for the model.
type ObjSense ¶
type ObjSense int
ObjSense represents whether an optimization objective is to be maximized or minimized. This implementation conforms to the Gurobi encoding
const ( SenseMinimize ObjSense = 1 SenseMaximize = -1 )
Objective senses (minimize and maximize) encoding using Gurobi's standard
type Objective ¶
type Objective struct { Expr // contains filtered or unexported fields }
Objective represents an optimization objective given an expression and objective sense (maximize or minimize).
func NewObjective ¶
NewObjective returns a new optimization objective given an expression and objective sense
type Solution ¶
type Solution struct { // The objective for the solution Objective float64 // Whether or not the solution is within the optimality threshold Optimal bool // The optimality gap returned from the solver. For many solvers, this is // the gap between the best possible solution with integer relaxation and // the best integer solution found so far. Gap float64 // contains filtered or unexported fields }
Solution stores the solution of an optimization problem and associated metatdata
type Var ¶
type Var struct {
// contains filtered or unexported fields
}
Var represnts a variable in a optimization problem. The variable is identified with an uint64.
func (*Var) Coeffs ¶
Coeffs returns a slice of the coefficients in the expression. For a variable, it always returns a singleton slice containing the value one.
func (*Var) Constant ¶
Constant returns the constant additive value in the expression. For a variable, it always returns zero.
func (*Var) GreaterEq ¶
GreaterEq returns a greater than or equal to (>=) constraint between the current expression and another
func (*Var) LessEq ¶
LessEq returns a less than or equal to (<=) constraint between the current expression and another
func (*Var) Mult ¶
Mult multiplies the current expression to another and returns the resulting expression
func (*Var) NumVars ¶
NumVars returns the number of variables in the expression. For a variable, it always returns one.
func (*Var) Plus ¶
Plus adds the current expression to another and returns the resulting expression.