Skip to content

Introducing a Few Important Upgrades for the Optimization Problem #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ go 1.21

require gonum.org/v1/gonum v0.14.0

require github.com/MatProGo-dev/SymbolicMath.go v0.1.4
require github.com/MatProGo-dev/SymbolicMath.go v0.1.8
14 changes: 2 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
github.com/MatProGo-dev/SymbolicMath.go v0.0.0-20240104201035-f9a42f642121 h1:nnJVXcnTvAdkfR4kZRBw4Ot+KoL8S3PijlLTmKOooco=
github.com/MatProGo-dev/SymbolicMath.go v0.0.0-20240104201035-f9a42f642121/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
github.com/MatProGo-dev/SymbolicMath.go v0.1.0 h1:FUwLQZzZhtgGj6WKyuwQE74P9UYhgbNr5eD5f8zzuu8=
github.com/MatProGo-dev/SymbolicMath.go v0.1.0/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
github.com/MatProGo-dev/SymbolicMath.go v0.1.1 h1:YigpL7w5D8qLu0xzDAayMXePBh9LK0/w3ykvuoVZwXQ=
github.com/MatProGo-dev/SymbolicMath.go v0.1.1/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
github.com/MatProGo-dev/SymbolicMath.go v0.1.2 h1:9tYbiHWm0doXOSipd02pxtENqBU8WhmHTrDXetPW+ms=
github.com/MatProGo-dev/SymbolicMath.go v0.1.2/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
github.com/MatProGo-dev/SymbolicMath.go v0.1.3 h1:IeofFqvZ/jAO6LywlZR/UMl5t/KOyMAvvctVgTzvgHI=
github.com/MatProGo-dev/SymbolicMath.go v0.1.3/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
github.com/MatProGo-dev/SymbolicMath.go v0.1.4 h1:6DaDRYoANmKMkZL0uSUSx7Ibx8Hc9S6AX+7L0hIy1A4=
github.com/MatProGo-dev/SymbolicMath.go v0.1.4/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
github.com/MatProGo-dev/SymbolicMath.go v0.1.8 h1:lpe+6cK/2fg29WwxOykm4hKvfJeqvFUBGturC9qh5ug=
github.com/MatProGo-dev/SymbolicMath.go v0.1.8/go.mod h1:gKbGR/6sYWi2koMUEDIPWBPi6jQPELKle0ijIM+eaHU=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
gonum.org/v1/gonum v0.14.0 h1:2NiG67LD1tEH0D7kM+ps2V+fXmsAnpUeec7n8tcr4S0=
Expand Down
7 changes: 7 additions & 0 deletions mpiErrors/no_objective_defined.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mpiErrors

type NoObjectiveDefinedError struct{}

func (e NoObjectiveDefinedError) Error() string {
return "No objective defined for the optimization problem; please define one with SetObjective()"
}
10 changes: 10 additions & 0 deletions problem/objective.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ type Objective struct {
func NewObjective(e symbolic.Expression, sense ObjSense) *Objective {
return &Objective{e, sense}
}

/*
IsLinear
Description:

This method returns true if the objective is linear, false otherwise.
*/
func (o *Objective) IsLinear() bool {
return symbolic.IsLinear(o.Expression)
}
72 changes: 72 additions & 0 deletions problem/optimization_problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"fmt"

"github.com/MatProGo-dev/MatProInterface.go/mpiErrors"
"github.com/MatProGo-dev/MatProInterface.go/optim"
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
)
Expand Down Expand Up @@ -268,3 +270,73 @@
return newOptimProblem, nil

}

/*
Check
Description:

Checks that the OptimizationProblem is valid.
*/
func (op *OptimizationProblem) Check() error {
// Check Objective
if op.Objective == (Objective{}) {
return mpiErrors.NoObjectiveDefinedError{}
}

err := op.Objective.Check()
if err != nil {
return fmt.Errorf("the objective is not valid: %v", err)
}

// Check Variables
for _, variable := range op.Variables {
err = variable.Check()
if err != nil {
return fmt.Errorf("the variable is not valid: %v", err)
}
}

// Check Constraints
for _, constraint := range op.Constraints {
err = constraint.Check()
if err != nil {
return fmt.Errorf("the constraint is not valid: %v", err)
}
}

// All Checks Passed!
return nil
}

/*
IsLinear
Description:

Checks if the optimization problem is linear.
Per the definition of a linear optimization problem, the problem is linear if and only if:
1. The objective function is linear (i.e., a constant or an affine combination of variables).
2. All constraints are linear (i.e., an affine combination of variables in an inequality or equality).
*/
func (op *OptimizationProblem) IsLinear() bool {
// Input Processing
// Verify that the problem is well-formed
err := op.Check()
if err != nil {
panic(fmt.Errorf("the optimization problem is not well-formed: %v", err))

Check warning on line 325 in problem/optimization_problem.go

View check run for this annotation

Codecov / codecov/patch

problem/optimization_problem.go#L325

Added line #L325 was not covered by tests
}

// Check Objective
if !op.Objective.IsLinear() {
return false
}

// Check Constraints
for _, constraint := range op.Constraints {
if !constraint.IsLinear() {
return false
}
}

// All Checks Passed!
return true
}
136 changes: 134 additions & 2 deletions testing/optim/scalar_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ Description:
*/

