This commit is contained in:
lperron@google.com
2012-01-25 18:02:25 +00:00
parent 4c3bda8cea
commit 1155f383c6
5 changed files with 402 additions and 406 deletions

View File

@@ -1,105 +1,105 @@
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.Graph;
public class CsFlow
{
private static void SolveMinCostFlow()
{
Console.WriteLine("Min Cost Flow Problem");
int numSources = 4;
int numTargets = 4;
int[,] costs = { {90, 75, 75, 80},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115} };
int expectedCost = 275;
StarGraph graph = new StarGraph(numSources + numTargets,
numSources * numTargets);
MinCostFlow minCostFlow = new MinCostFlow(graph);
for (int source = 0; source < numSources; ++source)
{
for (int target = 0; target < numTargets; ++target) {
int arc = graph.AddArc(source, numSources + target);
minCostFlow.SetArcUnitCost(arc, costs[source, target]);
minCostFlow.SetArcCapacity(arc, 1);
}
}
for (int source = 0; source < numSources; ++source)
{
minCostFlow.SetNodeSupply(source, 1);
}
for (int target = 0; target < numTargets; ++target)
{
minCostFlow.SetNodeSupply(numSources + target, -1);
}
Console.WriteLine("Solving min cost flow with " + numSources +
" sources, and " + numTargets + " targets.");
if (minCostFlow.Solve())
{
long totalFlowCost = minCostFlow.GetOptimalCost();
Console.WriteLine("total computed flow cost = " + totalFlowCost +
", expected = " + expectedCost);
}
else
{
Console.WriteLine("No solution found");
}
}
private static void SolveMaxFlow()
{
Console.WriteLine("Max Flow Problem");
int numNodes = 6;
int numArcs = 9;
int[] tails = {0, 0, 0, 0, 1, 2, 3, 3, 4};
int[] heads = {1, 2, 3, 4, 3, 4, 4, 5, 5};
int[] capacities = {5, 8, 5, 3, 4, 5, 6, 6, 4};
int[] expectedFlows = {4, 4, 2, 0, 4, 4, 0, 6, 4};
int expectedTotalFlow = 10;
StarGraph graph = new StarGraph(numNodes, numArcs);
MaxFlow maxFlow = new MaxFlow(graph, 0, numNodes - 1);
for (int i = 0; i < numArcs; ++i)
{
int arc = graph.AddArc(tails[i], heads[i]);
maxFlow.SetArcCapacity(arc, capacities[i]);
}
Console.WriteLine("Solving max flow with " + numNodes + " nodes, and " +
numArcs + " arcs, source = 0, sink = " + (numNodes - 1));
if (maxFlow.Solve())
{
long totalFlow = maxFlow.GetOptimalFlow();
Console.WriteLine("total computed flow " + totalFlow +
", expected = " + expectedTotalFlow);
for (int i = 0; i < numArcs; ++i)
{
Console.WriteLine("Arc " + i + " (" + heads[i] + " -> " + tails[i] +
", capacity = " + capacities[i] + ") computed = " +
maxFlow.Flow(i) + ", expected = " + expectedFlows[i]);
}
}
else
{
Console.WriteLine("No solution found");
}
}
static void Main()
{
SolveMinCostFlow();
SolveMaxFlow();
}
}
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.Graph;
public class CsFlow
{
private static void SolveMinCostFlow()
{
Console.WriteLine("Min Cost Flow Problem");
int numSources = 4;
int numTargets = 4;
int[,] costs = { {90, 75, 75, 80},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115} };
int expectedCost = 275;
StarGraph graph = new StarGraph(numSources + numTargets,
numSources * numTargets);
MinCostFlow minCostFlow = new MinCostFlow(graph);
for (int source = 0; source < numSources; ++source)
{
for (int target = 0; target < numTargets; ++target) {
int arc = graph.AddArc(source, numSources + target);
minCostFlow.SetArcUnitCost(arc, costs[source, target]);
minCostFlow.SetArcCapacity(arc, 1);
}
}
for (int source = 0; source < numSources; ++source)
{
minCostFlow.SetNodeSupply(source, 1);
}
for (int target = 0; target < numTargets; ++target)
{
minCostFlow.SetNodeSupply(numSources + target, -1);
}
Console.WriteLine("Solving min cost flow with " + numSources +
" sources, and " + numTargets + " targets.");
if (minCostFlow.Solve())
{
long totalFlowCost = minCostFlow.GetOptimalCost();
Console.WriteLine("total computed flow cost = " + totalFlowCost +
", expected = " + expectedCost);
}
else
{
Console.WriteLine("No solution found");
}
}
private static void SolveMaxFlow()
{
Console.WriteLine("Max Flow Problem");
int numNodes = 6;
int numArcs = 9;
int[] tails = {0, 0, 0, 0, 1, 2, 3, 3, 4};
int[] heads = {1, 2, 3, 4, 3, 4, 4, 5, 5};
int[] capacities = {5, 8, 5, 3, 4, 5, 6, 6, 4};
int[] expectedFlows = {4, 4, 2, 0, 4, 4, 0, 6, 4};
int expectedTotalFlow = 10;
StarGraph graph = new StarGraph(numNodes, numArcs);
MaxFlow maxFlow = new MaxFlow(graph, 0, numNodes - 1);
for (int i = 0; i < numArcs; ++i)
{
int arc = graph.AddArc(tails[i], heads[i]);
maxFlow.SetArcCapacity(arc, capacities[i]);
}
Console.WriteLine("Solving max flow with " + numNodes + " nodes, and " +
numArcs + " arcs, source = 0, sink = " + (numNodes - 1));
if (maxFlow.Solve())
{
long totalFlow = maxFlow.GetOptimalFlow();
Console.WriteLine("total computed flow " + totalFlow +
", expected = " + expectedTotalFlow);
for (int i = 0; i < numArcs; ++i)
{
Console.WriteLine("Arc " + i + " (" + heads[i] + " -> " + tails[i] +
", capacity = " + capacities[i] + ") computed = " +
maxFlow.Flow(i) + ", expected = " + expectedFlows[i]);
}
}
else
{
Console.WriteLine("No solution found");
}
}
static void Main()
{
SolveMinCostFlow();
SolveMaxFlow();
}
}

