@@ -54,11 +54,12 @@ func (op *OptimizationProblem) AddRealVariable() symbolic.Variable {
5454func (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