import (
"testing"

"github.com/MatProGo-dev/MatProInterface.go/optim"
"gonum.org/v1/gonum/mat"
"testing"
)

func TestScalarConstraint_ScalarConstraint1(t *testing.T) {
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestScalarConstraint_IsLinear1(t *testing.T) {
}

/*
TestScalarConstraint_IsLinear1
TestScalarConstraint_IsLinear2
Description:

Detects whether a simple inequality between
Expand Down Expand Up @@ -99,6 +100,43 @@ func TestScalarConstraint_IsLinear2(t *testing.T) {

}

/*
TestScalarConstraint_IsLinear3
Description:

Detects whether a simple inequality between
a variable and a constant is a quadratic expression.
The function should identify that this is NOT a linear expression.
*/
func TestScalarConstraint_IsLinear3(t *testing.T) {
// Constants
m := optim.NewModel("scalar-constraint-test1")
v1 := m.AddVariable()
sqe2 := optim.ScalarQuadraticExpression{
L: optim.OnesVector(1),
Q: *mat.NewDense(1, 1, []float64{3.14}),
X: optim.VarVector{Elements: []optim.Variable{v1}},
}

k1 := optim.K(2.8)

// Algorithm
sc1 := optim.ScalarConstraint{
LeftHandSide: k1,
RightHandSide: sqe2,
Sense: optim.SenseEqual,
}

tf, err := sc1.IsLinear()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if tf {
t.Errorf("sc1 is not linear, but function claims it is!")
}

}

/*
TestScalarConstraint_Simplify1
Description:
Expand Down Expand Up @@ -138,3 +176,97 @@ func TestScalarConstraint_Simplify1(t *testing.T) {
t.Errorf("Remainder on LHS was not contained properly")
}
}

/*
TestScalarConstraint_Simplify2
Description:

Attempts to simplify the constraint between
a scalar linear epression and a variable.
The resulting constraint should have a zero remainder on the right hand side
and the left hand side should contain a variable vector with 1 more element than it started with.
*/
func TestScalarConstraint_Simplify2(t *testing.T) {
// Constants
m := optim.NewModel("scalar-constraint-test2")
vv1 := m.AddVariableVector(3)
sle2 := optim.ScalarLinearExpr{
L: optim.OnesVector(vv1.Len()),
X: vv1,
C: 2.0,
}
v3 := m.AddVariable()

// Create sles
sc1 := optim.ScalarConstraint{
LeftHandSide: sle2,
RightHandSide: v3,
Sense: optim.SenseEqual,
}

// Attempt to simplify
sc2, err := sc1.Simplify()
if err != nil {
t.Errorf("unexpected error during simplify(): %v", err)
}

if float64(sc2.RightHandSide.(optim.K)) != 0.0 {
t.Errorf("Remainder on LHS was not contained properly")
}

if sc2.LeftHandSide.(optim.ScalarLinearExpr).X.Len() != 4 {
t.Errorf("Variable vector did not increase in size")
}
}

/*
TestScalarConstraint_Simplify3
Description:

Attempts to simplify the constraint between
a scalar linear epression and a scalar quadratic expression.
*/
func TestScalarConstraint_Simplify3(t *testing.T) {
// Constants
m := optim.NewModel("scalar-constraint-test3")
vv1 := m.AddVariableVector(3)
sle2 := optim.ScalarLinearExpr{
L: optim.OnesVector(vv1.Len()),
X: vv1,
C: 2.0,
}
sqe3 := optim.ScalarQuadraticExpression{
L: optim.OnesVector(3),
Q: *mat.NewDense(
3, 3,
[]float64{
3.14, 0.0, 0.0,
0.0, 3.14, 0.0,
0.0, 0.0, 3.14,
},
),
X: vv1,
C: 1.5,
}

// Create sles
sc1 := optim.ScalarConstraint{
LeftHandSide: sle2,
RightHandSide: sqe3,
Sense: optim.SenseLessThanEqual,
}

// Attempt to simplify
sc2, err := sc1.Simplify()
if err != nil {
t.Errorf("unexpected error during simplify(): %v", err)
}

if float64(sc2.RightHandSide.(optim.K)) != sqe3.C {
t.Errorf(
"Remainder on RHS was not contained properly; expected %v, received %v",
sqe3.C,
sc2.RightHandSide.(optim.K),
)
}
}
Loading
Loading