Documentation
¶
Overview ¶
Package glpk contains Go bindings for GLPK (GNU Linear Programming Kit).
For usage examples see https://github.com/lukpank/go-glpk#examples.
The binding is not complete but enough for my purposes. Fill free to contact me if there is some part of GLPK that you would like to use and it is not yet covered by the glpk package.
Package glpk is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Index ¶
- Constants
- type BndsType
- type CPXCP
- type Iocp
- type MPSCP
- type MPSFormat
- type Meth
- type MsgLev
- type ObjDir
- type OptError
- type PathError
- type Pricing
- type Prob
- func (p *Prob) AddCols(nrs int) int
- func (p *Prob) AddRows(nrs int) int
- func (p *Prob) ColKind(j int) VarType
- func (p *Prob) ColLB(j int) float64
- func (p *Prob) ColName(j int) string
- func (p *Prob) ColPrim(j int) float64
- func (p *Prob) ColStat(j int) VarStat
- func (p *Prob) ColType(j int) BndsType
- func (p *Prob) ColUB(j int) float64
- func (p *Prob) Copy(names bool) *Prob
- func (p *Prob) Delete()
- func (p *Prob) DualStat() SolStat
- func (p *Prob) Erase()
- func (p *Prob) Exact(parm *Smcp) error
- func (p *Prob) Intopt(params *Iocp) error
- func (p *Prob) LoadMatrix(ia, ja []int32, ar []float64)
- func (p *Prob) MatCol(j int) (ind []int32, val []float64)
- func (p *Prob) MatRow(i int) (ind []int32, val []float64)
- func (p *Prob) MipColVal(i int) float64
- func (p *Prob) MipObjVal() float64
- func (p *Prob) MipStatus() SolStat
- func (p *Prob) NumCols() int
- func (p *Prob) NumRows() int
- func (p *Prob) ObjCoef(j int) float64
- func (p *Prob) ObjDir() ObjDir
- func (p *Prob) ObjName() string
- func (p *Prob) ObjVal() float64
- func (p *Prob) PrimStat() SolStat
- func (p *Prob) ProbName() string
- func (p *Prob) ReadLP(params *CPXCP, filename string) error
- func (p *Prob) ReadMPS(format MPSFormat, params *MPSCP, filename string) error
- func (p *Prob) ReadProb(flags ProbRWFlags, filename string) error
- func (p *Prob) RowLB(i int) float64
- func (p *Prob) RowName(i int) string
- func (p *Prob) RowStat(i int) VarStat
- func (p *Prob) RowType(i int) BndsType
- func (p *Prob) RowUB(i int) float64
- func (p *Prob) SetColBnds(j int, typ BndsType, lb float64, ub float64)
- func (p *Prob) SetColKind(j int, kind VarType)
- func (p *Prob) SetColName(j int, name string)
- func (p *Prob) SetColStat(j int, stat VarStat)
- func (p *Prob) SetMatCol(j int, ind []int32, val []float64)
- func (p *Prob) SetMatRow(i int, ind []int32, val []float64)
- func (p *Prob) SetObjCoef(j int, coef float64)
- func (p *Prob) SetObjDir(dir ObjDir)
- func (p *Prob) SetObjName(name string)
- func (p *Prob) SetProbName(name string)
- func (p *Prob) SetRowBnds(i int, typ BndsType, lb float64, ub float64)
- func (p *Prob) SetRowName(i int, name string)
- func (p *Prob) SetRowStat(i int, stat VarStat)
- func (p *Prob) Simplex(parm *Smcp) error
- func (p *Prob) Status() SolStat
- func (p *Prob) WriteLP(params *CPXCP, filename string) error
- func (p *Prob) WriteMPS(format MPSFormat, params *MPSCP, filename string) error
- func (p *Prob) WriteProb(flags ProbRWFlags, filename string) error
- type ProbRWFlags
- type RTest
- type Smcp
- type SolStat
- type VarStat
- type VarType
Constants ¶
const ( MAX = ObjDir(C.GLP_MAX) // maximization MIN = ObjDir(C.GLP_MIN) // minimization )
Allowed values of type ObjDir (objective function direction).
const ( FR = BndsType(C.GLP_FR) // a free (unbounded) variable LO = BndsType(C.GLP_LO) // a lower-bounded variable UP = BndsType(C.GLP_UP) // an upper-bounded variable DB = BndsType(C.GLP_DB) // a double-bounded variable FX = BndsType(C.GLP_FX) // a fixed variable )
Allowed values of type BndsType (bounds type of a variable).
const ( UNDEF = SolStat(C.GLP_UNDEF) // solution is undefined FEAS = SolStat(C.GLP_FEAS) // solution is feasible INFEAS = SolStat(C.GLP_INFEAS) // solution is infeasible NOFEAS = SolStat(C.GLP_NOFEAS) // there is no feasible solution OPT = SolStat(C.GLP_OPT) // solution is optimal UNBND = SolStat(C.GLP_UNBND) // problem has unbounded solution )
Allowed values of type SolStat (solution status).
const ( CV = VarType(C.GLP_CV) // Contineous Variable IV = VarType(C.GLP_IV) // Integer Variable BV = VarType(C.GLP_BV) // Binary Variable. Equivalent to IV with 0<=iv<=1 )
Allowed values of type VarType (variable type).
const ( BS = VarStat(C.GLP_BS) // basic variable NL = VarStat(C.GLP_NL) // non-basic variable on lower bound NU = VarStat(C.GLP_NU) // non-basic variable on upper bound NF = VarStat(C.GLP_NF) // non-basic free (unbounded) variable NS = VarStat(C.GLP_NS) // non-basic fixed variable )
Allowed values of type VarStat (status of auxiliary/structural variable).
const ( EBADB = OptError(C.GLP_EBADB) // invalid basis ESING = OptError(C.GLP_ESING) // singular matrix ECOND = OptError(C.GLP_ECOND) // ill-conditioned matrix EBOUND = OptError(C.GLP_EBOUND) // invalid bounds EFAIL = OptError(C.GLP_EFAIL) // solver failed EOBJLL = OptError(C.GLP_EOBJLL) // objective lower limit reached EOBJUL = OptError(C.GLP_EOBJUL) // objective upper limit reached EITLIM = OptError(C.GLP_EITLIM) // iteration limit exceeded ETMLIM = OptError(C.GLP_ETMLIM) // time limit exceeded ENOPFS = OptError(C.GLP_ENOPFS) // no primal feasible solution ENODFS = OptError(C.GLP_ENODFS) // no dual feasible solution EROOT = OptError(C.GLP_EROOT) // root LP optimum not provided ESTOP = OptError(C.GLP_ESTOP) // search terminated by application EMIPGAP = OptError(C.GLP_EMIPGAP) // relative mip gap tolerance reached ENOFEAS = OptError(C.GLP_ENOFEAS) // no primal/dual feasible solution ENOCVG = OptError(C.GLP_ENOCVG) // no convergence EINSTAB = OptError(C.GLP_EINSTAB) // numerical instability EDATA = OptError(C.GLP_EDATA) // invalid data ERANGE = OptError(C.GLP_ERANGE) // result out of range )
Allowed values of type OptError (optimization error).
const ( // Usage example: // // lp := glpk.New() // defer lp.Delete() // ... // smcp := glpk.NewSmcp() // smcp.SetMsgLev(glpk.MSG_ERR) // if err := lp.Simplex(smcp); err != nil { // log.Fatal(err) // } MSG_OFF = MsgLev(C.GLP_MSG_OFF) // no output MSG_ERR = MsgLev(C.GLP_MSG_ERR) // warning and error messages only MSG_ON = MsgLev(C.GLP_MSG_ON) // normal output MSG_ALL = MsgLev(C.GLP_MSG_ALL) // full output MSG_DBG = MsgLev(C.GLP_MSG_DBG) // debug output )
Allowed values of type MsgLev (message level, default: glpk.MSG_ALL).
const ( // Usage example: // // lp := glpk.New() // defer lp.Delete() // ... // smcp := glpk.NewSmcp() // smcp.SetMeth(glpk.DUALP) // if err := lp.Simplex(smcp); err != nil { // log.Fatal(err) // } // PRIMAL = Meth(C.GLP_PRIMAL) // use primal simplex DUALP = Meth(C.GLP_DUALP) // use dual; if it fails, use primal DUAL = Meth(C.GLP_DUAL) // use dual simplex )
Allowed values of type Meth (simplex method option, default: glpk.PRIMAL).
const ( // Usage example: // // lp := glpk.New() // defer lp.Delete() // ... // smcp := glpk.NewSmcp() // smcp.SetPricing(glpk.PT_STD) // if err := lp.Simplex(smcp); err != nil { // log.Fatal(err) // } // PT_STD = Pricing(C.GLP_PT_STD) // standard (Dantzig rule) PT_PSE = Pricing(C.GLP_PT_PSE) // projected steepest edge )
Allowed values of type Pricing (pricing technique, default: glpk.PT_PSE).
const ( // Usage example: // // lp := glpk.New() // defer lp.Delete() // ... // smcp := glpk.NewSmcp() // smcp.SetRTest(glpk.RT_STD) // if err := lp.Simplex(smcp); err != nil { // log.Fatal(err) // } // RT_STD = RTest(C.GLP_RT_STD) // standard (textbook) RT_HAR = RTest(C.GLP_RT_HAR) // two-pass Harris' ratio test )
Allowed values of type RTest (ratio test technique, default: glpk.RT_HAR).
const ( // To read an MPS (fixed) file and switch to maximization (as // MPS format does not specify objective function direction // and GLPK assumes minimization) run // // lp := glpk.New() // defer lp.Delete() // lp.ReadMPS(glpk.MPS_DECK, nil, "someMaximizationProblem.mps") // lp.SetObjDir(glpk.MAX) // if err := lp.Simplex(nil); err != nil { // log.Fatal(err) // } // MPS_DECK = MPSFormat(C.GLP_MPS_DECK) // fixed (ancient) MPS format MPS_FILE = MPSFormat(C.GLP_MPS_FILE) // free (modern) MPS format )
MPS file format type (fixed or free).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CPXCP ¶
type CPXCP struct {
// contains filtered or unexported fields
}
CPXCP represent CPLEX LP format control parameters
type Iocp ¶
type Iocp struct {
// contains filtered or unexported fields
}
Iocp represents MIP solver control parameters, a set of parameters for Prob.Intopt(). Please use NewIocp() to create Iocp structure which is properly initialized.
func NewIocp ¶
func NewIocp() *Iocp
NewIocp creates and initializes a new Iocp struct, which is used by the branch-and-cut solver.
func (*Iocp) SetPresolve ¶
SetPresolve enables or disables the optional MIP presolver.
type MPSCP ¶
type MPSCP struct {
// contains filtered or unexported fields
}
MPSCP represent MPS format control parameters
type MPSFormat ¶
type MPSFormat int
MPSFormat represents MPS file format: either fixed (ancient) or free (modern) format.
type ObjDir ¶
type ObjDir int
ObjDir is used to specify objective function direction (maximization or minimization).
type PathError ¶
type PathError struct { Op string // operation (either "read" or "write") Path string // name of the file on which the operation was performed Message string // short description of the problem }
PathError is the error used by methods reading and writing MPS, CPLEX LP, and GPLK LP/MIP formats.
type Prob ¶
type Prob struct {
// contains filtered or unexported fields
}
Prob represens optimization problem. Use glpk.New() to create a new problem.
func (*Prob) AddCols ¶
AddCols adds columns (variables). Returns (1-based) index of the first of the added columns.
func (*Prob) AddRows ¶
AddRows adds rows (constraints). Returns (1-based) index of the first of the added rows.
func (*Prob) ColLB ¶
ColLB returns the lower bound of j-th column, i.e. the lower bound of the corresponding structural variable. I the column has no lower bound -math.MaxFloat64 is returned.
func (*Prob) ColType ¶
ColType returns the type of j-th column, i.e. the type of the corresponding structural variable.
func (*Prob) ColUB ¶
ColUB returns the upper bound of j-th column, i.e. the upper bound of the corresponding structural variable. I the column has no upper bound +math.MaxFloat64 is returned.
func (*Prob) Copy ¶
Copy returns a copy of the given optimization problem. If name is true also symbolic names are copies otherwise their not copied
func (*Prob) Delete ¶
func (p *Prob) Delete()
Delete deletes a problem. Calling Delete on a deleted problem will have no effect (It is save to do so). But calling any other method on a deleted problem will panic. The problem will be deleted on garbage collection but you can do this as soon as you no longer need the optimization problem.
func (*Prob) Erase ¶
func (p *Prob) Erase()
Erase erases the problem. After erasing the problem is empty as if it were created with glpk.New().
func (*Prob) Exact ¶
Exact solves LP with Simplex method using exact (rational) arithmetic. argument parm may by nil (means that default values will be used). See also NewSmcp(). Returns nil if problem have been solved (not necessarly finding optimal solution) otherwise returns an error which is an instanse of OptError.
func (*Prob) LoadMatrix ¶
LoadMatrix replaces all of the constraint matrix. It sets
matrix[ia[i], ja[i]] = ar[i]
for i = 1..len(ia). ia[0], ja[0], and ar[0] are ignored. It requiers len(ia)=len(ja)=len(ar).
func (*Prob) MatCol ¶
MatCol returns nonzero elements of j-th column. ind[1]..ind[n] are row numbers of the nonzero elements of the column, val[1]..val[n] are their values, and n is the number of nonzero elements in the column.
func (*Prob) MatRow ¶
MatRow returns nonzero elements of i-th row. ind[1]..ind[n] are column numbers of the nonzero elements of the row, val[1]..val[n] are their values, and n is the number of nonzero elements in the row.
func (*Prob) ReadLP ¶
ReadLP reads the problem instance from a file in CPLEX LP file format. The params argument can be nil (could also be a value returned by NewCPXCP() but it is reserved for future use and at this point GLPK does allow to specify any CPLEX LP parameters).
func (*Prob) ReadMPS ¶
ReadMPS reads the problem instance from a file in MPS file format. The format argument specifies either the fixed or free MPS format. The params argument can be nil (could also be a value returned by NewMPSCP() but at this point GLPK package does not allow to specify any MPS parameters available in GLPK).
Note that MPS format does not specify objective function direction (minimization or maximization). GLPK assumes minimization, use SetObjDir(glpk.MAX) to switch to maximization if needed.
func (*Prob) ReadProb ¶
func (p *Prob) ReadProb(flags ProbRWFlags, filename string) error
ReadProb reads the problem instance from a file in GLPK LP/MIP file format. The flags argument is reserved for future use, for now zero value should be used.
func (*Prob) RowLB ¶
RowLB returns the lower bound of i-th row, i.e. the lower bound of the corresponding auxiliary variable. If the row has no lower bound -math.MaxFloat64 is returned.
func (*Prob) RowType ¶
RowType returns the type of i-th row, i.e. the type of the corresponding auxiliary variable.
func (*Prob) RowUB ¶
RowUB returns the upper bound of i-th row, i.e. the upper bound of the corresponding auxiliary variable. If the row has no upper bound +math.MaxFloat64 is returned.
func (*Prob) SetColBnds ¶
SetColBnds sets column bounds
func (*Prob) SetColKind ¶
SetColKind sets the kind of j-th column as specified by the VarType parameter kind.
func (*Prob) SetColName ¶
SetColName sets j-th column (variable) name.
func (*Prob) SetColStat ¶
SetColStat sets the current status of j-th column (structural variable) as specified by the stat argument.
func (*Prob) SetMatCol ¶
SetMatCol sets (replaces) j-th column. It sets
matrix[ind[i], j] = val[i]
for i=1..len(ind). ind[0] and val[0] are ignored. Requires len(ind) = len(val).
func (*Prob) SetMatRow ¶
SetMatRow sets (replaces) i-th row. It sets
matrix[i, ind[j]] = val[j]
for j=1..len(ind). ind[0] and val[0] are ignored. Requires len(ind) = len(val).
func (*Prob) SetObjCoef ¶
SetObjCoef sets objective function coefficient of j-th column.
func (*Prob) SetObjDir ¶
SetObjDir sets optimization direction (either glpk.MAX for maximization or glpk.MIN for minimization)
func (*Prob) SetObjName ¶
SetObjName sets (changes) objective function name.
func (*Prob) SetProbName ¶
SetProbName sets (changes) the problem name.
func (*Prob) SetRowBnds ¶
SetRowBnds sets row bounds
func (*Prob) SetRowName ¶
SetRowName sets i-th row (constraint) name.
func (*Prob) SetRowStat ¶
SetRowStat sets the current status of i-th row (auxiliary variable) as specified by the stat argument.
func (*Prob) Simplex ¶
Simplex solves LP with Simplex method. The argument parm may by nil (means that default values will be used). See also NewSmcp(). Returns nil if problem have been solved (not necessarly finding optimal solution) otherwise returns an error which is an instanse of OptError.
func (*Prob) WriteLP ¶
WriteLP writes the problem instance into a file in CPLEX LP file format. The params argument can be nil (could also be a value returned by NewCPXCP() but it is reserved for future use and at this point GLPK does allow to specify any CPLEX LP parameters).
func (*Prob) WriteMPS ¶
WriteMPS writes the problem instance into a file in MPS file format. The format argument specifies either the fixed or free MPS format. The params argument can be nil (could also be a value returned by NewMPSCP() but at this point GLPK package does not allow to specify any MPS parameters available in GLPK).
Note that MPS format does not specify objective function direction (minimization or maximization).
type ProbRWFlags ¶
type ProbRWFlags int
ProbRWFlags represents flags used for reading and writing of the problem instance in the GLPK LP/MIP format. Reserved for future use for now zero value should be used.
type Smcp ¶
type Smcp struct {
// contains filtered or unexported fields
}
Smcp represents simplex solver control parameters, a set of parameters for Prob.Simplex() and Prob.Exact(). Please use NewSmcp() to create Smtp structure which is properly initialized.
func NewSmcp ¶
func NewSmcp() *Smcp
NewSmcp creates new Smcp struct (a set of simplex solver control parameters) to be given as argument of Prob.Simplex() or Prob.Exact().
func (*Smcp) SetMsgLev ¶
SetMsgLev sets message level displayed by the optimization function (default: glpk.MSG_ALL).
func (*Smcp) SetPricing ¶
SetPricing sets pricing technique (default: glpk.PT_PSE).