View File

@@ -1,75 +1,75 @@
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.LinearSolver;
public class CsIntegerProgramming
{
private static void RunIntegerProgrammingExample(String solverType)
{
MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType);
if (solver == null)
{
Console.WriteLine("Could not create solver " + solverType);
return;
}
double infinity = MPSolver.Infinity();
// x1 and x2 are integer non-negative variables.
MPVariable x1 = solver.MakeIntVar(0.0, infinity, "x1");
MPVariable x2 = solver.MakeIntVar(0.0, infinity, "x2");
// Minimize x1 + 2 * x2.
solver.SetObjectiveCoefficient(x1, 1);
solver.SetObjectiveCoefficient(x2, 2);
// 2 * x2 + 3 * x1 >= 17.
MPConstraint ct = solver.MakeConstraint(17, infinity);
ct.SetCoefficient(x1, 3);
ct.SetCoefficient(x2, 2);
int resultStatus = solver.Solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
Console.WriteLine("Problem solved in " + solver.WallTime() +
" milliseconds");
// The objective value of the solution.
Console.WriteLine("Optimal objective value = " +
solver.ObjectiveValue());
// The value of each variable in the solution.
Console.WriteLine("x1 = " + x1.SolutionValue());
Console.WriteLine("x2 = " + x2.SolutionValue());
Console.WriteLine("Advanced usage:");
Console.WriteLine("Problem solved in " + solver.Nodes() +
" branch-and-bound nodes");
}
static void Main()
{
Console.WriteLine("---- Integer programming example with GLPK ----");
RunIntegerProgrammingExample("GLPK_MIXED_INTEGER_PROGRAMMING");
Console.WriteLine("---- Linear programming example with CBC ----");
RunIntegerProgrammingExample("CBC_MIXED_INTEGER_PROGRAMMING");
Console.WriteLine("---- Linear programming example with SCIP ----");
RunIntegerProgrammingExample("SCIP_MIXED_INTEGER_PROGRAMMING");
}
}
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.LinearSolver;
public class CsIntegerProgramming
{
private static void RunIntegerProgrammingExample(String solverType)
{
MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType);
if (solver == null)
{
Console.WriteLine("Could not create solver " + solverType);
return;
}
double infinity = MPSolver.Infinity();
// x1 and x2 are integer non-negative variables.
MPVariable x1 = solver.MakeIntVar(0.0, infinity, "x1");
MPVariable x2 = solver.MakeIntVar(0.0, infinity, "x2");
// Minimize x1 + 2 * x2.
solver.SetObjectiveCoefficient(x1, 1);
solver.SetObjectiveCoefficient(x2, 2);
// 2 * x2 + 3 * x1 >= 17.
MPConstraint ct = solver.MakeConstraint(17, infinity);
ct.SetCoefficient(x1, 3);
ct.SetCoefficient(x2, 2);
int resultStatus = solver.Solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
Console.WriteLine("Problem solved in " + solver.WallTime() +
" milliseconds");
// The objective value of the solution.
Console.WriteLine("Optimal objective value = " +
solver.ObjectiveValue());
// The value of each variable in the solution.
Console.WriteLine("x1 = " + x1.SolutionValue());
Console.WriteLine("x2 = " + x2.SolutionValue());
Console.WriteLine("Advanced usage:");
Console.WriteLine("Problem solved in " + solver.Nodes() +
" branch-and-bound nodes");
}
static void Main()
{
Console.WriteLine("---- Integer programming example with GLPK ----");
RunIntegerProgrammingExample("GLPK_MIXED_INTEGER_PROGRAMMING");
Console.WriteLine("---- Linear programming example with CBC ----");
RunIntegerProgrammingExample("CBC_MIXED_INTEGER_PROGRAMMING");
Console.WriteLine("---- Linear programming example with SCIP ----");
RunIntegerProgrammingExample("SCIP_MIXED_INTEGER_PROGRAMMING");
}
}

