Skip to content

Commit 1b7c77f

Browse files
committed
Added More Tests for OptimizationProblem.From which required standardizing a Check() method for all constraints and adding Check() methods for a few expressions in the optim package
1 parent 6ebb81b commit 1b7c77f

File tree

10 files changed

+298
-14
lines changed

10 files changed

+298
-14
lines changed

optim/constr_sense.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,21 @@ func (cs ConstrSense) ToSymbolic() symbolic.ConstrSense {
3232
}
3333
return '1'
3434
}
35+
36+
/*
37+
String
38+
Description:
39+
40+
Returns the string representation of the constraint sense.
41+
*/
42+
func (cs ConstrSense) String() string {
43+
switch cs {
44+
case SenseEqual:
45+
return "=="
46+
case SenseLessThanEqual:
47+
return "<="
48+
case SenseGreaterThanEqual:
49+
return ">="
50+
}
51+
return "UNRECOGNIZED ConstrSense"
52+
}

optim/scalar_constraint.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,17 @@ func (sc ScalarConstraint) Check() error {
130130
return fmt.Errorf("the constraint sense is not recognized.")
131131
}
132132

133-
// Check left and right hand sides?
133+
// Check left and right hand sides
134+
err := sc.LeftHandSide.Check()
135+
if err != nil {
136+
return fmt.Errorf("left hand side of the constraint is not valid: %v", err)
137+
}
138+
139+
// Check right hand side
140+
err = sc.RightHandSide.Check()
141+
if err != nil {
142+
return fmt.Errorf("right hand side of the constraint is not valid: %v", err)
143+
}
134144

135145
// Return
136146
return nil

optim/scalar_expression.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type ScalarExpression interface {
6363
//ToSymbolic Returns the symbolic version of the scalar expression
6464
// (i.e., the expression when declared using the symbolic math toolbox).
6565
ToSymbolic() (symbolic.Expression, error)
66+
67+
// Check, checks the expression for any errors
68+
Check() error
6669
}
6770

6871
// NewExpr returns a new expression with a single additive constant value, c,

optim/var_vector.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,6 @@ Description:
294294
input rhs as the right hand side if it is valid.
295295
*/
296296
func (vv VarVector) Eq(rightIn interface{}, errors ...error) (Constraint, error) {
297-
// Constants
298-
299-
// Algorithm
300297
return vv.Comparison(rightIn, SenseEqual, errors...)
301298

302299
}
@@ -373,7 +370,11 @@ func (vv VarVector) Comparison(rhs interface{}, sense ConstrSense, errors ...err
373370
)
374371

375372
default:
376-
return VectorConstraint{}, fmt.Errorf("The Eq() method for VarVector is not implemented yet for type %T!", rhs)
373+
return VectorConstraint{}, fmt.Errorf(
374+
"The VarVector.Comparison (%v) method is not implemented yet for type %T!",
375+
sense,
376+
rhs,
377+
)
377378
}
378379
}
379380

optim/vector_constant.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ KVector
2323
*/
2424
type KVector mat.VecDense // Inherit all methods from mat.VecDense
2525

26+
/*
27+
Check
28+
Description:
29+
30+
This method checks for errors in the KVector type.
31+
There should never be any.
32+
*/
33+
func (kv KVector) Check() error {
34+
return nil
35+
}
36+
2637
/*
2738
Len
2839

optim/vector_constant_transpose.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ KVectorTranspose
2323
*/
2424
type KVectorTranspose mat.VecDense // Inherit all methods from mat.VecDense
2525

26+
/*
27+
Check
28+
Description:
29+
30+
This method checks for errors in the KVectorTranspose type.
31+
There should never be any.
32+
*/
33+
func (kvt KVectorTranspose) Check() error {
34+
return nil
35+
}
36+
2637
/*
2738
Len
2839

optim/vector_constraint.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ func (vc VectorConstraint) Check() error {
6060
)
6161
}
6262

63+
// Check that the left and right hand sides are well-defined
64+
err := vc.LeftHandSide.Check()
65+
if err != nil {
66+
return fmt.Errorf("left hand side of the constraint is not valid: %v", err)
67+
}
68+
69+
err = vc.RightHandSide.Check()
70+
if err != nil {
71+
return fmt.Errorf("right hand side of the constraint is not valid: %v", err)
72+
}
73+
6374
// All Checks Passed!
6475
return nil
6576
}

optim/vector_expression.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ type VectorExpression interface {
7575
//ToSymbolic
7676
// Converts the expression to a symbolic expression (in SymbolicMath.go)
7777
ToSymbolic() (symbolic.Expression, error)
78+
79+
//Check
80+
// Checks the expression for any errors
81+
Check() error
7882
}
7983

8084
/*

problem/optimization_problem.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,10 @@ func ToSymbolicConstraint(inputConstraint optim.Constraint) (symbolic.Constraint
177177
}
178178

179179
// Convert LHS to symbolic expression
180-
lhs, err := inputConstraint.Left().ToSymbolic()
181-
if err != nil {
182-
return symbolic.ScalarConstraint{}, err
183-
}
180+
lhs, _ := inputConstraint.Left().ToSymbolic()
184181

185182
// Convert RHS to symbolic expression
186-
rhs, err := inputConstraint.Right().ToSymbolic()
187-
if err != nil {
188-
return symbolic.ScalarConstraint{}, err
189-
}
183+
rhs, _ := inputConstraint.Right().ToSymbolic()
190184

191185
// Get Sense
192186
sense := inputConstraint.ConstrSense().ToSymbolic()

0 commit comments

Comments
 (0)