Skip to content

Commit a331c4c

Browse files
Feature: Problem as symbolic.Environment interface (#29)
* forced upgrade to main of SymbolicMath * Upgraded methods for Adding variables + added methods for turning an OptimizationProblem pointer into an Environment * One more pivot to using the new "Environment" interface features for OP
1 parent eccb7e9 commit a331c4c

3 files changed

Lines changed: 64 additions & 25 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ toolchain go1.23.9
66

77
require gonum.org/v1/gonum v0.16.0
88

9-
require github.com/MatProGo-dev/SymbolicMath.go v0.3.1
9+
require github.com/MatProGo-dev/SymbolicMath.go v0.3.2-0.20251017032605-a7dee10c22bf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/MatProGo-dev/SymbolicMath.go v0.3.1 h1:WM5lVAySD4mQDvq3RABUmJgSGkSYHT0CC+ZJXeTTJRg=
2-
github.com/MatProGo-dev/SymbolicMath.go v0.3.1/go.mod h1:tW8thj4pkaTV9lFNU3OCKmwQ3mZ2Eim6S4JpHRDfRvU=
1+
github.com/MatProGo-dev/SymbolicMath.go v0.3.2-0.20251017032605-a7dee10c22bf h1:fvHlqO39XHb/TfMm979zsnrcvZDSAFU7PW61g1UQCW8=
2+
github.com/MatProGo-dev/SymbolicMath.go v0.3.2-0.20251017032605-a7dee10c22bf/go.mod h1:tW8thj4pkaTV9lFNU3OCKmwQ3mZ2Eim6S4JpHRDfRvU=
33
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
44
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=

problem/optimization_problem.go

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ func (op *OptimizationProblem) AddRealVariable() symbolic.Variable {
5454
func (op *OptimizationProblem) AddVariableClassic(lower, upper float64, vtype symbolic.VarType) symbolic.Variable {
5555
id := uint64(len(op.Variables))
5656
newVar := symbolic.Variable{
57-
ID: id,
58-
Lower: lower,
59-
Upper: upper,
60-
Type: vtype,
61-
Name: fmt.Sprintf("x_%v", id),
57+
ID: id,
58+
Lower: lower,
59+
Upper: upper,
60+
Type: vtype,
61+
Name: fmt.Sprintf("x_%v", id),
62+
Environment: op,
6263
}
6364
op.Variables = append(op.Variables, newVar)
6465
return newVar
@@ -100,11 +101,12 @@ func (op *OptimizationProblem) AddVariableVectorClassic(
100101
vs := make([]symbolic.Variable, num)
101102
for i := range vs {
102103
vs[i] = symbolic.Variable{
103-
ID: stID + uint64(i),
104-
Lower: lower,
105-
Upper: upper,
106-
Type: vtype,
107-
Name: fmt.Sprintf("x_%v", stID+uint64(i)),
104+
ID: stID + uint64(i),
105+
Lower: lower,
106+
Upper: upper,
107+
Type: vtype,
108+
Name: fmt.Sprintf("x_%v", stID+uint64(i)),
109+
Environment: op,
108110
}
109111
}
110112

@@ -127,14 +129,7 @@ func (op *OptimizationProblem) AddVariableMatrix(
127129
// environment as well as upper and lower bounds.
128130

129131
// Create variables
130-
vmOut := symbolic.NewVariableMatrix(rows, cols)
131-
132-
// Add variables to the problem
133-
for i := 0; i < rows; i++ {
134-
for j := 0; j < cols; j++ {
135-
op.Variables = append(op.Variables, vmOut[i][j])
136-
}
137-
}
132+
vmOut := symbolic.NewVariableMatrix(rows, cols, op)
138133

139134
return vmOut
140135
}
@@ -235,10 +230,12 @@ func From(inputModel optim.Model) (*OptimizationProblem, error) {
235230
// problem object.
236231
for ii, variable := range inputModel.Variables {
237232
newOptimProblem.Variables = append(newOptimProblem.Variables, symbolic.Variable{
238-
ID: uint64(ii),
239-
Lower: variable.Lower,
240-
Upper: variable.Upper,
241-
Type: symbolic.VarType(variable.Vtype),
233+
ID: uint64(ii),
234+
Lower: variable.Lower,
235+
Upper: variable.Upper,
236+
Type: symbolic.VarType(variable.Vtype),
237+
Name: fmt.Sprintf("x_%v", ii),
238+
Environment: newOptimProblem,
242239
})
243240
}
244241

@@ -1042,3 +1039,45 @@ func (op *OptimizationProblem) String() string {
10421039

10431040
return objString + constraintsString
10441041
}
1042+
1043+
/*
1044+
GetName
1045+
Description:
1046+
1047+
Returns the name of the optimization problem.
1048+
(Necessary for implementing the symbolic.Environment interface).
1049+
*/
1050+
func (op *OptimizationProblem) GetName() string {
1051+
return op.Name
1052+
}
1053+
1054+
/*
1055+
TrackVariable
1056+
Description:
1057+
1058+
Adds the given variable to the optimization problem if it is not already present.
1059+
Returns true if the variable was added, false if it was already present.
1060+
*/
1061+
func (op *OptimizationProblem) TrackVariable(v symbolic.Variable) bool {
1062+
// Check if the variable is already present
1063+
for _, variable := range op.Variables {
1064+
if variable.ID == v.ID {
1065+
return false
1066+
}
1067+
}
1068+
1069+
// Add the variable to the problem
1070+
op.Variables = append(op.Variables, v)
1071+
return true
1072+
}
1073+
1074+
/*
1075+
AllTrackedVariables
1076+
Description:
1077+
1078+
Returns a slice of all variables that are tracked by the optimization problem.
1079+
(Necessary for implementing the symbolic.Environment interface).
1080+
*/
1081+
func (op *OptimizationProblem) AllTrackedVariables() []symbolic.Variable {
1082+
return op.Variables
1083+
}

0 commit comments

Comments
 (0)