View File

@@ -1,49 +1,49 @@
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.Algorithms;
public class CsKnapsack
{
static void Main()
{
KnapsackSolver solver = new KnapsackSolver(
KnapsackSolver.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
long[] profits = { 360, 83, 59, 130, 431, 67, 230, 52, 93,
125, 670, 892, 600, 38, 48, 147, 78, 256,
63, 17, 120, 164, 432, 35, 92, 110, 22,
42, 50, 323, 514, 28, 87, 73, 78, 15,
26, 78, 210, 36, 85, 189, 274, 43, 33,
10, 19, 389, 276, 312 };
long[,] weights = { { 7, 0, 30, 22, 80, 94, 11, 81, 70,
64, 59, 18, 0, 36, 3, 8, 15, 42,
9, 0, 42, 47, 52, 32, 26, 48, 55,
6, 29, 84, 2, 4, 18, 56, 7, 29,
93, 44, 71, 3, 86, 66, 31, 65, 0,
79, 20, 65, 52, 13 } };
long[] capacities = { 850 };
long optimalProfit = 7534;
Console.WriteLine("Solving knapsack with " + profits.Length +
" items, and " + weights.GetLength(0) + " dimension");
solver.Init(profits, weights, capacities);
long computedProfit = solver.Solve();
Console.WriteLine("Optimal Profit = " + computedProfit + ", expected = " +
optimalProfit);
}
}
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.Algorithms;
public class CsKnapsack
{
static void Main()
{
KnapsackSolver solver = new KnapsackSolver(
KnapsackSolver.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
long[] profits = { 360, 83, 59, 130, 431, 67, 230, 52, 93,
125, 670, 892, 600, 38, 48, 147, 78, 256,
63, 17, 120, 164, 432, 35, 92, 110, 22,
42, 50, 323, 514, 28, 87, 73, 78, 15,
26, 78, 210, 36, 85, 189, 274, 43, 33,
10, 19, 389, 276, 312 };
long[,] weights = { { 7, 0, 30, 22, 80, 94, 11, 81, 70,
64, 59, 18, 0, 36, 3, 8, 15, 42,
9, 0, 42, 47, 52, 32, 26, 48, 55,
6, 29, 84, 2, 4, 18, 56, 7, 29,
93, 44, 71, 3, 86, 66, 31, 65, 0,
79, 20, 65, 52, 13 } };
long[] capacities = { 850 };
long optimalProfit = 7534;
Console.WriteLine("Solving knapsack with " + profits.Length +
" items, and " + weights.GetLength(0) + " dimension");
solver.Init(profits, weights, capacities);
long computedProfit = solver.Solve();
Console.WriteLine("Optimal Profit = " + computedProfit + ", expected = " +
optimalProfit);
}
}

View File

@@ -1,100 +1,100 @@
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.LinearSolver;
public class CsLinearProgramming
{
private static void RunLinearProgrammingExample(String solverType)
{
MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType);
if (solver == null)
{
Console.WriteLine("Could not create solver " + solverType);
return;
}
double infinity = MPSolver.Infinity();
// x1, x2 and x3 are continuous non-negative variables.
MPVariable x1 = solver.MakeNumVar(0.0, infinity, "x1");
MPVariable x2 = solver.MakeNumVar(0.0, infinity, "x2");
MPVariable x3 = solver.MakeNumVar(0.0, infinity, "x3");
// Maximize 10 * x1 + 6 * x2 + 4 * x3.
solver.SetObjectiveCoefficient(x1, 10);
solver.SetObjectiveCoefficient(x2, 6);
solver.SetObjectiveCoefficient(x3, 4);
solver.SetMaximization();
// x1 + x2 + x3 <= 100.
MPConstraint c0 = solver.MakeConstraint(-infinity, 100.0);
c0.SetCoefficient(x1, 1);
c0.SetCoefficient(x2, 1);
c0.SetCoefficient(x3, 1);
// 10 * x1 + 4 * x2 + 5 * x3 <= 600.
MPConstraint c1 = solver.MakeConstraint(-infinity, 600.0);
c1.SetCoefficient(x1, 10);
c1.SetCoefficient(x2, 4);
c1.SetCoefficient(x3, 5);
// 2 * x1 + 2 * x2 + 6 * x3 <= 300.
MPConstraint c2 = solver.MakeConstraint(-infinity, 300.0);
c2.SetCoefficient(x1, 2);
c2.SetCoefficient(x2, 2);
c2.SetCoefficient(x3, 6);
Console.WriteLine("Number of variables = " + solver.NumVariables());
Console.WriteLine("Number of constraints = " + solver.NumConstraints());
int resultStatus = solver.Solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.OPTIMAL) {
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
Console.WriteLine("Problem solved in " + solver.WallTime() +
" milliseconds");
// The objective value of the solution.
Console.WriteLine("Optimal objective value = " + solver.ObjectiveValue());
// The value of each variable in the solution.
Console.WriteLine("x1 = " + x1.SolutionValue());
Console.WriteLine("x2 = " + x2.SolutionValue());
Console.WriteLine("x3 = " + x3.SolutionValue());
Console.WriteLine("Advanced usage:");
Console.WriteLine("Problem solved in " + solver.Iterations() +
" iterations");
Console.WriteLine("x1: reduced cost = " + x1.ReducedCost());
Console.WriteLine("x2: reduced cost = " + x2.ReducedCost());
Console.WriteLine("x3: reduced cost = " + x3.ReducedCost());
Console.WriteLine("c0: dual value = " + c0.DualValue());
Console.WriteLine(" activity = " + c0.Activity());
Console.WriteLine("c1: dual value = " + c1.DualValue());
Console.WriteLine(" activity = " + c1.Activity());
Console.WriteLine("c2: dual value = " + c2.DualValue());
Console.WriteLine(" activity = " + c2.Activity());
}
static void Main()
{
Console.WriteLine("---- Linear programming example with GLPK ----");
RunLinearProgrammingExample("GLPK_LINEAR_PROGRAMMING");
Console.WriteLine("---- Linear programming example with CLP ----");
RunLinearProgrammingExample("CLP_LINEAR_PROGRAMMING");
}
}
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.LinearSolver;
public class CsLinearProgramming
{
private static void RunLinearProgrammingExample(String solverType)
{
MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType);
if (solver == null)
{
Console.WriteLine("Could not create solver " + solverType);
return;
}
double infinity = MPSolver.Infinity();
// x1, x2 and x3 are continuous non-negative variables.
MPVariable x1 = solver.MakeNumVar(0.0, infinity, "x1");
MPVariable x2 = solver.MakeNumVar(0.0, infinity, "x2");
MPVariable x3 = solver.MakeNumVar(0.0, infinity, "x3");
// Maximize 10 * x1 + 6 * x2 + 4 * x3.
solver.SetObjectiveCoefficient(x1, 10);
solver.SetObjectiveCoefficient(x2, 6);
solver.SetObjectiveCoefficient(x3, 4);
solver.SetMaximization();
// x1 + x2 + x3 <= 100.
MPConstraint c0 = solver.MakeConstraint(-infinity, 100.0);
c0.SetCoefficient(x1, 1);
c0.SetCoefficient(x2, 1);
c0.SetCoefficient(x3, 1);
// 10 * x1 + 4 * x2 + 5 * x3 <= 600.
MPConstraint c1 = solver.MakeConstraint(-infinity, 600.0);
c1.SetCoefficient(x1, 10);
c1.SetCoefficient(x2, 4);
c1.SetCoefficient(x3, 5);
// 2 * x1 + 2 * x2 + 6 * x3 <= 300.
MPConstraint c2 = solver.MakeConstraint(-infinity, 300.0);
c2.SetCoefficient(x1, 2);
c2.SetCoefficient(x2, 2);
c2.SetCoefficient(x3, 6);
Console.WriteLine("Number of variables = " + solver.NumVariables());
Console.WriteLine("Number of constraints = " + solver.NumConstraints());
int resultStatus = solver.Solve();
// Check that the problem has an optimal solution.
if (resultStatus != MPSolver.OPTIMAL) {
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
Console.WriteLine("Problem solved in " + solver.WallTime() +
" milliseconds");
// The objective value of the solution.
Console.WriteLine("Optimal objective value = " + solver.ObjectiveValue());
// The value of each variable in the solution.
Console.WriteLine("x1 = " + x1.SolutionValue());
Console.WriteLine("x2 = " + x2.SolutionValue());
Console.WriteLine("x3 = " + x3.SolutionValue());
Console.WriteLine("Advanced usage:");
Console.WriteLine("Problem solved in " + solver.Iterations() +
" iterations");
Console.WriteLine("x1: reduced cost = " + x1.ReducedCost());
Console.WriteLine("x2: reduced cost = " + x2.ReducedCost());
Console.WriteLine("x3: reduced cost = " + x3.ReducedCost());
Console.WriteLine("c0: dual value = " + c0.DualValue());
Console.WriteLine(" activity = " + c0.Activity());
Console.WriteLine("c1: dual value = " + c1.DualValue());
Console.WriteLine(" activity = " + c1.Activity());
Console.WriteLine("c2: dual value = " + c2.DualValue());
Console.WriteLine(" activity = " + c2.Activity());
}
static void Main()
{
Console.WriteLine("---- Linear programming example with GLPK ----");
RunLinearProgrammingExample("GLPK_LINEAR_PROGRAMMING");
Console.WriteLine("---- Linear programming example with CLP ----");
RunLinearProgrammingExample("CLP_LINEAR_PROGRAMMING");
}
}

View File

@@ -1,77 +1,73 @@
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.ConstraintSolver;
/**
* Shows how to write a custom decision builder.
*/
public class AssignFirstUnboundToMin : NetDecisionBuilder
{
public AssignFirstUnboundToMin(IntVar[] vars)
{
vars_ = vars;
}
public override Decision Next(Solver solver)
{
foreach (IntVar var in vars_)
{
if (!var.Bound())
{
return solver.MakeAssignVariableValue(var, var.Min());
}
else
{
solver.Fail();
}
}
return null;
}
private IntVar[] vars_;
}
public class CsRabbitsPheasants
{
/**
* Solves the rabbits + pheasants problem. We are seing 20 heads
* and 56 legs. How many rabbits and how many pheasants are we thus
* seeing?
*/
private static void Solve()
{
Solver solver = new Solver("RabbitsPheasants");
IntVar rabbits = solver.MakeIntVar(0, 100, "rabbits");
IntVar pheasants = solver.MakeIntVar(0, 100, "pheasants");
solver.Add(rabbits + pheasants == 20);
solver.Add(rabbits * 4 + pheasants * 2 == 56);
DecisionBuilder db =
new AssignFirstUnboundToMin(new IntVar[] {rabbits, pheasants});
solver.NewSearch(db);
solver.NextSolution();
Console.WriteLine(
"Solved Rabbits + Pheasants in {0} ms, and {1} search tree branches.",
solver.WallTime(), solver.Branches());
Console.WriteLine(rabbits.ToString());
Console.WriteLine(pheasants.ToString());
solver.EndSearch();
}
public static void Main(String[] args)
{
Solve();
}
}
// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using Google.OrTools.ConstraintSolver;
/**
* Shows how to write a custom decision builder.
*/
public class AssignFirstUnboundToMin : NetDecisionBuilder
{
public AssignFirstUnboundToMin(IntVar[] vars)
{
vars_ = vars;
}
public override Decision Next(Solver solver)
{
foreach (IntVar var in vars_)
{
if (!var.Bound())
{
return solver.MakeAssignVariableValue(var, var.Min());
}
}
return null;
}
private IntVar[] vars_;
}
public class CsRabbitsPheasants
{
/**
* Solves the rabbits + pheasants problem. We are seing 20 heads
* and 56 legs. How many rabbits and how many pheasants are we thus
* seeing?
*/
private static void Solve()
{
Solver solver = new Solver("RabbitsPheasants");
IntVar rabbits = solver.MakeIntVar(0, 100, "rabbits");
IntVar pheasants = solver.MakeIntVar(0, 100, "pheasants");
solver.Add(rabbits + pheasants == 20);
solver.Add(rabbits * 4 + pheasants * 2 == 56);
DecisionBuilder db =
new AssignFirstUnboundToMin(new IntVar[] {rabbits, pheasants});
solver.NewSearch(db);
solver.NextSolution();
Console.WriteLine(
"Solved Rabbits + Pheasants in {0} ms, and {1} search tree branches.",
solver.WallTime(), solver.Branches());
Console.WriteLine(rabbits.ToString());
Console.WriteLine(pheasants.ToString());
solver.EndSearch();
}
public static void Main(String[] args)
{
Solve();
}
}