From 6eafbab77b83e2a4489e5bf42594725ab9dd689c Mon Sep 17 00:00:00 2001 From: "lperron@google.com" Date: Sun, 29 Jan 2012 18:15:19 +0000 Subject: [PATCH] remove MP prefix in C# Math Programming Support --- .../ortools/linearsolver/LinearConstraint.cs | 32 ++--- com/google/ortools/linearsolver/LinearExpr.cs | 30 ++--- csharp/csintegerprogramming.cs | 18 +-- csharp/cslinearprogramming.cs | 32 ++--- csharp/set_covering_skiena.cs | 12 +- csharp/set_partition.cs | 13 +- linear_solver/linear_solver.swig | 123 +++++++++--------- makefiles/Makefile.test.unix | 3 +- makefiles/Makefile.test.win | 3 +- tests/testlp.cs | 100 +++++++------- 10 files changed, 186 insertions(+), 180 deletions(-) diff --git a/com/google/ortools/linearsolver/LinearConstraint.cs b/com/google/ortools/linearsolver/LinearConstraint.cs index 96a544a3ea..fbfebbcd6f 100644 --- a/com/google/ortools/linearsolver/LinearConstraint.cs +++ b/com/google/ortools/linearsolver/LinearConstraint.cs @@ -24,7 +24,7 @@ public class LinearConstraint return "LinearConstraint"; } - public virtual MPConstraint Extract(MPSolver solver) + public virtual Constraint Extract(Solver solver) { return null; } @@ -45,13 +45,13 @@ public class RangeConstraint : LinearConstraint return "" + lb_ + " <= " + expr_.ToString() + " <= " + ub_; } - public override MPConstraint Extract(MPSolver solver) + public override Constraint Extract(Solver solver) { - Dictionary coefficients = - new Dictionary(); + Dictionary coefficients = + new Dictionary(); double constant = expr_.Visit(coefficients); - MPConstraint ct = solver.MakeConstraint(lb_ - constant, ub_ - constant); - foreach (KeyValuePair pair in coefficients) + Constraint ct = solver.MakeConstraint(lb_ - constant, ub_ - constant); + foreach (KeyValuePair pair in coefficients) { ct.SetCoefficient(pair.Key, pair.Value); } @@ -82,14 +82,14 @@ public class Equality : LinearConstraint return "" + left_.ToString() + " == " + right_.ToString(); } - public override MPConstraint Extract(MPSolver solver) + public override Constraint Extract(Solver solver) { - Dictionary coefficients = - new Dictionary(); + Dictionary coefficients = + new Dictionary(); double constant = left_.Visit(coefficients); constant += right_.DoVisit(coefficients, -1); - MPConstraint ct = solver.MakeConstraint(-constant, -constant); - foreach (KeyValuePair pair in coefficients) + Constraint ct = solver.MakeConstraint(-constant, -constant); + foreach (KeyValuePair pair in coefficients) { ct.SetCoefficient(pair.Key, pair.Value); } @@ -108,7 +108,7 @@ public class Equality : LinearConstraint public class VarEquality : LinearConstraint { - public VarEquality(MPVariable left, MPVariable right, bool equality) + public VarEquality(Variable left, Variable right, bool equality) { this.left_ = left; this.right_ = right; @@ -120,9 +120,9 @@ public class VarEquality : LinearConstraint return "" + left_.Name() + " == " + right_.Name(); } - public override MPConstraint Extract(MPSolver solver) + public override Constraint Extract(Solver solver) { - MPConstraint ct = solver.MakeConstraint(0.0, 0.0); + Constraint ct = solver.MakeConstraint(0.0, 0.0); ct.SetCoefficient(left_, 1.0); ct.SetCoefficient(right_, -1.0); return ct; @@ -133,8 +133,8 @@ public class VarEquality : LinearConstraint return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_; } - private MPVariable left_; - private MPVariable right_; + private Variable left_; + private Variable right_; private bool equality_; } } // namespace Google.OrTools.LinearSolver diff --git a/com/google/ortools/linearsolver/LinearExpr.cs b/com/google/ortools/linearsolver/LinearExpr.cs index ee806ad6e3..3c2604e09e 100644 --- a/com/google/ortools/linearsolver/LinearExpr.cs +++ b/com/google/ortools/linearsolver/LinearExpr.cs @@ -18,13 +18,13 @@ namespace Google.OrTools.LinearSolver public class LinearExpr { - public virtual double DoVisit(Dictionary coefficients, + public virtual double DoVisit(Dictionary coefficients, double multiplier) { return 0; } - public double Visit(Dictionary coefficients) + public double Visit(Dictionary coefficients) { return DoVisit(coefficients, 1.0); } @@ -129,7 +129,7 @@ public class LinearExpr return a - b >= 0; } - public static implicit operator LinearExpr(MPVariable a) + public static implicit operator LinearExpr(Variable a) { return new VarWrapper(a); } @@ -142,7 +142,7 @@ public static class LinearExprArrayHelper return new SumArray(exprs); } - public static LinearExpr Sum(this MPVariable[] vars) + public static LinearExpr Sum(this Variable[] vars) { return new SumVarArray(vars); } @@ -174,7 +174,7 @@ class ProductCst : LinearExpr return "(" + expr_.ToString() + " * " + coeff_ + ")"; } - public override double DoVisit(Dictionary coefficients, + public override double DoVisit(Dictionary coefficients, double multiplier) { double current_multiplier = multiplier * coeff_; @@ -205,7 +205,7 @@ class SumCst : LinearExpr return "(" + expr_.ToString() + " + " + coeff_ + ")"; } - public override double DoVisit(Dictionary coefficients, + public override double DoVisit(Dictionary coefficients, double multiplier) { if (multiplier != 0.0) @@ -224,7 +224,7 @@ class SumCst : LinearExpr class VarWrapper : LinearExpr { - public VarWrapper(MPVariable var) + public VarWrapper(Variable var) { this.var_ = var; } @@ -234,7 +234,7 @@ class VarWrapper : LinearExpr return var_.Name(); } - public override double DoVisit(Dictionary coefficients, + public override double DoVisit(Dictionary coefficients, double multiplier) { if (multiplier != 0.0) @@ -251,7 +251,7 @@ class VarWrapper : LinearExpr return 0.0; } - private MPVariable var_; + private Variable var_; } @@ -268,7 +268,7 @@ class Sum : LinearExpr return "(" + left_.ToString() + " + " + right_.ToString() + ")"; } - public override double DoVisit(Dictionary coefficients, + public override double DoVisit(Dictionary coefficients, double multiplier) { if (multiplier != 0.0) @@ -293,7 +293,7 @@ public class SumArray : LinearExpr this.array_ = array; } - public override double DoVisit(Dictionary coefficients, + public override double DoVisit(Dictionary coefficients, double multiplier) { if (multiplier != 0.0) { @@ -315,16 +315,16 @@ public class SumArray : LinearExpr public class SumVarArray : LinearExpr { - public SumVarArray(MPVariable[] array) + public SumVarArray(Variable[] array) { this.array_ = array; } - public override double DoVisit(Dictionary coefficients, + public override double DoVisit(Dictionary coefficients, double multiplier) { if (multiplier != 0.0) { - foreach (MPVariable var in array_) + foreach (Variable var in array_) { if (coefficients.ContainsKey(var)) { @@ -339,6 +339,6 @@ public class SumVarArray : LinearExpr return 0.0; } - private MPVariable[] array_; + private Variable[] array_; } } // namespace Google.OrTools.LinearSolver diff --git a/csharp/csintegerprogramming.cs b/csharp/csintegerprogramming.cs index 2c5762ec64..18a42c194d 100644 --- a/csharp/csintegerprogramming.cs +++ b/csharp/csintegerprogramming.cs @@ -18,29 +18,29 @@ public class CsIntegerProgramming { private static void RunIntegerProgrammingExample(String solverType) { - MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType); + Solver solver = Solver.CreateSolver("IntegerProgramming", solverType); if (solver == null) { Console.WriteLine("Could not create solver " + solverType); return; } // x1 and x2 are integer non-negative variables. - MPVariable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1"); - MPVariable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2"); + Variable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1"); + Variable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2"); // Minimize x1 + 2 * x2. solver.SetObjectiveCoefficient(x1, 1); solver.SetObjectiveCoefficient(x2, 2); // 2 * x2 + 3 * x1 >= 17. - MPConstraint ct = solver.MakeConstraint(17, double.PositiveInfinity); + Constraint ct = solver.MakeConstraint(17, double.PositiveInfinity); ct.SetCoefficient(x1, 3); ct.SetCoefficient(x2, 2); int resultStatus = solver.Solve(); // Check that the problem has an optimal solution. - if (resultStatus != MPSolver.OPTIMAL) + if (resultStatus != Solver.OPTIMAL) { Console.WriteLine("The problem does not have an optimal solution!"); return; @@ -64,15 +64,15 @@ public class CsIntegerProgramming private static void RunIntegerProgrammingExampleNaturalApi(String solverType) { - MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType); + Solver solver = Solver.CreateSolver("IntegerProgramming", solverType); if (solver == null) { Console.WriteLine("Could not create solver " + solverType); return; } // x1 and x2 are integer non-negative variables. - MPVariable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1"); - MPVariable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2"); + Variable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1"); + Variable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2"); solver.Minimize(x1 + 2 * x2); solver.Add(2 * x2 + 3 * x1 >= 17); @@ -80,7 +80,7 @@ public class CsIntegerProgramming int resultStatus = solver.Solve(); // Check that the problem has an optimal solution. - if (resultStatus != MPSolver.OPTIMAL) + if (resultStatus != Solver.OPTIMAL) { Console.WriteLine("The problem does not have an optimal solution!"); return; diff --git a/csharp/cslinearprogramming.cs b/csharp/cslinearprogramming.cs index 50e10dedee..843b4b7710 100644 --- a/csharp/cslinearprogramming.cs +++ b/csharp/cslinearprogramming.cs @@ -18,16 +18,16 @@ public class CsLinearProgramming { private static void RunLinearProgrammingExample(String solverType) { - MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType); + Solver solver = Solver.CreateSolver("IntegerProgramming", solverType); if (solver == null) { Console.WriteLine("Could not create solver " + solverType); return; } // x1, x2 and x3 are continuous non-negative variables. - MPVariable x1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x1"); - MPVariable x2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x2"); - MPVariable x3 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x3"); + Variable x1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x1"); + Variable x2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x2"); + Variable x3 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x3"); // Maximize 10 * x1 + 6 * x2 + 4 * x3. solver.SetObjectiveCoefficient(x1, 10); @@ -36,19 +36,19 @@ public class CsLinearProgramming solver.SetMaximization(); // x1 + x2 + x3 <= 100. - MPConstraint c0 = solver.MakeConstraint(double.NegativeInfinity, 100.0); + Constraint c0 = solver.MakeConstraint(double.NegativeInfinity, 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(double.NegativeInfinity, 600.0); + Constraint c1 = solver.MakeConstraint(double.NegativeInfinity, 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(double.NegativeInfinity, 300.0); + Constraint c2 = solver.MakeConstraint(double.NegativeInfinity, 300.0); c2.SetCoefficient(x1, 2); c2.SetCoefficient(x2, 2); c2.SetCoefficient(x3, 6); @@ -59,7 +59,7 @@ public class CsLinearProgramming int resultStatus = solver.Solve(); // Check that the problem has an optimal solution. - if (resultStatus != MPSolver.OPTIMAL) { + if (resultStatus != Solver.OPTIMAL) { Console.WriteLine("The problem does not have an optimal solution!"); return; } @@ -91,21 +91,21 @@ public class CsLinearProgramming private static void RunLinearProgrammingExampleNaturalApi(String solverType) { - MPSolver solver = MPSolver.CreateSolver("IntegerProgramming", solverType); + Solver solver = Solver.CreateSolver("IntegerProgramming", solverType); if (solver == null) { Console.WriteLine("Could not create solver " + solverType); return; } // x1, x2 and x3 are continuous non-negative variables. - MPVariable x1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x1"); - MPVariable x2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x2"); - MPVariable x3 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x3"); + Variable x1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x1"); + Variable x2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x2"); + Variable x3 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x3"); solver.Maximize(10 * x1 + 6 * x2 + 4 * x3); - MPConstraint c0 = solver.Add(x1 + x2 + x3 <= 100); - MPConstraint c1 = solver.Add(10 * x1 + x2 * 4 + 5 * x3 <= 600); - MPConstraint c2 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300); + Constraint c0 = solver.Add(x1 + x2 + x3 <= 100); + Constraint c1 = solver.Add(10 * x1 + x2 * 4 + 5 * x3 <= 600); + Constraint c2 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300); Console.WriteLine("Number of variables = " + solver.NumVariables()); Console.WriteLine("Number of constraints = " + solver.NumConstraints()); @@ -113,7 +113,7 @@ public class CsLinearProgramming int resultStatus = solver.Solve(); // Check that the problem has an optimal solution. - if (resultStatus != MPSolver.OPTIMAL) { + if (resultStatus != Solver.OPTIMAL) { Console.WriteLine("The problem does not have an optimal solution!"); return; } diff --git a/csharp/set_covering_skiena.cs b/csharp/set_covering_skiena.cs index 71d797682b..739625523d 100644 --- a/csharp/set_covering_skiena.cs +++ b/csharp/set_covering_skiena.cs @@ -29,7 +29,7 @@ public class SetCoveringSkiena * """ * Input Description: A set of subsets S_1, ..., S_m of the * universal set U = {1,...,n}. - * + * * Problem: What is the smallest subset of subsets T subset S such * that \cup_{t_i in T} t_i = U? * """ @@ -50,7 +50,7 @@ public class SetCoveringSkiena IEnumerable Elements = Enumerable.Range(0, num_elements); // Which element belongs to which set - int[,] belongs = + int[,] belongs = { // 1 2 3 4 5 6 7 8 9 0 1 2 elements {1,1,0,0,0,0,0,0,0,0,0,0}, // Set 1 @@ -88,7 +88,7 @@ public class SetCoveringSkiena from j in Elements select (x[i]*belongs[i,j]).Var() ).ToArray().Sum() == tot_elements); - + // // Objective // @@ -107,10 +107,10 @@ public class SetCoveringSkiena Console.WriteLine("z: {0}", z.Value()); Console.WriteLine("tot_elements: {0}", tot_elements.Value()); Console.WriteLine( - "x: {0}", + "x: {0}", String.Join(" ", (from i in Enumerable.Range(0, num_sets) - select x[i].Value()).ToArray())); - + select x[i].Value().ToString()).ToArray())); + } Console.WriteLine("\nSolutions: {0}", solver.Solutions()); diff --git a/csharp/set_partition.cs b/csharp/set_partition.cs index 769627eb90..60dba13264 100644 --- a/csharp/set_partition.cs +++ b/csharp/set_partition.cs @@ -26,8 +26,7 @@ public class SetPartition // // Partition the sets (binary matrix representation). // - public static void partition_sets( - Solver solver, + public static void partition_sets(Solver solver, IntVar[,] x, int num_sets, int n) { @@ -109,14 +108,14 @@ public class SetPartition (from k in NRange select a[i,k].Var()).ToArray().Sum() == (from k in NRange select a[j,k].Var()).ToArray().Sum()); - + // same sum solver.Add( (from k in NRange select (k*a[i,k]).Var()).ToArray().Sum() == (from k in NRange select (k*a[j,k]).Var()).ToArray().Sum()); - - + + // same sum squared solver.Add( (from k in NRange select (k*a[i,k]*k*a[i,k]).Var()).ToArray().Sum() @@ -169,7 +168,7 @@ public class SetPartition } } Console.WriteLine(); - + } Console.WriteLine("\nSolutions: {0}", solver.Solutions()); @@ -195,7 +194,7 @@ public class SetPartition } if (n % num_sets == 0) { - + Solve(n, num_sets); } else { Console.WriteLine("n {0} num_sets {1}: Equal sets is not possible!", diff --git a/linear_solver/linear_solver.swig b/linear_solver/linear_solver.swig index 4b4bcd58e0..98377a5c98 100644 --- a/linear_solver/linear_solver.swig +++ b/linear_solver/linear_solver.swig @@ -573,6 +573,13 @@ namespace operations_research { %rename("%(camelcase)s", %$isfunction) ""; // Rename MakeRowConstraint into MakeConstraint %rename (MakeConstraint) MPSolver::MakeRowConstraint; +// Rename classes, remove MP prefix. +%rename (Solver) MPSolver; +%rename (Variable) MPVariable; +%rename (Constraint) MPConstraint; +%rename (Objective) MPObjective; +%rename (SolverParameters) MPSolverParameters; + // Ignore code on MPSolver, see replacement java code below. %ignore MPSolver::MakeVarArray; %ignore MPSolver::MakeNumVarArray; @@ -586,235 +593,235 @@ namespace operations_research { %ignore MPSolver::SolveWithProtocolBuffers; %typemap(cscode) MPVariable %{ - public static LinearExpr operator+(MPVariable a, double v) + public static LinearExpr operator+(Variable a, double v) { return new VarWrapper(a) + v; } - public static LinearExpr operator+(double v, MPVariable a) + public static LinearExpr operator+(double v, Variable a) { return a + v; } - public static LinearExpr operator+(MPVariable a, LinearExpr b) + public static LinearExpr operator+(Variable a, LinearExpr b) { return new VarWrapper(a) + b; } - public static LinearExpr operator+(MPVariable a, MPVariable b) + public static LinearExpr operator+(Variable a, Variable b) { return new VarWrapper(a) + new VarWrapper(b); } - public static LinearExpr operator+(LinearExpr a, MPVariable b) + public static LinearExpr operator+(LinearExpr a, Variable b) { return a + new VarWrapper(b); } - public static LinearExpr operator-(MPVariable a, double v) + public static LinearExpr operator-(Variable a, double v) { return new VarWrapper(a) - v; } - public static LinearExpr operator-(double v, MPVariable a) + public static LinearExpr operator-(double v, Variable a) { return v - new VarWrapper(a); } - public static LinearExpr operator-(MPVariable a, LinearExpr b) + public static LinearExpr operator-(Variable a, LinearExpr b) { return new VarWrapper(a) - b; } - public static LinearExpr operator-(LinearExpr a, MPVariable b) + public static LinearExpr operator-(LinearExpr a, Variable b) { return a - new VarWrapper(b); } - public static LinearExpr operator-(MPVariable a, MPVariable b) + public static LinearExpr operator-(Variable a, Variable b) { return new VarWrapper(a) - new VarWrapper(b); } - public static LinearExpr operator-(MPVariable a) + public static LinearExpr operator-(Variable a) { return - new VarWrapper(a); } - public static LinearExpr operator*(MPVariable a, double v) + public static LinearExpr operator*(Variable a, double v) { return new VarWrapper(a) * v; } - public static LinearExpr operator/(MPVariable a, double v) + public static LinearExpr operator/(Variable a, double v) { return new VarWrapper(a) / v; } - public static LinearExpr operator*(double v, MPVariable a) + public static LinearExpr operator*(double v, Variable a) { return v * new VarWrapper(a); } - public static RangeConstraint operator==(MPVariable a, double v) + public static RangeConstraint operator==(Variable a, double v) { return new VarWrapper(a) == v; } - public static RangeConstraint operator==(double v, MPVariable a) + public static RangeConstraint operator==(double v, Variable a) { return v == new VarWrapper(a); } - public static RangeConstraint operator!=(MPVariable a, double v) + public static RangeConstraint operator!=(Variable a, double v) { return new VarWrapper(a) != v; } - public static RangeConstraint operator!=(double v, MPVariable a) + public static RangeConstraint operator!=(double v, Variable a) { return new VarWrapper(a) != v; } - public static Equality operator==(MPVariable a, LinearExpr b) + public static Equality operator==(Variable a, LinearExpr b) { return new VarWrapper(a) == b; } - public static Equality operator==(LinearExpr a, MPVariable b) + public static Equality operator==(LinearExpr a, Variable b) { return a == new VarWrapper(b); } - public static VarEquality operator==(MPVariable a, MPVariable b) + public static VarEquality operator==(Variable a, Variable b) { return new VarEquality(a, b, true); } - public static Equality operator!=(MPVariable a, LinearExpr b) + public static Equality operator!=(Variable a, LinearExpr b) { return new VarWrapper(a) != b; } - public static Equality operator!=(LinearExpr a, MPVariable b) + public static Equality operator!=(LinearExpr a, Variable b) { return a != new VarWrapper(b); } - public static VarEquality operator!=(MPVariable a, MPVariable b) + public static VarEquality operator!=(Variable a, Variable b) { return new VarEquality(a, b, false); } - public static RangeConstraint operator<=(MPVariable a, double v) + public static RangeConstraint operator<=(Variable a, double v) { return new VarWrapper(a) <= v; } - public static RangeConstraint operator>=(MPVariable a, double v) + public static RangeConstraint operator>=(Variable a, double v) { return new VarWrapper(a) >= v; } - public static RangeConstraint operator<=(double v, MPVariable a) + public static RangeConstraint operator<=(double v, Variable a) { return new VarWrapper(a) >= v; } - public static RangeConstraint operator>=(double v, MPVariable a) + public static RangeConstraint operator>=(double v, Variable a) { return new VarWrapper(a) <= v; } - public static RangeConstraint operator<=(MPVariable a, LinearExpr b) + public static RangeConstraint operator<=(Variable a, LinearExpr b) { return new VarWrapper(a) <= b; } - public static RangeConstraint operator>=(MPVariable a, LinearExpr b) + public static RangeConstraint operator>=(Variable a, LinearExpr b) { return new VarWrapper(a) >= b; } - public static RangeConstraint operator<=(MPVariable a, MPVariable b) + public static RangeConstraint operator<=(Variable a, Variable b) { return new VarWrapper(a) <= new VarWrapper(b); } - public static RangeConstraint operator>=(MPVariable a, MPVariable b) + public static RangeConstraint operator>=(Variable a, Variable b) { return new VarWrapper(a) >= new VarWrapper(b); } - public static RangeConstraint operator<=(LinearExpr a, MPVariable b) + public static RangeConstraint operator<=(LinearExpr a, Variable b) { return a <= new VarWrapper(b); } - public static RangeConstraint operator>=(LinearExpr a, MPVariable b) + public static RangeConstraint operator>=(LinearExpr a, Variable b) { return a >= new VarWrapper(b); } %} -// Add csharp code on MPSolver. +// Add csharp code on Solver. %typemap(cscode) MPSolver %{ - public MPVariable[] MakeVarArray(int count, + public Variable[] MakeVarArray(int count, double lb, double ub, bool integer) { - MPVariable[] array = new MPVariable[count]; + Variable[] array = new Variable[count]; for (int i = 0; i < count; ++i) { array[i] = MakeVar(lb, ub, integer, ""); } return array; } - public MPVariable[] MakeVarArray(int count, + public Variable[] MakeVarArray(int count, double lb, double ub, bool integer, string var_name) { - MPVariable[] array = new MPVariable[count]; + Variable[] array = new Variable[count]; for (int i = 0; i < count; ++i) { array[i] = MakeVar(lb, ub, integer, var_name + i); } return array; } - public MPVariable[] MakeNumVarArray(int count, double lb, double ub) { + public Variable[] MakeNumVarArray(int count, double lb, double ub) { return MakeVarArray(count, lb, ub, false); } - public MPVariable[] MakeNumVarArray(int count, + public Variable[] MakeNumVarArray(int count, double lb, double ub, string var_name) { return MakeVarArray(count, lb, ub, false, var_name); } - public MPVariable[] MakeIntVarArray(int count, double lb, double ub) { + public Variable[] MakeIntVarArray(int count, double lb, double ub) { return MakeVarArray(count, lb, ub, true); } - public MPVariable[] MakeIntVarArray(int count, + public Variable[] MakeIntVarArray(int count, double lb, double ub, string var_name) { return MakeVarArray(count, lb, ub, true, var_name); } - public MPVariable[] MakeBoolVarArray(int count) { + public Variable[] MakeBoolVarArray(int count) { return MakeVarArray(count, 0.0, 1.0, true); } - public MPVariable[] MakeBoolVarArray(int count, string var_name) { + public Variable[] MakeBoolVarArray(int count, string var_name) { return MakeVarArray(count, 0.0, 1.0, true, var_name); } public static int GetSolverEnum(String solverType) { System.Reflection.FieldInfo fieldInfo = - typeof(MPSolver).GetField(solverType); + typeof(Solver).GetField(solverType); if (fieldInfo != null) { return (int)fieldInfo.GetValue(null); } else { @@ -822,17 +829,17 @@ namespace operations_research { } } - public static MPSolver CreateSolver(String name, String type) { + public static Solver CreateSolver(String name, String type) { System.Reflection.FieldInfo fieldInfo = - typeof(MPSolver).GetField(type); + typeof(Solver).GetField(type); if (fieldInfo != null) { - return new MPSolver(name, (int)fieldInfo.GetValue(null)); + return new Solver(name, (int)fieldInfo.GetValue(null)); } else { return null; } } - public MPConstraint Add(LinearConstraint constraint) { + public Constraint Add(LinearConstraint constraint) { return constraint.Extract(this); } @@ -840,10 +847,10 @@ namespace operations_research { { ClearObjective(); SetMinimization(); - Dictionary coefficients = - new Dictionary(); + Dictionary coefficients = + new Dictionary(); double constant = expr.Visit(coefficients); - foreach (KeyValuePair pair in coefficients) + foreach (KeyValuePair pair in coefficients) { SetObjectiveCoefficient(pair.Key, pair.Value); SetObjectiveOffset(constant); @@ -854,24 +861,24 @@ namespace operations_research { { ClearObjective(); SetMaximization(); - Dictionary coefficients = - new Dictionary(); + Dictionary coefficients = + new Dictionary(); double constant = expr.Visit(coefficients); - foreach (KeyValuePair pair in coefficients) + foreach (KeyValuePair pair in coefficients) { SetObjectiveCoefficient(pair.Key, pair.Value); SetObjectiveOffset(constant); } } - public void Minimize(MPVariable var) + public void Minimize(Variable var) { ClearObjective(); SetMinimization(); SetObjectiveCoefficient(var, 1.0); } - public void Maximize(MPVariable var) + public void Maximize(Variable var) { ClearObjective(); SetMaximization(); diff --git a/makefiles/Makefile.test.unix b/makefiles/Makefile.test.unix index 6a441666e5..60b59d2005 100644 --- a/makefiles/Makefile.test.unix +++ b/makefiles/Makefile.test.unix @@ -19,7 +19,7 @@ test_python: python test_java: java run_RabbitsPheasants run_FlowExample run_LinearProgramming run_IntegerProgramming run_Knapsack # csharp test -test_csharp: $(CSHARPEXE) +test_csharp: $(CSHARPEXE) testlp.exe $(MONO) cslinearprogramming.exe $(MONO) csintegerprogramming.exe $(MONO) csrabbitspheasants.exe @@ -27,3 +27,4 @@ test_csharp: $(CSHARPEXE) $(MONO) csknapsack.exe $(MONO) furniture_moving_intervals.exe $(MONO) organize_day_intervals.exe + $(MONO) testlp.exe diff --git a/makefiles/Makefile.test.win b/makefiles/Makefile.test.win index 658ee87f31..f5010a2331 100644 --- a/makefiles/Makefile.test.win +++ b/makefiles/Makefile.test.win @@ -18,7 +18,7 @@ test_python: python test_java: java run_RabbitsPheasants run_FlowExample run_LinearProgramming run_IntegerProgramming run_Knapsack # csharp test -test_csharp: $(CSHARPEXE) +test_csharp: $(CSHARPEXE) testlp.exe cslinearprogramming.exe csintegerprogramming.exe csrabbitspheasants.exe @@ -26,6 +26,7 @@ test_csharp: $(CSHARPEXE) csknapsack.exe furniture_moving_intervals.exe organize_day_intervals.exe + testlp.exe diff --git a/tests/testlp.cs b/tests/testlp.cs index ed40aa68f6..78abc55d13 100644 --- a/tests/testlp.cs +++ b/tests/testlp.cs @@ -35,15 +35,15 @@ public class CsTestLp static void TestVarOperator() { Console.WriteLine("Running TestVarOperator"); - MPSolver solver = new MPSolver("TestVarOperator", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable x = solver.MakeNumVar(0.0, 100.0, "x"); - MPConstraint ct1 = solver.Add(x >= 1); - MPConstraint ct2 = solver.Add(x <= 1); - MPConstraint ct3 = solver.Add(x == 1); - MPConstraint ct4 = solver.Add(1 >= x); - MPConstraint ct5 = solver.Add(1 <= x); - MPConstraint ct6 = solver.Add(1 == x); + Solver solver = new Solver("TestVarOperator", + Solver.CLP_LINEAR_PROGRAMMING); + Variable x = solver.MakeNumVar(0.0, 100.0, "x"); + Constraint ct1 = solver.Add(x >= 1); + Constraint ct2 = solver.Add(x <= 1); + Constraint ct3 = solver.Add(x == 1); + Constraint ct4 = solver.Add(1 >= x); + Constraint ct5 = solver.Add(1 <= x); + Constraint ct6 = solver.Add(1 == x); CheckEquality(ct1.GetCoefficient(x), 1.0, "test1"); CheckEquality(ct2.GetCoefficient(x), 1.0, "test2"); CheckEquality(ct3.GetCoefficient(x), 1.0, "test3"); @@ -67,19 +67,19 @@ public class CsTestLp static void TestVarAddition() { Console.WriteLine("Running TestVarAddition"); - MPSolver solver = new MPSolver("TestVarAddition", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable x = solver.MakeNumVar(0.0, 100.0, "x"); - MPVariable y = solver.MakeNumVar(0.0, 100.0, "y"); - MPConstraint ct1 = solver.Add(x + y == 1); + Solver solver = new Solver("TestVarAddition", + Solver.CLP_LINEAR_PROGRAMMING); + Variable x = solver.MakeNumVar(0.0, 100.0, "x"); + Variable y = solver.MakeNumVar(0.0, 100.0, "y"); + Constraint ct1 = solver.Add(x + y == 1); CheckEquality(ct1.GetCoefficient(x), 1.0, "test1"); CheckEquality(ct1.GetCoefficient(y), 1.0, "test2"); - MPConstraint ct2 = solver.Add(x + x == 1); + Constraint ct2 = solver.Add(x + x == 1); CheckEquality(ct2.GetCoefficient(x), 2.0, "test3"); - MPConstraint ct3 = solver.Add(x + (y + x) == 1); + Constraint ct3 = solver.Add(x + (y + x) == 1); CheckEquality(ct3.GetCoefficient(x), 2.0, "test4"); CheckEquality(ct3.GetCoefficient(y), 1.0, "test5"); - MPConstraint ct4 = solver.Add(x + (y + x + 3) == 1); + Constraint ct4 = solver.Add(x + (y + x + 3) == 1); CheckEquality(ct4.GetCoefficient(x), 2.0, "test4"); CheckEquality(ct4.GetCoefficient(y), 1.0, "test5"); CheckEquality(ct4.Lb(), -2.0, "test6"); @@ -89,23 +89,23 @@ public class CsTestLp static void TestVarMultiplication() { Console.WriteLine("Running TestVarMultiplication"); - MPSolver solver = new MPSolver("TestVarMultiplication", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable x = solver.MakeNumVar(0.0, 100.0, "x"); - MPVariable y = solver.MakeNumVar(0.0, 100.0, "y"); - MPConstraint ct1 = solver.Add(3 * x == 1); + Solver solver = new Solver("TestVarMultiplication", + Solver.CLP_LINEAR_PROGRAMMING); + Variable x = solver.MakeNumVar(0.0, 100.0, "x"); + Variable y = solver.MakeNumVar(0.0, 100.0, "y"); + Constraint ct1 = solver.Add(3 * x == 1); CheckEquality(ct1.GetCoefficient(x), 3.0, "test1"); - MPConstraint ct2 = solver.Add(x * 3 == 1); + Constraint ct2 = solver.Add(x * 3 == 1); CheckEquality(ct2.GetCoefficient(x), 3.0, "test2"); - MPConstraint ct3 = solver.Add(x + (2 * y + 3 * x) == 1); + Constraint ct3 = solver.Add(x + (2 * y + 3 * x) == 1); CheckEquality(ct3.GetCoefficient(x), 4.0, "test3"); CheckEquality(ct3.GetCoefficient(y), 2.0, "test4"); - MPConstraint ct4 = solver.Add(x + 5 * (y + x + 3) == 1); + Constraint ct4 = solver.Add(x + 5 * (y + x + 3) == 1); CheckEquality(ct4.GetCoefficient(x), 6.0, "test5"); CheckEquality(ct4.GetCoefficient(y), 5.0, "test6"); CheckEquality(ct4.Lb(), -14.0, "test7"); CheckEquality(ct4.Ub(), -14.0, "test8"); - MPConstraint ct5 = solver.Add(x + (2 * y + x + 3) * 3 == 1); + Constraint ct5 = solver.Add(x + (2 * y + x + 3) * 3 == 1); CheckEquality(ct5.GetCoefficient(x), 4.0, "test9"); CheckEquality(ct5.GetCoefficient(y), 6.0, "test10"); CheckEquality(ct5.Lb(), -8.0, "test11"); @@ -115,19 +115,19 @@ public class CsTestLp static void TestBinaryOperations() { Console.WriteLine("Running TestBinaryOperations"); - MPSolver solver = new MPSolver("TestBinaryOperations", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable x = solver.MakeNumVar(0.0, 100.0, "x"); - MPVariable y = solver.MakeNumVar(0.0, 100.0, "y"); - MPConstraint ct1 = solver.Add(x == y); + Solver solver = new Solver("TestBinaryOperations", + Solver.CLP_LINEAR_PROGRAMMING); + Variable x = solver.MakeNumVar(0.0, 100.0, "x"); + Variable y = solver.MakeNumVar(0.0, 100.0, "y"); + Constraint ct1 = solver.Add(x == y); CheckEquality(ct1.GetCoefficient(x), 1.0, "test1"); CheckEquality(ct1.GetCoefficient(y), -1.0, "test2"); - MPConstraint ct2 = solver.Add(x == 3 * y + 5); + Constraint ct2 = solver.Add(x == 3 * y + 5); CheckEquality(ct2.GetCoefficient(x), 1.0, "test3"); CheckEquality(ct2.GetCoefficient(y), -3.0, "test4"); CheckEquality(ct2.Lb(), 5.0, "test5"); CheckEquality(ct2.Ub(), 5.0, "test6"); - MPConstraint ct3 = solver.Add(2 * x - 9 == y); + Constraint ct3 = solver.Add(2 * x - 9 == y); CheckEquality(ct3.GetCoefficient(x), 2.0, "test7"); CheckEquality(ct3.GetCoefficient(y), -1.0, "test8"); CheckEquality(ct3.Lb(), 9.0, "test9"); @@ -141,26 +141,26 @@ public class CsTestLp static void TestInequalities() { Console.WriteLine("Running TestInequalities"); - MPSolver solver = new MPSolver("TestInequalities", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable x = solver.MakeNumVar(0.0, 100.0, "x"); - MPVariable y = solver.MakeNumVar(0.0, 100.0, "y"); - MPConstraint ct1 = solver.Add(2 * (x + 3) + 5 * (y + x -1) >= 3); + Solver solver = new Solver("TestInequalities", + Solver.CLP_LINEAR_PROGRAMMING); + Variable x = solver.MakeNumVar(0.0, 100.0, "x"); + Variable y = solver.MakeNumVar(0.0, 100.0, "y"); + Constraint ct1 = solver.Add(2 * (x + 3) + 5 * (y + x -1) >= 3); CheckEquality(ct1.GetCoefficient(x), 7.0, "test1"); CheckEquality(ct1.GetCoefficient(y), 5.0, "test2"); CheckEquality(ct1.Lb(), 2.0, "test3"); CheckEquality(ct1.Ub(), double.PositiveInfinity, "test4"); - MPConstraint ct2 = solver.Add(2 * (x + 3) + 5 * (y + x -1) <= 3); + Constraint ct2 = solver.Add(2 * (x + 3) + 5 * (y + x -1) <= 3); CheckEquality(ct2.GetCoefficient(x), 7.0, "test5"); CheckEquality(ct2.GetCoefficient(y), 5.0, "test6"); CheckEquality(ct2.Lb(), double.NegativeInfinity, "test7"); CheckEquality(ct2.Ub(), 2.0, "test8"); - MPConstraint ct3 = solver.Add(2 * (x + 3) + 5 * (y + x -1) >= 3 - x - y); + Constraint ct3 = solver.Add(2 * (x + 3) + 5 * (y + x -1) >= 3 - x - y); CheckEquality(ct3.GetCoefficient(x), 8.0, "test9"); CheckEquality(ct3.GetCoefficient(y), 6.0, "test10"); CheckEquality(ct3.Lb(), 2.0, "test11"); CheckEquality(ct3.Ub(), double.PositiveInfinity, "test12"); - MPConstraint ct4 = solver.Add(2 * (x + 3) + 5 * (y + x -1) <= -x - y + 3); + Constraint ct4 = solver.Add(2 * (x + 3) + 5 * (y + x -1) <= -x - y + 3); CheckEquality(ct4.GetCoefficient(x), 8.0, "test13"); CheckEquality(ct4.GetCoefficient(y), 6.0, "test14"); CheckEquality(ct4.Lb(), double.NegativeInfinity, "test15"); @@ -170,15 +170,14 @@ public class CsTestLp static void TestSumArray() { Console.WriteLine("Running TestSumArray"); - MPSolver solver = new MPSolver("TestSumArray", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable[] x = solver.MakeBoolVarArray(10, "x"); - MPConstraint ct1 = solver.Add(x.Sum() == 3); + Solver solver = new Solver("TestSumArray", Solver.CLP_LINEAR_PROGRAMMING); + Variable[] x = solver.MakeBoolVarArray(10, "x"); + Constraint ct1 = solver.Add(x.Sum() == 3); CheckEquality(ct1.GetCoefficient(x[0]), 1.0, "test1"); - MPConstraint ct2 = solver.Add(-2 * x.Sum() == 3); + Constraint ct2 = solver.Add(-2 * x.Sum() == 3); CheckEquality(ct2.GetCoefficient(x[0]), -2.0, "test2"); LinearExpr[] array = new LinearExpr[] { x[0]+ 2.0, x[0] + 3, x[0] + 4 }; - MPConstraint ct3 = solver.Add(array.Sum() == 1); + Constraint ct3 = solver.Add(array.Sum() == 1); CheckEquality(ct3.GetCoefficient(x[0]), 3.0, "test3"); CheckEquality(ct3.Lb(), -8.0, "test4"); CheckEquality(ct3.Ub(), -8.0, "test5"); @@ -187,10 +186,9 @@ public class CsTestLp static void TestObjective() { Console.WriteLine("Running TestObjective"); - MPSolver solver = new MPSolver("TestObjective", - MPSolver.CLP_LINEAR_PROGRAMMING); - MPVariable x = solver.MakeNumVar(0.0, 100.0, "x"); - MPVariable y = solver.MakeNumVar(0.0, 100.0, "y"); + Solver solver = new Solver("TestObjective", Solver.CLP_LINEAR_PROGRAMMING); + Variable x = solver.MakeNumVar(0.0, 100.0, "x"); + Variable y = solver.MakeNumVar(0.0, 100.0, "y"); solver.Maximize(x); CheckEquality(0.0, solver.Objective().Offset(), "test1"); CheckEquality(1.0, solver.Objective().GetCoefficient(x), "test2");