glpk

package
v0.0.0-...-2e64496 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2018 License: GPL-3.0 Imports: 3 Imported by: 8

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

View Source
const (
	MAX = ObjDir(C.GLP_MAX) // maximization
	MIN = ObjDir(C.GLP_MIN) // minimization
)

Allowed values of type ObjDir (objective function direction).

View Source
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).

View Source
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).

View Source
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).

View Source
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).

View Source
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).

View Source
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).

View Source
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).

View Source
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).

View Source
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).

View Source
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 BndsType

type BndsType int

BndsType is used to specify bounds type of a variable.

type CPXCP

type CPXCP struct {
	// contains filtered or unexported fields
}

CPXCP represent CPLEX LP format control parameters

func NewCPXCP

func NewCPXCP() *CPXCP

NewCPXCP creates new initialized CPXCP struct (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) Presolve

func (p *Iocp) Presolve() bool

Presolve checks whether the optional MIP presolver is enabled.

func (*Iocp) SetMsgLev

func (p *Iocp) SetMsgLev(lev MsgLev)

SetMsgLev sets message level.

func (*Iocp) SetPresolve

func (p *Iocp) SetPresolve(on bool)

SetPresolve enables or disables the optional MIP presolver.

type MPSCP

type MPSCP struct {
	// contains filtered or unexported fields
}

MPSCP represent MPS format control parameters

func NewMPSCP

func NewMPSCP() *MPSCP

NewMPSCP creates new initialized MPSCP struct (MPS format control parameters)

type MPSFormat

type MPSFormat int

MPSFormat represents MPS file format: either fixed (ancient) or free (modern) format.

type Meth

type Meth int

Meth represents simplex method option.

type MsgLev

type MsgLev int

MsgLev represents message level.

type ObjDir

type ObjDir int

ObjDir is used to specify objective function direction (maximization or minimization).

type OptError

type OptError int

OptError represents optimization error.

func (OptError) Error

func (r OptError) Error() string

Error implements the error interface.

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.

func (*PathError) Error

func (e *PathError) Error() string

Error implements the error interface.

type Pricing

type Pricing int

Pricing represents pricing technique.

type Prob

type Prob struct {
	// contains filtered or unexported fields
}

Prob represens optimization problem. Use glpk.New() to create a new problem.

func New

func New() *Prob

New creates a new optimization problem.

func (*Prob) AddCols

func (p *Prob) AddCols(nrs int) int

AddCols adds columns (variables). Returns (1-based) index of the first of the added columns.

func (*Prob) AddRows

func (p *Prob) AddRows(nrs int) int

AddRows adds rows (constraints). Returns (1-based) index of the first of the added rows.

func (*Prob) ColKind

func (p *Prob) ColKind(j int) VarType

ColKind returns the kind of j-th column

func (*Prob) ColLB

func (p *Prob) ColLB(j int) float64

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) ColName

func (p *Prob) ColName(j int) string

ColName returns column (variable) name of j-th column.

func (*Prob) ColPrim

func (p *Prob) ColPrim(j int) float64

ColPrim returns primal value of the variable associated with j-th column.

func (*Prob) ColStat

func (p *Prob) ColStat(j int) VarStat

ColStat returns the current status of j-th column structural variable.

func (*Prob) ColType

func (p *Prob) ColType(j int) BndsType

ColType returns the type of j-th column, i.e. the type of the corresponding structural variable.

func (*Prob) ColUB

func (p *Prob) ColUB(j int) float64

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

func (p *Prob) Copy(names bool) *Prob

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) DualStat

func (p *Prob) DualStat() SolStat

DualStat returns status of the dual basic solution.

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

func (p *Prob) Exact(parm *Smcp) error

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) Intopt

func (p *Prob) Intopt(params *Iocp) error

Intopt solves MIP problem with the branch-and-cut method.

func (*Prob) LoadMatrix

func (p *Prob) LoadMatrix(ia, ja []int32, ar []float64)

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

func (p *Prob) MatCol(j int) (ind []int32, val []float64)

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

func (p *Prob) MatRow(i int) (ind []int32, val []float64)

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) MipColVal

func (p *Prob) MipColVal(i int) float64

MipColVal returns value of the j-th column for MIP solution.

func (*Prob) MipObjVal

func (p *Prob) MipObjVal() float64

MipObjVal returns value of the objective function for MIP solution.

func (*Prob) MipStatus

func (p *Prob) MipStatus() SolStat

MipStatus returns status of a MIP solution.

func (*Prob) NumCols

func (p *Prob) NumCols() int

NumCols returns number of columns.

func (*Prob) NumRows

func (p *Prob) NumRows() int

NumRows returns number of rows.

func (*Prob) ObjCoef

func (p *Prob) ObjCoef(j int) float64

