Skip to content

Commit

Permalink
Merge pull request #3837 from koen-lee/eval-LinearExp
Browse files Browse the repository at this point in the history
Evaluate LinearExp for the found solution
  • Loading branch information
lperron authored Jun 28, 2023
2 parents c52fcb4 + aa894f0 commit c6074f8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ortools/linear_solver/csharp/LinearExpr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public virtual double DoVisit(Dictionary<Variable, double> coefficients, double
return 0;
}

public virtual double SolutionValue()
{
return 0;
}

public double Visit(Dictionary<Variable, double> coefficients)
{
return DoVisit(coefficients, 1.0);
Expand Down Expand Up @@ -185,6 +190,11 @@ public override double DoVisit(Dictionary<Variable, double> coefficients, double
}
}

public override double SolutionValue()
{
return expr_.SolutionValue() * coeff_;
}

private LinearExpr expr_;
private double coeff_;
}
Expand Down Expand Up @@ -213,6 +223,11 @@ public override double DoVisit(Dictionary<Variable, double> coefficients, double
return 0.0;
}
}

public override double SolutionValue()
{
return expr_.SolutionValue() + coeff_;
}

private LinearExpr expr_;
private double coeff_;
Expand Down Expand Up @@ -246,6 +261,11 @@ public override double DoVisit(Dictionary<Variable, double> coefficients, double
return 0.0;
}

public override double SolutionValue()
{
return var_.SolutionValue();
}

private Variable var_;
}

Expand Down Expand Up @@ -274,6 +294,11 @@ public override double DoVisit(Dictionary<Variable, double> coefficients, double
}
}

public override double SolutionValue()
{
return left_.SolutionValue() + right_.SolutionValue();
}

private LinearExpr left_;
private LinearExpr right_;
}
Expand Down Expand Up @@ -302,6 +327,16 @@ public override double DoVisit(Dictionary<Variable, double> coefficients, double
}
}

public override double SolutionValue()
{
double sum = 0.0;
foreach (LinearExpr expr in array_)
{
sum += expr.SolutionValue();
}
return sum;
}

private LinearExpr[] array_;
}

Expand Down Expand Up @@ -331,6 +366,16 @@ public override double DoVisit(Dictionary<Variable, double> coefficients, double
return 0.0;
}

public override double SolutionValue()
{
double sum = 0.0;
foreach (Variable var in array_)
{
sum += var.SolutionValue();
}
return sum;
}

private Variable[] array_;
}
} // namespace Google.OrTools.LinearSolver
23 changes: 23 additions & 0 deletions ortools/linear_solver/csharp/LinearSolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,28 @@ static void testSetHintAndSolverGetters()

solver.SetHint(new Variable[] { x, y }, new double[] { 2.0, 3.0 });
}

[Fact]
static void Given_a_LinearExpr_and_a_solution_When_SolutionValue_is_called_then_the_result_is_correct()
{
Console.WriteLine(nameof(Given_a_LinearExpr_and_a_solution_When_SolutionValue_is_called_then_the_result_is_correct));
Solver solver = Solver.CreateSolver("glop");
// x, y and z are fixed; we don't want to test the solver here.
Variable x = solver.MakeIntVar(3, 3, "x");
Variable y = solver.MakeIntVar(4, 4, "y");
Variable z = solver.MakeIntVar(5, 5, "z");

LinearExpr part1 = x * 2; // 6
LinearExpr part2 = y * 3 + z + 4; // 21
LinearExpr objective = part1 + part2; // 27
LinearExpr anew = new();
solver.Maximize(objective);
solver.Solve();
Assert.Equal(27, objective.SolutionValue(), precision: 9);
Assert.Equal(6, part1.SolutionValue(), precision: 9);
Assert.Equal(21, part2.SolutionValue(), precision: 9);
Assert.Equal(0, anew.SolutionValue(), precision: 9);
Assert.Equal(27, (objective + anew).SolutionValue(), precision: 9);
}
}
} // namespace Google.OrTools.Tests

0 comments on commit c6074f8

Please sign in to comment.