Skip to content

Commit 31ca2c2

Browse files
committed
Added More Check() values to the ToSymbolic() methods and Tested More Of The OptimizationProblem object
1 parent 1548177 commit 31ca2c2

8 files changed

+375
-73
lines changed

Diff for: optim/scalar_linear_expr.go

+6
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ Description:
431431
symbolic math toolbox).
432432
*/
433433
func (sle ScalarLinearExpr) ToSymbolic() (symbolic.Expression, error) {
434+
// Check for errors
435+
err := sle.Check()
436+
if err != nil {
437+
return nil, err
438+
}
439+
434440
// Compute product of L and X
435441
symX, err := sle.X.ToSymbolic()
436442
if err != nil {

Diff for: optim/scalar_quadratic_expression.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,11 @@ Description:
480480
(i.e., one that uses the symbolic math toolbox).
481481
*/
482482
func (qe ScalarQuadraticExpression) ToSymbolic() (symbolic.Expression, error) {
483-
// Constants
484-
var err error
483+
// Input Checking
484+
err := qe.Check()
485+
if err != nil {
486+
return nil, err
487+
}
485488

486489
// Convert Q, L and C to symbolic
487490
symQ := symbolic.DenseToKMatrix(qe.Q)

Diff for: optim/var_vector.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,11 @@ Description:
442442
(i.e., one that uses the symbolic math toolbox).
443443
*/
444444
func (vv VarVector) ToSymbolic() (symbolic.Expression, error) {
445-
// Constants
445+
// Input Checking
446+
err := vv.Check()
447+
if err != nil {
448+
return nil, err
449+
}
446450

447451
// Algorithm
448452
// Create the symbolic vector

Diff for: optim/var_vector_transpose.go

+28
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,28 @@ func (vvt VarVectorTranspose) Dims() []int {
469469
return []int{1, vvt.Len()}
470470
}
471471

472+
/*
473+
Check
474+
Description:
475+
476+
Checks whether or not the VarVector has a sensible initialization.
477+
*/
478+
func (vvt VarVectorTranspose) Check() error {
479+
// Check that each variable is properly defined
480+
for ii, element := range vvt.Elements {
481+
err := element.Check()
482+
if err != nil {
483+
return fmt.Errorf(
484+
"element %v has an issue: %v",
485+
ii, err,
486+
)
487+
}
488+
}
489+
490+
// If nothing was thrown, then return nil!
491+
return nil
492+
}
493+
472494
/*
473495
ToSymbolic
474496
Description:
@@ -477,6 +499,12 @@ Description:
477499
(i.e., an expression made using SymbolicMath.go).
478500
*/
479501
func (vvt VarVectorTranspose) ToSymbolic() (symbolic.Expression, error) {
502+
// Input Processing
503+
err := vvt.Check()
504+
if err != nil {
505+
return nil, err
506+
}
507+
480508
// Constants
481509
vm := symbolic.VariableMatrix{}
482510

Diff for: optim/vars.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ Description:
421421
(from the symbolic math toolbox).
422422
*/
423423
func (v Variable) ToSymbolic() (symbolic.Expression, error) {
424-
// Constants
424+
// Input Checking
425+
err := v.Check()
426+
if err != nil {
427+
return nil, err
428+
}
425429

426430
// Create base variable and fill its elements
427431
symVar := symbolic.Variable{

Diff for: problem/objective_sense.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ObjSense int
1010
const (
1111
SenseMinimize ObjSense = 1
1212
SenseMaximize = -1
13+
SenseFind ObjSense = 0
1314
)
1415

1516
/*

Diff for: problem/optimization_problem.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type OptimizationProblem struct {
1414
Name string
1515
Variables []symbolic.Variable
1616
Constraints []symbolic.Constraint
17-
Objective symbolic.Expression
17+
Objective Objective
1818
}
1919

2020
// NewProblem returns a new model with some default arguments such as not to show
@@ -139,21 +139,6 @@ func (op *OptimizationProblem) AddBinaryVariableMatrix(rows, cols int) [][]symbo
139139
return op.AddVariableMatrix(rows, cols, 0, 1, symbolic.Binary)
140140
}
141141

142-
// AddConstr adds the given constraint to the model.
143-
func (op *OptimizationProblem) AddConstraint(constr symbolic.Constraint) error {
144-
// Constants
145-
146-
// Input Processing
147-
//err := constr.Check()
148-
//if err != nil {
149-
// return err
150-
//}
151-
152-
// Algorithm
153-
op.Constraints = append(op.Constraints, constr)
154-
return nil
155-
}
156-
157142
/*
158143
SetObjective
159144
Description:
@@ -172,7 +157,7 @@ func (op *OptimizationProblem) SetObjective(e symbolic.Expression, sense ObjSens
172157
}
173158

174159
// Return
175-
op.Objective = NewObjective(se, sense)
160+
op.Objective = *NewObjective(se, sense)
176161
return nil
177162
}
178163

@@ -272,7 +257,15 @@ func From(inputModel optim.Model) (*OptimizationProblem, error) {
272257
}
273258

274259
// Convert Objective
275-
newOptimProblem.Objective, err = inputModel.Obj.ToSymbolic()
260+
objectiveExpr, err := inputModel.Obj.ToSymbolic()
261+
if err != nil {
262+
return nil, err
263+
}
264+
265+
err = newOptimProblem.SetObjective(
266+
objectiveExpr,
267+
ObjSense(inputModel.Obj.Sense),
268+
)
276269
if err != nil {
277270
return nil, err
278271
}

0 commit comments

Comments
 (0)