Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate LinearExp for the found solution #3837

Merged
merged 3 commits into from Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions ortools/linear_solver/csharp/LinearExpr.cs
Expand Up @@ -16,13 +16,18 @@
using System;
using System.Collections.Generic;

public class LinearExpr

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / make

'LinearExpr' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / make

'LinearExpr' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / make

'LinearExpr' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / make

'LinearExpr' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / macos (Xcode, Release, ALL_BUILD, RUN_TESTS, install)

'LinearExpr' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / macos (Xcode, Release, ALL_BUILD, RUN_TESTS, install)

'LinearExpr' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / linux_dotnet

'LinearExpr' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / linux_dotnet

'LinearExpr' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / cmake

'LinearExpr' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / cmake

'LinearExpr' defines operator == or operator != but does not override Object.GetHashCode()

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / macos (Unix Makefiles, Release, all, test, install)

'LinearExpr' defines operator == or operator != but does not override Object.Equals(object o)

Check warning on line 19 in ortools/linear_solver/csharp/LinearExpr.cs

View workflow job for this annotation

GitHub Actions / macos (Unix Makefiles, Release, all, test, install)

'LinearExpr' defines operator == or operator != but does not override Object.GetHashCode()
{
public virtual double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
{
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 SolutionValue()
{
return expr_.SolutionValue() * coeff_;
}

private LinearExpr expr_;
private double coeff_;
}
Expand Down Expand Up @@ -213,6 +223,11 @@
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 @@
return 0.0;
}

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

private Variable var_;
}

Expand Down Expand Up @@ -274,6 +294,11 @@
}
}

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 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 @@
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
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