dotnet: Add RoutingSolver test.
This commit is contained in:
40
examples/tests/RoutingSolverTests.cs
Normal file
40
examples/tests/RoutingSolverTests.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
using Google.OrTools.ConstraintSolver;
|
||||
|
||||
namespace Google.OrTools.Tests {
|
||||
public class RoutingSolverTest {
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void SimpleLambdaCallback(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.OrTools.Tests
|
||||
|
||||
24
examples/tests/RoutingSolverTests.csproj
Normal file
24
examples/tests/RoutingSolverTests.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<LangVersion>7.2</LangVersion>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<EnableDefaultItems>false</EnableDefaultItems>
|
||||
<AssemblyName>Google.OrTools.RoutingSolverTests</AssemblyName>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<RestoreSources>../../packages;$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugType>full</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="RoutingSolverTests.cs" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||
<PackageReference Include="Google.OrTools" Version="7.0.6170-*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -578,6 +578,7 @@ check_dotnet_pimpl: \
|
||||
.PHONY: test_dotnet_tests # Build and Run all .Net Tests (located in examples/test)
|
||||
test_dotnet_tests:
|
||||
$(MAKE) run_test SOURCE=examples/tests/ConstraintSolverTests.cs
|
||||
$(MAKE) run_test SOURCE=examples/tests/RoutingSolverTests.cs
|
||||
$(MAKE) run SOURCE=examples/tests/issue18.cs
|
||||
$(MAKE) run SOURCE=examples/tests/issue22.cs
|
||||
$(MAKE) run SOURCE=examples/tests/issue33.cs
|
||||
|
||||
Reference in New Issue
Block a user