cmake: Format samples

This commit is contained in:
Corentin Le Molgat
2025-06-16 11:47:36 +02:00
parent 863ba8a91d
commit 91eae80e1c
5 changed files with 106 additions and 90 deletions

View File

@@ -16,22 +16,26 @@ using Xunit;
using Google.OrTools.ConstraintSolver;
namespace Google.OrTools.Tests {
public class ConstraintSolverTest {
namespace Google.OrTools.Tests
{
public class ConstraintSolverTest
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SolverTest(bool callGC) {
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 7, "x");
public void SolverTest(bool callGC)
{
Solver solver = new Solver("Solver");
IntVar x = solver.MakeIntVar(3, 7, "x");
if (callGC) {
GC.Collect();
}
if (callGC)
{
GC.Collect();
}
Assert.Equal(3, x.Min());
Assert.Equal(7, x.Max());
Assert.Equal("x(3..7)", x.ToString());
Assert.Equal(3, x.Min());
Assert.Equal(7, x.Max());
Assert.Equal("x(3..7)", x.ToString());
}
}
}
} // namespace Google.Sample.Tests

View File

@@ -16,19 +16,21 @@ using Xunit;
using Google.OrTools.LinearSolver;
namespace Google.OrTools.Tests {
public class LinearSolverTest {
namespace Google.OrTools.Tests
{
public class LinearSolverTest
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SolverTest(bool callGC) {
Solver solver = new Solver(
"Solver",
Solver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING);
public void SolverTest(bool callGC)
{
Solver solver = new Solver("Solver", Solver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING);
if (callGC) {
GC.Collect();
}
if (callGC)
{
GC.Collect();
}
}
}
}
} // namespace Google.Sample.Tests

View File

@@ -16,38 +16,41 @@ using Xunit;
using Google.OrTools.ConstraintSolver;
namespace Google.OrTools.Tests {
public class RoutingSolverTest {
namespace Google.OrTools.Tests
{
public class RoutingSolverTest
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SolverTest(bool callGC) {
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(
5/*locations*/, 1/*vehicle*/, 0/*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) => {
// Convert from routing variable Index to distance matrix NodeIndex.
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return Math.Abs(toNode - fromNode);
});
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
if (callGC) {
GC.Collect();
}
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy =
FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+4)-> 0 := +8
Assert.Equal(8, solution.ObjectiveValue());
public void SolverTest(bool callGC)
{
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(5 /*locations*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create a distance callback.
int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) =>
{
// Convert from routing variable Index to
// distance matrix NodeIndex.
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return Math.Abs(toNode - fromNode);
});
// Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
if (callGC)
{
GC.Collect();
}
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy = FirstSolutionStrategy.Types.Value.PathCheapestArc;
Assignment solution = routing.SolveWithParameters(searchParameters);
// 0 --(+1)-> 1 --(+1)-> 2 --(+1)-> 3 --(+1)-> 4 --(+4)-> 0 := +8
Assert.Equal(8, solution.ObjectiveValue());
}
}
}
} // namespace Google.Sample.Tests

View File

@@ -16,26 +16,30 @@ using Xunit;
using Google.OrTools.Sat;
namespace Google.OrTools.Tests {
public class SatSolverTest {
namespace Google.OrTools.Tests
{
public class SatSolverTest
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void SolverTest(bool callGC) {
CpModel model = new CpModel();
public void SolverTest(bool callGC)
{
CpModel model = new CpModel();
int num_vals = 3;
IntVar x = model.NewIntVar(0, num_vals - 1, "x");
IntVar y = model.NewIntVar(0, num_vals - 1, "y");
IntVar z = model.NewIntVar(0, num_vals - 1, "z");
int num_vals = 3;
IntVar x = model.NewIntVar(0, num_vals - 1, "x");
IntVar y = model.NewIntVar(0, num_vals - 1, "y");
IntVar z = model.NewIntVar(0, num_vals - 1, "z");
model.Add(x != y);
model.Add(x != y);
CpSolver solver = new CpSolver();
if (callGC) {
GC.Collect();
}
CpSolverStatus status = solver.Solve(model);
CpSolver solver = new CpSolver();
if (callGC)
{
GC.Collect();
}
CpSolverStatus status = solver.Solve(model);
}
}
}
} // namespace Google.Sample.Tests

View File

@@ -14,12 +14,15 @@
"""Sample to test or-tools installation."""
import ortools
# from ortools.algorithms import knapsack_solver
from ortools.constraint_solver import pywrapcp
# from ortools.graph.python import linear_sum_assignment
# from ortools.graph.python import max_flow
# from ortools.graph.python import min_cost_flow
from ortools.linear_solver import pywraplp
# from ortools.linear_solver import linear_solver_pb2
# from ortools.sat.python import cp_model_helper
# from ortools.sat.python import cp_model
@@ -28,34 +31,34 @@ from ortools.linear_solver import pywraplp
def lpsolver_test():
"""Test pywraplp."""
print('Test lpsolver...')
lpsolver = pywraplp.Solver('LinearTest',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
lpsolver.Solve()
print('Test lpsolver...DONE')
"""Test pywraplp."""
print("Test lpsolver...")
lpsolver = pywraplp.Solver("LinearTest", pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
lpsolver.Solve()
print("Test lpsolver...DONE")
def cpsolver_test():
"""Test pywrapcp."""
print('Test cpsolver...')
cpsolver = pywrapcp.Solver('ConstraintTest')
num_vals = 3
x = cpsolver.IntVar(0, num_vals - 1, 'x')
y = cpsolver.IntVar(0, num_vals - 1, 'y')
z = cpsolver.IntVar(0, num_vals - 1, 'z')
cpsolver.Add(x != y)
db = cpsolver.Phase([x, y, z], cpsolver.CHOOSE_FIRST_UNBOUND,
cpsolver.ASSIGN_MIN_VALUE)
cpsolver.Solve(db)
print('Test cpsolver...DONE')
"""Test pywrapcp."""
print("Test cpsolver...")
cpsolver = pywrapcp.Solver("ConstraintTest")
num_vals = 3
x = cpsolver.IntVar(0, num_vals - 1, "x")
y = cpsolver.IntVar(0, num_vals - 1, "y")
z = cpsolver.IntVar(0, num_vals - 1, "z")
cpsolver.Add(x != y)
db = cpsolver.Phase(
[x, y, z], cpsolver.CHOOSE_FIRST_UNBOUND, cpsolver.ASSIGN_MIN_VALUE
)
cpsolver.Solve(db)
print("Test cpsolver...DONE")
def main():
print(ortools.__version__)
lpsolver_test()
cpsolver_test()
print(ortools.__version__)
lpsolver_test()
cpsolver_test()
if __name__ == '__main__':
main()
if __name__ == "__main__":
main()