Skip to content

Commit 7725553

Browse files
committed
Upgraded methods for Adding variables + added methods for turning an OptimizationProblem pointer into an Environment
1 parent 48e48d7 commit 7725553

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

problem/optimization_problem.go

Lines changed: 55 additions & 18 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
}
@@ -1042,3 +1037,45 @@ func (op *OptimizationProblem) String() string {
10421037

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

0 commit comments

Comments
 (0)