Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions problem/optimization_problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,52 @@ func (problemIn *OptimizationProblem) ToLPStandardForm1() (*OptimizationProblem,
return problemInStandardForm, slackVariables, nil
}

/*
ToLPStandardForm2
Description:

Transforms the given linear program (represented in an OptimizationProblem object)
into a standard form (i.e., only linear equality constraints and a linear objective function).

max c^T * x
subject to
A * x = b
x >= 0

Where:
- A is a matrix of coefficients,
- b is a vector of constants, and
- c is the vector of coefficients for the objective function.
This method also returns the slack variables (i.e., the variables that
are added to the problem to convert the inequalities into equalities).
*/
func (problemIn *OptimizationProblem) ToLPStandardForm2() (*OptimizationProblem, []symbolic.Variable, error) {
// Input Processing
err := problemIn.Check()
if err != nil {
return nil, nil, fmt.Errorf("the optimization problem is not well-formed: %v", err)
}

// Use the existing method to convert to standard form 1
problemInStandardForm, slackVariables, err := problemIn.ToLPStandardForm1()
if err != nil {
return nil, nil, err
}

// Modify the objective function to be a maximization problem,
// if it is not already.
if problemInStandardForm.Objective.Sense == SenseMinimize {
Copy link

Copilot AI Aug 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The objective function transformation lacks documentation explaining why the expression is multiplied by -1. This is a critical mathematical transformation that converts min c^Tx to max (-c)^Tx while preserving optimal solutions, and should be documented with a comment.

Copilot uses AI. Check for mistakes.
newObjectiveExpression := problemInStandardForm.Objective.Expression.Multiply(-1.0)
err = problemInStandardForm.SetObjective(newObjectiveExpression, SenseMaximize)
if err != nil {
return nil, nil, fmt.Errorf("there was a problem setting the new objective function: %v", err)
}
}

// Return the new problem and the slack variables
return problemInStandardForm, slackVariables, nil
}

/*
WithAllPositiveVariableConstraintsRemoved
Description:
Expand Down
63 changes: 63 additions & 0 deletions testing/problem/optimization_problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2943,3 +2943,66 @@ func TestOptimizationProblem_SimplifyConstraints3(t *testing.T) {
L1, L2)
}
}

/*
TestOptimizationProblem_ToLPStandardForm2_1
Description:

Tests the ToLPStandardForm2 function with a simple problem
that contains:
- a linear objective,
- a MINIMIZATION sense
- 1 variable,
- and a single linear inequality constraint (SenseGreaterThanEqual).
The result should be a problem with 2 variables and 1 constraint.
The sense of the resulting problem should be MAXIMIZATION.
*/
func TestOptimizationProblem_ToLPStandardForm2_1(t *testing.T) {
// Constants
p1 := problem.NewProblem("TestOptimizationProblem_ToLPStandardForm2_1")
v1 := p1.AddVariable()
c1 := v1.GreaterEq(-1.0)

p1.Constraints = append(p1.Constraints, c1)

// Create good objective
p1.Objective = *problem.NewObjective(
v1,
problem.SenseMinimize,
)

// Algorithm
p2, _, err := p1.ToLPStandardForm2()
if err != nil {
t.Errorf("unexpected error: %v", err)
}

// Check that the number of variables is as expected.
expectedNumVariables := 0
expectedNumVariables += 2 * len(p1.Variables) // original variables (positive and negative halfs)
expectedNumVariables += len(p1.Constraints) // slack variables
if len(p2.Variables) != expectedNumVariables {
t.Errorf("expected the number of variables to be %v; received %v",
expectedNumVariables, len(p2.Variables))
}

// Check that the number of constraints is as expected.
if len(p2.Constraints) != 1 {
t.Errorf("expected the number of constraints to be %v; received %v",
1, len(p2.Constraints))
}

// Verify that all constraints are equality constraints
for _, c := range p2.Constraints {
if c.ConstrSense() != symbolic.SenseEqual {
t.Errorf("expected the constraint to be an equality constraint; received %v",
c.ConstrSense())
}
}

// Verify that the sense of the objective is MAXIMIZATION
if p2.Objective.Sense != problem.SenseMaximize {
t.Errorf("expected the sense of the objective to be %v; received %v",
problem.SenseMaximize, p2.Objective.Sense)
}
}
Loading