@@ -568,26 +568,6 @@ func (op *OptimizationProblem) LinearEqualityConstraintMatrices() (symbolic.KMat
568568 return COut2 , dOut2 , nil
569569}
570570
571- // func (op *OptimizationProblem) Simplify() OptimizationProblem {
572- // // Create a new optimization problem
573- // newProblem := NewProblem(op.Name + " (Simplified)")
574-
575- // // Add all variables to the new problem
576- // for _, variable := range op.Variables {
577- // newProblem.Variables = append(newProblem.Variables, variable)
578- // }
579-
580- // // Add all constraints to the new problem
581- // for _, constraint := range op.Constraints {
582- // newProblem.Constraints = append(newProblem.Constraints, constraint)
583- // }
584-
585- // // Set the objective of the new problem
586- // newProblem.Objective = op.Objective
587-
588- // return newProblem
589- // }
590-
591571/*
592572ToProblemWithAllPositiveVariables
593573Description:
@@ -602,6 +582,7 @@ Description:
602582func (op * OptimizationProblem ) ToProblemWithAllPositiveVariables () (* OptimizationProblem , error ) {
603583 // Setup
604584 newProblem := NewProblem (op .Name + " (All Positive Variables)" )
585+ epsMagic := 1e-8 // TODO(Kwesi): Make this a parameter OR a constant in the package.
605586
606587 // For each variable, let's create two new variables
607588 // and set the original variable to be the difference of the two
@@ -610,20 +591,35 @@ func (op *OptimizationProblem) ToProblemWithAllPositiveVariables() (*Optimizatio
610591 // Setup
611592 xII := op .Variables [ii ]
612593
594+ // Expression for the positive and negative parts
595+ var expressionForReplacement symbolic.Expression = symbolic .K (0.0 )
596+
613597 // Create the two new variables
614- newProblem .AddVariableClassic (0.0 , symbolic .Infinity .Constant (), symbolic .Continuous )
615- nVariables := len (newProblem .Variables )
616- newProblem .Variables [nVariables - 1 ].Name = xII .Name + " (+)"
617- variablePositivePart := newProblem .Variables [nVariables - 1 ]
598+ // - Positive Part
599+ positivePartExists := xII .Upper >= 0
600+ positivePartExists = positivePartExists && ! ConstraintIsRedundantGivenOthers (xII .LessEq (0.0 - epsMagic ), op .Constraints )
601+ if positivePartExists {
602+ newProblem .AddVariableClassic (0.0 , symbolic .Infinity .Constant (), symbolic .Continuous )
603+ nVariables := len (newProblem .Variables )
604+ newProblem .Variables [nVariables - 1 ].Name = xII .Name + " (+)"
605+ variablePositivePart := newProblem .Variables [nVariables - 1 ]
606+ expressionForReplacement = expressionForReplacement .Plus (variablePositivePart )
607+ }
618608
619- newProblem .AddVariableClassic (0.0 , symbolic .Infinity .Constant (), symbolic .Continuous )
620- nVariables = len (newProblem .Variables )
621- newProblem .Variables [nVariables - 1 ].Name = xII .Name + " (-)"
622- variableNegativePart := newProblem .Variables [nVariables - 1 ]
609+ // - Negative Part
610+ negativePartExists := xII .Lower < 0
611+ negativePartExists = negativePartExists && ! ConstraintIsRedundantGivenOthers (xII .GreaterEq (0.0 ), op .Constraints )
612+ if negativePartExists {
613+ newProblem .AddVariableClassic (0.0 , symbolic .Infinity .Constant (), symbolic .Continuous )
614+ nVariables := len (newProblem .Variables )
615+ newProblem .Variables [nVariables - 1 ].Name = xII .Name + " (-)"
616+ variableNegativePart := newProblem .Variables [nVariables - 1 ]
617+
618+ expressionForReplacement = expressionForReplacement .Minus (variableNegativePart )
619+ }
623620
624621 // Set the original variable to be the difference of the two new variables
625- mapFromOriginalVariablesToNewExpressions [xII ] =
626- variablePositivePart .Minus (variableNegativePart )
622+ mapFromOriginalVariablesToNewExpressions [xII ] = expressionForReplacement
627623 }
628624
629625 // Now, let's create the new constraints by replacing the variables in the
@@ -688,6 +684,8 @@ func (problemIn *OptimizationProblem) ToLPStandardForm1() (*OptimizationProblem,
688684 problemIn .Name + " (In Standard Form)" ,
689685 )
690686
687+ // Copy over each of the
688+
691689 // Add all variables to the new problem
692690 mapFromInToNewVariables := make (map [symbolic.Variable ]symbolic.Expression )
693691 for _ , varII := range problemWithAllPositiveVariables .Variables {
0 commit comments