lp

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package lp provides foundational types and utilities for defining and manipulating linear programming (LP) models.

It defines core abstractions such as variables, expressions, constraints, objective functions, and problem categories (e.g., continuous, integer).

The package supports constructing LP problems programmatically using types like LinearProgram, LpVariable, LpTerm, and LpExpression.

Example usage:

vars := []lp.LpVariable{
    lp.NewVariable("x1"),
    lp.NewVariable("x2"),
}
prog := lp.NewLinearProgram("Example LP", vars)

Index

Constants

View Source
const (
	LpMinimise = LpSense(-1)
	LpMaximise = LpSense(1)
)

Variables

View Source
var LpStatusMap = map[LpStatus]string{
	LpStatusNotSolved:      "Not Solved",
	LpStatusOptimal:        "Optimal",
	LpStatusInfeasible:     "Infeasible",
	LpStatusUnbounded:      "Unbounded",
	LpStatusUndefined:      "Undefined",
	LpStatusNotImplemented: "Not Implemented",
}

LpStatusMap maps LpStatus values to their string representations.

Functions

This section is empty.

Types

type LinearProgram

type LinearProgram struct {
	// Solution
	Solution float64 // z

	// Matrix Representation
	Variables     *mat.VecDense // x
	ObjectiveFunc *mat.VecDense // c
	Constraints   *mat.Dense    // A
	RHS           *mat.VecDense // b

	// Simplex Internal Variables
	Indices *mat.VecDense // Indices of basic variables

	// Others
	Description      string
	VariablesMap     []LpVariable
	Status           LpStatus
	Sense            LpSense
	ConstraintVector []LpConstraintType
	// contains filtered or unexported fields
}

LinearProgram represents a linear programming problem in standard form.

func NewLinearProgram

func NewLinearProgram(desc string, vars []LpVariable) LinearProgram

NewLinearProgram Create a new Linear Program

func (*LinearProgram) AddConstraint

func (prog *LinearProgram) AddConstraint(constraint LpExpression, constraintType LpConstraintType, rightHandSide float64)

AddConstraint Add a constraint to the linear program

func (*LinearProgram) AddIPConstraints

func (prog *LinearProgram) AddIPConstraints()

AddIPConstraints adds integer programming constraints to the linear program.

func (*LinearProgram) AddObjective

func (prog *LinearProgram) AddObjective(sense LpSense, objective LpExpression)

AddObjective Add an objective to the linear program

func (*LinearProgram) PrintSolution

func (lp *LinearProgram) PrintSolution()

PrintSolution prints the solution of the linear program in a human-readable format.

func (*LinearProgram) String

func (lp *LinearProgram) String() string

String returns a string representation of the LinearProgram String returns a string representation of the LinearProgram.

type LpCategory

type LpCategory int

LpCategory represents the category of a linear programming variable, such as continuous, integer, or binary.

const (
	LpCategoryContinuous LpCategory = iota
	LpCategoryInteger
	LpCategoryBinary
)

type LpConstraintType

type LpConstraintType int

LpConstraintType represents the type of a constraint in a linear programming problem.

const (
	LpConstraintLE LpConstraintType = -1
	LpConstraintEQ LpConstraintType = 0
	LpConstraintGE LpConstraintType = 1
)

type LpExpression

type LpExpression struct {
	Terms []LpTerm
}

LpExpression represents the LHS of a linear expression

func NewExpression

func NewExpression(terms []LpTerm) LpExpression

NewExpression creates a new LpExpression with the given terms

type LpSense

type LpSense int

LpSense represents the sense of the linear programming problem, either minimization or maximization.

type LpStatus

type LpStatus int

LpStatus represents the current status of solving the linear programming problem.

const (
	LpStatusNotSolved LpStatus = iota
	LpStatusOptimal
	LpStatusInfeasible
	LpStatusUnbounded
	LpStatusUndefined
	LpStatusNotImplemented
)

func (LpStatus) String

func (s LpStatus) String() string

String returns the string representation of the LpStatus.

type LpTerm

type LpTerm struct {
	Coefficient float64
	Variable    LpVariable // These get added to the variable list in the LinearProgram??
}

LpTerm represents a term in a linear expression, consisting of a coefficient and a variable.

func NewTerm

func NewTerm(coefficient float64, variable LpVariable) LpTerm

NewTerm creates a new LpTerm with the given coefficient and variable.

type LpVariable

type LpVariable struct {
	Name         string
	Value        float64
	IsSlack      bool
	IsArtificial bool
	Category     LpCategory
}

LpVariable represents a variable in a linear programming problem.

func NewVariable

func NewVariable(name string, category ...LpCategory) LpVariable

NewVariable creates a new LpVariable with the given name.

Jump to

Keyboard shortcuts

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