ObjCoef returns objective function coefficient of j-th column.

func (*Prob) ObjDir

func (p *Prob) ObjDir() ObjDir

ObjDir returns optimization direction (either glpk.MAX or glpk.MIN).

func (*Prob) ObjName

func (p *Prob) ObjName() string

ObjName returns objective name.

func (*Prob) ObjVal

func (p *Prob) ObjVal() float64

ObjVal returns objective function value.

func (*Prob) PrimStat

func (p *Prob) PrimStat() SolStat

PrimStat returns status of the primal basic solution.

func (*Prob) ProbName

func (p *Prob) ProbName() string

ProbName returns problem name.

func (*Prob) ReadLP

func (p *Prob) ReadLP(params *CPXCP, filename string) error

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

func (p *Prob) ReadMPS(format MPSFormat, params *MPSCP, filename string) error

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

func (p *Prob) RowLB(i int) float64

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) RowName

func (p *Prob) RowName(i int) string

RowName returns row (constraint) name of i-th row.

func (*Prob) RowStat

func (p *Prob) RowStat(i int) VarStat

RowStat returns the current status of i-th row auxiliary variable.

func (*Prob) RowType

func (p *Prob) RowType(i int) BndsType

RowType returns the type of i-th row, i.e. the type of the corresponding auxiliary variable.

func (*Prob) RowUB

func (p *Prob) RowUB(i int) float64

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

func (p *Prob) SetColBnds(j int, typ BndsType, lb float64, ub float64)

SetColBnds sets column bounds

func (*Prob) SetColKind

func (p *Prob) SetColKind(j int, kind VarType)

SetColKind sets the kind of j-th column as specified by the VarType parameter kind.

func (*Prob) SetColName

func (p *Prob) SetColName(j int, name string)

SetColName sets j-th column (variable) name.

func (*Prob) SetColStat

func (p *Prob) SetColStat(j int, stat VarStat)

SetColStat sets the current status of j-th column (structural variable) as specified by the stat argument.

func (*Prob) SetMatCol

func (p *Prob) SetMatCol(j int, ind []int32, val []float64)

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

func (p *Prob) SetMatRow(i int, ind []int32, val []float64)

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

func (p *Prob) SetObjCoef(j int, coef float64)

SetObjCoef sets objective function coefficient of j-th column.

func (*Prob) SetObjDir

func (p *Prob) SetObjDir(dir ObjDir)

SetObjDir sets optimization direction (either glpk.MAX for maximization or glpk.MIN for minimization)

func (*Prob) SetObjName

func (p *Prob) SetObjName(name string)

SetObjName sets (changes) objective function name.

func (*Prob) SetProbName

func (p *Prob) SetProbName(name string)

SetProbName sets (changes) the problem name.

func (*Prob) SetRowBnds

func (p *Prob) SetRowBnds(i int, typ BndsType, lb float64, ub float64)

SetRowBnds sets row bounds

func (*Prob) SetRowName

func (p *Prob) SetRowName(i int, name string)

SetRowName sets i-th row (constraint) name.

func (*Prob) SetRowStat

func (p *Prob) SetRowStat(i int, stat VarStat)

SetRowStat sets the current status of i-th row (auxiliary variable) as specified by the stat argument.

func (*Prob) Simplex

func (p *Prob) Simplex(parm *Smcp) error

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) Status

func (p *Prob) Status() SolStat

Status returns status of the basic solution.

func (*Prob) WriteLP

func (p *Prob) WriteLP(params *CPXCP, filename string) error

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

func (p *Prob) WriteMPS(format MPSFormat, params *MPSCP, filename string) error

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).

func (*Prob) WriteProb

func (p *Prob) WriteProb(flags ProbRWFlags, filename string) error

WriteProb writes the problem instance into a file in GLPK LP/MIP file format. The flags argument is reserved for future use, for now zero value should be used.

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 RTest

type RTest int

RTest represents ratio test technique.

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) SetMeth

func (s *Smcp) SetMeth(meth Meth)

SetMeth sets simplex method option (default: glpk.PRIMAL).

func (*Smcp) SetMsgLev

func (s *Smcp) SetMsgLev(lev MsgLev)

SetMsgLev sets message level displayed by the optimization function (default: glpk.MSG_ALL).

func (*Smcp) SetPricing

func (s *Smcp) SetPricing(pricing Pricing)

SetPricing sets pricing technique (default: glpk.PT_PSE).

func (*Smcp) SetRTest

func (s *Smcp) SetRTest(rTest RTest)

SetRTest sets ratio test technique (default: glpk.RT_HAR)

type SolStat

type SolStat int

SolStat specifies solution status.

type VarStat

type VarStat int

VarStat represents status of auxiliary/structural variable.

type VarType

type VarType int

VarType is used to specify variable type (kind).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL