lpwrapper

package module
v0.0.0-...-099623a Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 2 Imported by: 0

README

lpwrapper

A simple linear programing library, built on top of golp and lpsolve

It allows easily creating models using math expressions, instead of operating the final matrix directly.

Example

Here is a simple example with two variables:

lp := lpwrapper.NewLP()

x := lp.AddVariable("x")
y := lp.AddVariable("y")

c1 := x.PlusTerm(y.Times(3)).LE(lpwrapper.NewConstExpression(7))
lp.AddConstraint(c1)

c2 := x.Times(2).Plus(y.ToTerm()).LE(lpwrapper.NewConstExpression(4))
lp.AddConstraint(c2)

obj := x.PlusVar(y)
lp.SetObjFn(obj.Terms, true)

result := lp.Solve()

fmt.Printf("Maximum value: %f\n", result)
fmt.Printf("(x, y): (%.1f, %.1f)\n", x.Value(), y.Value())
fmt.Println("")
fmt.Println("Full model:")
lp.PrintModel()

This produces:

Maximum value: 3.000000
(x, y): (1.0, 2.0)

Full model:
Objective function:  1.00*x + 1.00*y

Maximize:  true

Variables: 
x
y

Constraints: 
1.00*x + 3.00*y LE 7.00
2.00*x + 1.00*y LE 4.00

You can find more elaborate examples in the examples folder.

Getting started

  • This library requires lpsolve to be previously installed on your machine.
  • Add this to you environment variables:
export CGO_CFLAGS="-I/usr/include/lpsolve"
export CGO_LDFLAGS="-llpsolve55 -lm -ldl"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constraint

type Constraint struct {
	Expression    *Expression
	Constraint    golp.ConstraintType
	RightHandSide float64
}

func (*Constraint) String

func (e *Constraint) String() string

type Expression

type Expression struct {
	Terms     []*Term
	ConstTerm float64
}

Expression represents a linear expression, as a sum of terms.

func NewConstExpression

func NewConstExpression(c float64) *Expression

NewConstExpression creates a new expression with a constant term.

func (*Expression) EQ

func (e *Expression) EQ(e2 *Expression) *Constraint

EQ creates a new constraint from two expressions, with the first equal to the second.

func (*Expression) GE

func (e *Expression) GE(e2 *Expression) *Constraint

GE creates a new constraint from two expressions, with the first greater than or equal to the second.

func (*Expression) LE

func (e *Expression) LE(e2 *Expression) *Constraint

LE creates a new constraint from two expressions, with the first less than or equal to the second.

func (*Expression) MinusConst

func (e *Expression) MinusConst(c float64) *Expression

MinusConst subtracts a constant from the expression, modifying the same expression and returning itself.

func (*Expression) MinusTerm

func (e *Expression) MinusTerm(t *Term) *Expression

MinusTermadds a term with inverted sign to the expression, modifying the same expression and returning itself.

func (*Expression) MinusTerms

func (e *Expression) MinusTerms(terms []*Term) *Expression

MinusTerms adds multiple terms with inverted sign to the expression, modifying the same expression and returning itself.

func (*Expression) PlusConst

func (e *Expression) PlusConst(c float64) *Expression

PlusConst adds a constant to the expression, modifying the same expression and returning itself.

func (*Expression) PlusTerm

func (e *Expression) PlusTerm(t *Term) *Expression

PlusTerm adds a term to the expression, modifying the same expression and returning itself.

func (*Expression) PlusTerms

func (e *Expression) PlusTerms(terms []*Term) *Expression

PlusTerms adds multiple terms to the expression, modifying the same expression and returning itself.

func (*Expression) String

func (e *Expression) String() string

String returns a string representation of the expression.

type LP

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

LP is a linear programming model, that wraps the golp library, with some additional functionality to help with creating models.

func NewLP

func NewLP() *LP

NewLP creates a new LP model.

func (*LP) AddConstraint

func (lp *LP) AddConstraint(constraint *Constraint)

AddConstraintLegacy adds a new constraint to the model.

func (*LP) AddVariable

func (lp *LP) AddVariable(name string) *Variable

AddVariable adds a new variable to the model, with a name to help identify it.

func (*LP) PrintModel

func (lp *LP) PrintModel()

PrintModel prints the model to the console.

func (*LP) SetObjFn

func (lp *LP) SetObjFn(terms []*Term, maximize bool)

SetObjFn sets the objective function of the model.

func (*LP) SetVervoseLevel

func (lp *LP) SetVervoseLevel(level golp.VerboseLevel)

SetVervoseLevel sets the verbose level of the model. Defaults to golp.IMPORTANT

func (*LP) Solve

func (lp *LP) Solve() float64

Solve the model and return the objective function value.

type Term

type Term struct {
	Coefficient float64
	Variable    *Variable
}

Term represents a term in a linear expression, as constant * variable.

func (*Term) Negative

func (t *Term) Negative() *Term

Negative returns the negative of the term.

func (*Term) Plus

func (t *Term) Plus(t2 *Term) *Expression

Plus adds two terms together, creating a new expression.

func (*Term) PlusConst

func (t *Term) PlusConst(c float64) *Expression

PlusConst adds a constant to the term, creating a new expression.

func (*Term) String

func (t *Term) String() string

String returns a string representation of the term.

func (*Term) ToExpression

func (t *Term) ToExpression() *Expression

ToExpression converts the term to an expression with only one term.

type Variable

type Variable struct {
	Name string
	// contains filtered or unexported fields
}

func (*Variable) MinusTerm

func (v *Variable) MinusTerm(t *Term) *Expression

MinusTerm subtracts a term from the variable, creating an expression.

func (*Variable) MinusVar

func (v *Variable) MinusVar(v2 *Variable) *Expression

MinusVar subtracts a variable from the variable, creating an expression.

func (*Variable) PlusConst

func (v *Variable) PlusConst(c float64) *Expression

PlusConst adds a constant to the variable, creating an expression.

func (*Variable) PlusTerm

func (v *Variable) PlusTerm(t *Term) *Expression

PlusTerm adds a term to the variable, creating an expression.

func (*Variable) PlusVar

func (v *Variable) PlusVar(v2 *Variable) *Expression

PlusVar adds a variable to the variable, creating an expression of type 1*v1 + 1*v2.

func (*Variable) SetMaxBound

func (v *Variable) SetMaxBound(bound float64)

SetMaxBound sets the maximum bound for the variable. By default it is +infinity.

func (*Variable) SetMinBound

func (v *Variable) SetMinBound(bound float64)

SetMinBound sets the minimum bound for the variable. By default it is 0.

func (*Variable) SetUnbounded

func (v *Variable) SetUnbounded()

SetUnbounded sets the variable to be unbounded (-inf, +inf).

func (*Variable) String

func (v *Variable) String() string

String returns the name of the variable.

func (*Variable) Times

func (v *Variable) Times(c float64) *Term

Times multiplies the variable by a constant, creating a term.

func (*Variable) ToExpression

func (v *Variable) ToExpression() *Expression

ToExpression converts the variable to an expression with only one term.

func (*Variable) ToTerm

func (v *Variable) ToTerm() *Term

ToTerm converts the variable to a term with a coefficient of 1.

func (*Variable) Value

func (v *Variable) Value() float64

Value returns the value of the variable after the solution.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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