diff --git a/ortools/constraint_solver/csharp/IntArrayHelper.cs b/ortools/constraint_solver/csharp/IntArrayHelper.cs index 90f2ef52a1..06d7bec676 100644 --- a/ortools/constraint_solver/csharp/IntArrayHelper.cs +++ b/ortools/constraint_solver/csharp/IntArrayHelper.cs @@ -13,20 +13,20 @@ namespace Google.OrTools.ConstraintSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - // int[] and long[] helper class. - public static class IntArrayHelper +// int[] and long[] helper class. +public static class IntArrayHelper +{ + public static IntExpr Element(this int[] array, IntExpr index) { - public static IntExpr Element(this int[] array, IntExpr index) - { - return index.solver().MakeElement(array, index.Var()); - } - public static IntExpr Element(this long[] array, IntExpr index) - { - return index.solver().MakeElement(array, index.Var()); - } + return index.solver().MakeElement(array, index.Var()); } + public static IntExpr Element(this long[] array, IntExpr index) + { + return index.solver().MakeElement(array, index.Var()); + } +} } // namespace Google.OrTools.ConstraintSolver diff --git a/ortools/constraint_solver/csharp/IntVarArrayHelper.cs b/ortools/constraint_solver/csharp/IntVarArrayHelper.cs index aaaa742182..22cde01cd4 100644 --- a/ortools/constraint_solver/csharp/IntVarArrayHelper.cs +++ b/ortools/constraint_solver/csharp/IntVarArrayHelper.cs @@ -13,381 +13,381 @@ namespace Google.OrTools.ConstraintSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - // IntVar[] helper class. - public static class IntVarArrayHelper +// IntVar[] helper class. +public static class IntVarArrayHelper +{ + // All Different + public static Constraint AllDifferent(this IntVar[] vars) { - // All Different - public static Constraint AllDifferent(this IntVar[] vars) + Solver solver = GetSolver(vars); + return solver.MakeAllDifferent(vars); + } + // Allowed assignment + public static Constraint AllowedAssignments(this IntVar[] vars, IntTupleSet tuples) + { + Solver solver = GetSolver(vars); + return solver.MakeAllowedAssignments(vars, tuples); + } + // sum of all vars. + public static IntExpr Sum(this IntVar[] vars) + { + Solver solver = GetSolver(vars); + return solver.MakeSum(vars); + } + // sum of all constraints. + public static IntExpr Sum(this IConstraintWithStatus[] cts) + { + Solver solver = GetSolver(cts); + IntVar[] vars = new IntVar[cts.Length]; + for (int i = 0; i < cts.Length; ++i) { - Solver solver = GetSolver(vars); - return solver.MakeAllDifferent(vars); + vars[i] = cts[i].Var(); } - // Allowed assignment - public static Constraint AllowedAssignments(this IntVar[] vars, IntTupleSet tuples) + return solver.MakeSum(vars); + } + public static IntExpr Sum(this IntExpr[] exprs) + { + Solver solver = GetSolver(exprs); + IntVar[] vars = new IntVar[exprs.Length]; + for (int i = 0; i < exprs.Length; ++i) { - Solver solver = GetSolver(vars); - return solver.MakeAllowedAssignments(vars, tuples); - } - // sum of all vars. - public static IntExpr Sum(this IntVar[] vars) - { - Solver solver = GetSolver(vars); - return solver.MakeSum(vars); - } - // sum of all constraints. - public static IntExpr Sum(this IConstraintWithStatus[] cts) - { - Solver solver = GetSolver(cts); - IntVar[] vars = new IntVar[cts.Length]; - for (int i = 0; i < cts.Length; ++i) - { - vars[i] = cts[i].Var(); - } - return solver.MakeSum(vars); - } - public static IntExpr Sum(this IntExpr[] exprs) - { - Solver solver = GetSolver(exprs); - IntVar[] vars = new IntVar[exprs.Length]; - for (int i = 0; i < exprs.Length; ++i) - { - vars[i] = exprs[i].Var(); - } - return solver.MakeSum(vars); - } - - // scalar product - public static IntExpr ScalProd(this IntVar[] vars, long[] coefs) - { - Solver solver = GetSolver(vars); - return solver.MakeScalProd(vars, coefs); - } - - // scalar product - public static IntExpr ScalProd(this IntVar[] vars, int[] coefs) - { - Solver solver = GetSolver(vars); - return solver.MakeScalProd(vars, coefs); - } - - // get solver from array of integer variables - private static Solver GetSolver(IntVar[] vars) - { - if (vars == null || vars.Length <= 0) - throw new ArgumentException("Array cannot be null or empty"); - - return vars[0].solver(); - } - // get solver from array of integer expressions - private static Solver GetSolver(IntExpr[] expressions) - { - if (expressions == null || expressions.Length <= 0) - throw new ArgumentException("Array cannot be null or empty"); - - return expressions[0].solver(); - } - private static Solver GetSolver(IConstraintWithStatus[] cts) - { - if (cts == null || cts.Length <= 0) - throw new ArgumentException("Array cannot be null or empty"); - - return cts[0].solver(); - } - public static IntExpr Element(this IntVar[] array, IntExpr index) - { - return index.solver().MakeElement(array, index.Var()); - } - // min of all vars. - public static IntExpr Min(this IntVar[] vars) - { - Solver solver = GetSolver(vars); - return solver.MakeMin(vars); - } - // min of all vars. - public static IntExpr Max(this IntVar[] vars) - { - Solver solver = GetSolver(vars); - return solver.MakeMax(vars); - } - // count of all vars. - public static Constraint Count(this IntVar[] vars, long value, long count) - { - Solver solver = GetSolver(vars); - return solver.MakeCount(vars, value, count); - } - // count of all vars. - public static Constraint Count(this IntVar[] vars, long value, IntExpr count) - { - Solver solver = GetSolver(vars); - return solver.MakeCount(vars, value, count.Var()); - } - public static Constraint Distribute(this IntVar[] vars, long[] values, IntVar[] cards) - { - Solver solver = GetSolver(vars); - return solver.MakeDistribute(vars, values, cards); - } - public static Constraint Distribute(this IntVar[] vars, int[] values, IntVar[] cards) - { - Solver solver = GetSolver(vars); - return solver.MakeDistribute(vars, values, cards); - } - public static Constraint Distribute(this IntVar[] vars, IntVar[] cards) - { - Solver solver = GetSolver(vars); - return solver.MakeDistribute(vars, cards); - } - public static Constraint Distribute(this IntVar[] vars, long card_min, long card_max, long card_size) - { - Solver solver = GetSolver(vars); - return solver.MakeDistribute(vars, card_min, card_max, card_size); - } - public static Constraint Transition(this IntVar[] vars, IntTupleSet transitions, long initial_state, - long[] final_states) - { - Solver solver = GetSolver(vars); - return solver.MakeTransitionConstraint(vars, transitions, initial_state, final_states); - } - public static Constraint Transition(this IntVar[] vars, IntTupleSet transitions, long initial_state, - int[] final_states) - { - Solver solver = GetSolver(vars); - return solver.MakeTransitionConstraint(vars, transitions, initial_state, final_states); - } - - // Matrix API - public static IntVar[] Flatten(this IntVar[,] vars) - { - int rows = vars.GetLength(0); - int cols = vars.GetLength(1); - IntVar[] flat = new IntVar[cols * rows]; - for (int i = 0; i < rows; i++) - { - for (int j = 0; j < cols; j++) - { - flat[i * cols + j] = vars[i, j]; - } - } - return flat; + vars[i] = exprs[i].Var(); } + return solver.MakeSum(vars); } - // TODO(user): Try to move this code back to the .swig with @define macros. - public partial class IntVarVector : IDisposable, - System.Collections.IEnumerable + // scalar product + public static IntExpr ScalProd(this IntVar[] vars, long[] coefs) + { + Solver solver = GetSolver(vars); + return solver.MakeScalProd(vars, coefs); + } + + // scalar product + public static IntExpr ScalProd(this IntVar[] vars, int[] coefs) + { + Solver solver = GetSolver(vars); + return solver.MakeScalProd(vars, coefs); + } + + // get solver from array of integer variables + private static Solver GetSolver(IntVar[] vars) + { + if (vars == null || vars.Length <= 0) + throw new ArgumentException("Array cannot be null or empty"); + + return vars[0].solver(); + } + // get solver from array of integer expressions + private static Solver GetSolver(IntExpr[] expressions) + { + if (expressions == null || expressions.Length <= 0) + throw new ArgumentException("Array cannot be null or empty"); + + return expressions[0].solver(); + } + private static Solver GetSolver(IConstraintWithStatus[] cts) + { + if (cts == null || cts.Length <= 0) + throw new ArgumentException("Array cannot be null or empty"); + + return cts[0].solver(); + } + public static IntExpr Element(this IntVar[] array, IntExpr index) + { + return index.solver().MakeElement(array, index.Var()); + } + // min of all vars. + public static IntExpr Min(this IntVar[] vars) + { + Solver solver = GetSolver(vars); + return solver.MakeMin(vars); + } + // min of all vars. + public static IntExpr Max(this IntVar[] vars) + { + Solver solver = GetSolver(vars); + return solver.MakeMax(vars); + } + // count of all vars. + public static Constraint Count(this IntVar[] vars, long value, long count) + { + Solver solver = GetSolver(vars); + return solver.MakeCount(vars, value, count); + } + // count of all vars. + public static Constraint Count(this IntVar[] vars, long value, IntExpr count) + { + Solver solver = GetSolver(vars); + return solver.MakeCount(vars, value, count.Var()); + } + public static Constraint Distribute(this IntVar[] vars, long[] values, IntVar[] cards) + { + Solver solver = GetSolver(vars); + return solver.MakeDistribute(vars, values, cards); + } + public static Constraint Distribute(this IntVar[] vars, int[] values, IntVar[] cards) + { + Solver solver = GetSolver(vars); + return solver.MakeDistribute(vars, values, cards); + } + public static Constraint Distribute(this IntVar[] vars, IntVar[] cards) + { + Solver solver = GetSolver(vars); + return solver.MakeDistribute(vars, cards); + } + public static Constraint Distribute(this IntVar[] vars, long card_min, long card_max, long card_size) + { + Solver solver = GetSolver(vars); + return solver.MakeDistribute(vars, card_min, card_max, card_size); + } + public static Constraint Transition(this IntVar[] vars, IntTupleSet transitions, long initial_state, + long[] final_states) + { + Solver solver = GetSolver(vars); + return solver.MakeTransitionConstraint(vars, transitions, initial_state, final_states); + } + public static Constraint Transition(this IntVar[] vars, IntTupleSet transitions, long initial_state, + int[] final_states) + { + Solver solver = GetSolver(vars); + return solver.MakeTransitionConstraint(vars, transitions, initial_state, final_states); + } + + // Matrix API + public static IntVar[] Flatten(this IntVar[,] vars) + { + int rows = vars.GetLength(0); + int cols = vars.GetLength(1); + IntVar[] flat = new IntVar[cols * rows]; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + flat[i * cols + j] = vars[i, j]; + } + } + return flat; + } +} + +// TODO(user): Try to move this code back to the .swig with @define macros. +public partial class IntVarVector : IDisposable, + System.Collections.IEnumerable #if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList + , + System.Collections.Generic.IList #endif +{ + // cast from C# IntVar array + public static implicit operator IntVarVector(IntVar[] inVal) { - // cast from C# IntVar array - public static implicit operator IntVarVector(IntVar[] inVal) + var outVal = new IntVarVector(); + foreach (IntVar element in inVal) { - var outVal = new IntVarVector(); - foreach (IntVar element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# IntVar array - public static implicit operator IntVar[](IntVarVector inVal) - { - var outVal = new IntVar[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; + outVal.Add(element); } + return outVal; } - public partial class SearchMonitorVector : IDisposable, + // cast to C# IntVar array + public static implicit operator IntVar[](IntVarVector inVal) + { + var outVal = new IntVar[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + +public partial class SearchMonitorVector : IDisposable, + System.Collections.IEnumerable +#if !SWIG_DOTNET_1 + , + System.Collections.Generic.IList +#endif +{ + // cast from C# SearchMonitor array + public static implicit operator SearchMonitorVector(SearchMonitor[] inVal) + { + var outVal = new SearchMonitorVector(); + foreach (SearchMonitor element in inVal) + { + outVal.Add(element); + } + return outVal; + } + + // cast to C# SearchMonitor array + public static implicit operator SearchMonitor[](SearchMonitorVector inVal) + { + var outVal = new SearchMonitor[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + +public partial class DecisionBuilderVector : IDisposable, + System.Collections.IEnumerable +#if !SWIG_DOTNET_1 + , + System.Collections.Generic.IList +#endif +{ + // cast from C# DecisionBuilder array + public static implicit operator DecisionBuilderVector(DecisionBuilder[] inVal) + { + var outVal = new DecisionBuilderVector(); + foreach (DecisionBuilder element in inVal) + { + outVal.Add(element); + } + return outVal; + } + + // cast to C# DecisionBuilder array + public static implicit operator DecisionBuilder[](DecisionBuilderVector inVal) + { + var outVal = new DecisionBuilder[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + +public partial class IntervalVarVector : IDisposable, + System.Collections.IEnumerable +#if !SWIG_DOTNET_1 + , + System.Collections.Generic.IList +#endif +{ + // cast from C# IntervalVar array + public static implicit operator IntervalVarVector(IntervalVar[] inVal) + { + var outVal = new IntervalVarVector(); + foreach (IntervalVar element in inVal) + { + outVal.Add(element); + } + return outVal; + } + + // cast to C# IntervalVar array + public static implicit operator IntervalVar[](IntervalVarVector inVal) + { + var outVal = new IntervalVar[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + +public partial class SequenceVarVector : IDisposable, + System.Collections.IEnumerable +#if !SWIG_DOTNET_1 + , + System.Collections.Generic.IList +#endif +{ + // cast from C# SequenceVar array + public static implicit operator SequenceVarVector(SequenceVar[] inVal) + { + var outVal = new SequenceVarVector(); + foreach (SequenceVar element in inVal) + { + outVal.Add(element); + } + return outVal; + } + + // cast to C# SequenceVar array + public static implicit operator SequenceVar[](SequenceVarVector inVal) + { + var outVal = new SequenceVar[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + +public partial class LocalSearchOperatorVector : IDisposable, + System.Collections.IEnumerable +#if !SWIG_DOTNET_1 + , + System.Collections.Generic.IList +#endif +{ + // cast from C# LocalSearchOperator array + public static implicit operator LocalSearchOperatorVector(LocalSearchOperator[] inVal) + { + var outVal = new LocalSearchOperatorVector(); + foreach (LocalSearchOperator element in inVal) + { + outVal.Add(element); + } + return outVal; + } + + // cast to C# LocalSearchOperator array + public static implicit operator LocalSearchOperator[](LocalSearchOperatorVector inVal) + { + var outVal = new LocalSearchOperator[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + +public partial class LocalSearchFilterVector : IDisposable, System.Collections.IEnumerable #if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList + , + System.Collections.Generic.IList #endif +{ + // cast from C# LocalSearchFilter array + public static implicit operator LocalSearchFilterVector(LocalSearchFilter[] inVal) { - // cast from C# SearchMonitor array - public static implicit operator SearchMonitorVector(SearchMonitor[] inVal) + var outVal = new LocalSearchFilterVector(); + foreach (LocalSearchFilter element in inVal) { - var outVal = new SearchMonitorVector(); - foreach (SearchMonitor element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# SearchMonitor array - public static implicit operator SearchMonitor[](SearchMonitorVector inVal) - { - var outVal = new SearchMonitor[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; + outVal.Add(element); } + return outVal; } - public partial class DecisionBuilderVector : IDisposable, - System.Collections.IEnumerable -#if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList -#endif + // cast to C# LocalSearchFilter array + public static implicit operator LocalSearchFilter[](LocalSearchFilterVector inVal) { - // cast from C# DecisionBuilder array - public static implicit operator DecisionBuilderVector(DecisionBuilder[] inVal) - { - var outVal = new DecisionBuilderVector(); - foreach (DecisionBuilder element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# DecisionBuilder array - public static implicit operator DecisionBuilder[](DecisionBuilderVector inVal) - { - var outVal = new DecisionBuilder[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; - } + var outVal = new LocalSearchFilter[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; } +} - public partial class IntervalVarVector : IDisposable, +public partial class SymmetryBreakerVector : IDisposable, System.Collections.IEnumerable #if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList + , + System.Collections.Generic.IList #endif +{ + // cast from C# SymmetryBreaker array + public static implicit operator SymmetryBreakerVector(SymmetryBreaker[] inVal) { - // cast from C# IntervalVar array - public static implicit operator IntervalVarVector(IntervalVar[] inVal) + var outVal = new SymmetryBreakerVector(); + foreach (SymmetryBreaker element in inVal) { - var outVal = new IntervalVarVector(); - foreach (IntervalVar element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# IntervalVar array - public static implicit operator IntervalVar[](IntervalVarVector inVal) - { - var outVal = new IntervalVar[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; + outVal.Add(element); } + return outVal; } - public partial class SequenceVarVector : IDisposable, - System.Collections.IEnumerable -#if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList -#endif + // cast to C# SymmetryBreaker array + public static implicit operator SymmetryBreaker[](SymmetryBreakerVector inVal) { - // cast from C# SequenceVar array - public static implicit operator SequenceVarVector(SequenceVar[] inVal) - { - var outVal = new SequenceVarVector(); - foreach (SequenceVar element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# SequenceVar array - public static implicit operator SequenceVar[](SequenceVarVector inVal) - { - var outVal = new SequenceVar[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; - } - } - - public partial class LocalSearchOperatorVector : IDisposable, - System.Collections.IEnumerable -#if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList -#endif - { - // cast from C# LocalSearchOperator array - public static implicit operator LocalSearchOperatorVector(LocalSearchOperator[] inVal) - { - var outVal = new LocalSearchOperatorVector(); - foreach (LocalSearchOperator element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# LocalSearchOperator array - public static implicit operator LocalSearchOperator[](LocalSearchOperatorVector inVal) - { - var outVal = new LocalSearchOperator[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; - } - } - - public partial class LocalSearchFilterVector : IDisposable, - System.Collections.IEnumerable -#if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList -#endif - { - // cast from C# LocalSearchFilter array - public static implicit operator LocalSearchFilterVector(LocalSearchFilter[] inVal) - { - var outVal = new LocalSearchFilterVector(); - foreach (LocalSearchFilter element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# LocalSearchFilter array - public static implicit operator LocalSearchFilter[](LocalSearchFilterVector inVal) - { - var outVal = new LocalSearchFilter[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; - } - } - - public partial class SymmetryBreakerVector : IDisposable, - System.Collections.IEnumerable -#if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList -#endif - { - // cast from C# SymmetryBreaker array - public static implicit operator SymmetryBreakerVector(SymmetryBreaker[] inVal) - { - var outVal = new SymmetryBreakerVector(); - foreach (SymmetryBreaker element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# SymmetryBreaker array - public static implicit operator SymmetryBreaker[](SymmetryBreakerVector inVal) - { - var outVal = new SymmetryBreaker[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; - } + var outVal = new SymmetryBreaker[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; } +} } // namespace Google.OrTools.ConstraintSolver diff --git a/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs b/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs index ffe8906fb4..0c39c49092 100644 --- a/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs +++ b/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs @@ -13,34 +13,34 @@ namespace Google.OrTools.ConstraintSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - // IntervalVar[] helper class. - public static class IntervalVarArrayHelper +// IntervalVar[] helper class. +public static class IntervalVarArrayHelper +{ + // get solver from array of interval variables + private static Solver GetSolver(IntervalVar[] vars) { - // get solver from array of interval variables - private static Solver GetSolver(IntervalVar[] vars) - { - if (vars == null || vars.Length <= 0) - throw new ArgumentException("Array cannot be null or empty"); + if (vars == null || vars.Length <= 0) + throw new ArgumentException("Array cannot be null or empty"); - return vars[0].solver(); - } - public static DisjunctiveConstraint Disjunctive(this IntervalVar[] vars, String name) - { - Solver solver = GetSolver(vars); - return solver.MakeDisjunctiveConstraint(vars, name); - } - public static Constraint Cumulative(this IntervalVar[] vars, long[] demands, long capacity, String name) - { - Solver solver = GetSolver(vars); - return solver.MakeCumulative(vars, demands, capacity, name); - } - public static Constraint Cumulative(this IntervalVar[] vars, int[] demands, long capacity, String name) - { - Solver solver = GetSolver(vars); - return solver.MakeCumulative(vars, demands, capacity, name); - } + return vars[0].solver(); } + public static DisjunctiveConstraint Disjunctive(this IntervalVar[] vars, String name) + { + Solver solver = GetSolver(vars); + return solver.MakeDisjunctiveConstraint(vars, name); + } + public static Constraint Cumulative(this IntervalVar[] vars, long[] demands, long capacity, String name) + { + Solver solver = GetSolver(vars); + return solver.MakeCumulative(vars, demands, capacity, name); + } + public static Constraint Cumulative(this IntervalVar[] vars, int[] demands, long capacity, String name) + { + Solver solver = GetSolver(vars); + return solver.MakeCumulative(vars, demands, capacity, name); + } +} } // namespace Google.OrTools.ConstraintSolver diff --git a/ortools/constraint_solver/csharp/NetDecisionBuilder.cs b/ortools/constraint_solver/csharp/NetDecisionBuilder.cs index 67f120164f..81721a3081 100644 --- a/ortools/constraint_solver/csharp/NetDecisionBuilder.cs +++ b/ortools/constraint_solver/csharp/NetDecisionBuilder.cs @@ -15,220 +15,220 @@ using System; using System.Collections; namespace Google.OrTools.ConstraintSolver +{ +/** + * This class acts as a intermediate step between a c++ decision builder and a + * .Net one. Its main purpose is to catch the .Net application exception + * launched when a failure occurs during the Next() call, and to return + * silently a System.ApplicationException that will propagate the failure back + * to the C++ code. + * + */ +public class NetDecisionBuilder : DecisionBuilder { /** - * This class acts as a intermediate step between a c++ decision builder and a - * .Net one. Its main purpose is to catch the .Net application exception - * launched when a failure occurs during the Next() call, and to return - * silently a System.ApplicationException that will propagate the failure back - * to the C++ code. - * + * This methods wraps the calls to next() and catches fail exceptions. + * It currently catches all application exceptions. */ - public class NetDecisionBuilder : DecisionBuilder + public override Decision NextWrapper(Solver solver) { - /** - * This methods wraps the calls to next() and catches fail exceptions. - * It currently catches all application exceptions. - */ - public override Decision NextWrapper(Solver solver) + try { - try - { - return Next(solver); - } - catch (ApplicationException /*e*/) - { - // TODO(user): Catch only fail exceptions. - return solver.MakeFailDecision(); - } + return Next(solver); } - /** - * This is the new method to subclass when defining a .Net decision builder. - */ - public virtual Decision Next(Solver solver) + catch (ApplicationException /*e*/) { - return null; + // TODO(user): Catch only fail exceptions. + return solver.MakeFailDecision(); } } - /** - * This class acts as a intermediate step between a c++ decision and a - * .Net one. Its main purpose is to catch the .Net application - * exception launched when a failure occurs during the - * Apply()/Refute() calls, and to set the ShouldFail() flag on the - * solver that will propagate the failure back to the C++ code. - * + * This is the new method to subclass when defining a .Net decision builder. */ - public class NetDecision : Decision + public virtual Decision Next(Solver solver) { - /** - * This methods wraps the calls to Apply() and catches fail exceptions. - * It currently catches all application exceptions. - */ - public override void ApplyWrapper(Solver solver) - { - try - { - Apply(solver); - } - catch (ApplicationException /*e*/) - { - // TODO(user): Catch only fail exceptions. - solver.ShouldFail(); - } - } - /** - * This is a new method to subclass when defining a .Net decision. - */ - public virtual void Apply(Solver solver) - { - // By default, do nothing - } + return null; + } +} - public override void RefuteWrapper(Solver solver) +/** + * This class acts as a intermediate step between a c++ decision and a + * .Net one. Its main purpose is to catch the .Net application + * exception launched when a failure occurs during the + * Apply()/Refute() calls, and to set the ShouldFail() flag on the + * solver that will propagate the failure back to the C++ code. + * + */ +public class NetDecision : Decision +{ + /** + * This methods wraps the calls to Apply() and catches fail exceptions. + * It currently catches all application exceptions. + */ + public override void ApplyWrapper(Solver solver) + { + try { - try - { - Refute(solver); - } - catch (ApplicationException /*e*/) - { - // TODO(user): Catch only fail exceptions. - solver.ShouldFail(); - } + Apply(solver); } - /** - * This is a new method to subclass when defining a .Net decision. - */ - public virtual void Refute(Solver solver) + catch (ApplicationException /*e*/) { + // TODO(user): Catch only fail exceptions. + solver.ShouldFail(); + } + } + /** + * This is a new method to subclass when defining a .Net decision. + */ + public virtual void Apply(Solver solver) + { + // By default, do nothing + } + + public override void RefuteWrapper(Solver solver) + { + try + { + Refute(solver); + } + catch (ApplicationException /*e*/) + { + // TODO(user): Catch only fail exceptions. + solver.ShouldFail(); + } + } + /** + * This is a new method to subclass when defining a .Net decision. + */ + public virtual void Refute(Solver solver) + { + } +} + +public class NetDemon : Demon +{ + /** + * This methods wraps the calls to next() and catches fail exceptions. + */ + public override void RunWrapper(Solver solver) + { + try + { + Run(solver); + } + catch (ApplicationException /*e*/) + { + // TODO(user): Check that this is indeed a fail. Try implementing + // custom exceptions (hard). + solver.ShouldFail(); + } + } + /** + * This is the new method to subclass when defining a .Net decision builder. + */ + public virtual void Run(Solver solver) + { + } + public override int Priority() + { + return Solver.NORMAL_PRIORITY; + } + public override string ToString() + { + return "NetDemon"; + } +} + +public class NetConstraint : Constraint +{ + public NetConstraint(Solver s) : base(s) + { + } + + public override void InitialPropagateWrapper() + { + try + { + InitialPropagate(); + } + catch (ApplicationException /*e*/) + { + solver().ShouldFail(); + } + } + public virtual void InitialPropagate() + { + } + public override string ToString() + { + return "NetConstraint"; + } +} + +public class IntVarEnumerator : IEnumerator +{ + private IntVarIterator iterator_; + + // Enumerators are positioned before the first element + // until the first MoveNext() call. + private bool first_ = true; + + public IntVarEnumerator(IntVarIterator iterator) + { + iterator_ = iterator; + } + + public bool MoveNext() + { + if (first_) + { + iterator_.Init(); + first_ = false; + } + else + { + iterator_.Next(); + } + return iterator_.Ok(); + } + + public void Reset() + { + first_ = true; + } + + object IEnumerator.Current + { + get { + return Current; } } - public class NetDemon : Demon + public long Current { - /** - * This methods wraps the calls to next() and catches fail exceptions. - */ - public override void RunWrapper(Solver solver) - { - try + get { + if (!first_ && iterator_.Ok()) { - Run(solver); - } - catch (ApplicationException /*e*/) - { - // TODO(user): Check that this is indeed a fail. Try implementing - // custom exceptions (hard). - solver.ShouldFail(); - } - } - /** - * This is the new method to subclass when defining a .Net decision builder. - */ - public virtual void Run(Solver solver) - { - } - public override int Priority() - { - return Solver.NORMAL_PRIORITY; - } - public override string ToString() - { - return "NetDemon"; - } - } - - public class NetConstraint : Constraint - { - public NetConstraint(Solver s) : base(s) - { - } - - public override void InitialPropagateWrapper() - { - try - { - InitialPropagate(); - } - catch (ApplicationException /*e*/) - { - solver().ShouldFail(); - } - } - public virtual void InitialPropagate() - { - } - public override string ToString() - { - return "NetConstraint"; - } - } - - public class IntVarEnumerator : IEnumerator - { - private IntVarIterator iterator_; - - // Enumerators are positioned before the first element - // until the first MoveNext() call. - private bool first_ = true; - - public IntVarEnumerator(IntVarIterator iterator) - { - iterator_ = iterator; - } - - public bool MoveNext() - { - if (first_) - { - iterator_.Init(); - first_ = false; + return iterator_.Value(); } else { - iterator_.Next(); - } - return iterator_.Ok(); - } - - public void Reset() - { - first_ = true; - } - - object IEnumerator.Current - { - get { - return Current; - } - } - - public long Current - { - get { - if (!first_ && iterator_.Ok()) - { - return iterator_.Value(); - } - else - { - throw new InvalidOperationException(); - } + throw new InvalidOperationException(); } } } +} - public partial class IntVarIterator : BaseObject, IEnumerable +public partial class IntVarIterator : BaseObject, IEnumerable +{ + IEnumerator IEnumerable.GetEnumerator() { - IEnumerator IEnumerable.GetEnumerator() - { - return (IEnumerator)GetEnumerator(); - } - - public IntVarEnumerator GetEnumerator() - { - return new IntVarEnumerator(this); - } + return (IEnumerator)GetEnumerator(); } + + public IntVarEnumerator GetEnumerator() + { + return new IntVarEnumerator(this); + } +} } // namespace Google.OrTools.ConstraintSolver diff --git a/ortools/constraint_solver/csharp/SolverHelper.cs b/ortools/constraint_solver/csharp/SolverHelper.cs index 823cd8ad46..e2559c96ff 100644 --- a/ortools/constraint_solver/csharp/SolverHelper.cs +++ b/ortools/constraint_solver/csharp/SolverHelper.cs @@ -13,574 +13,574 @@ namespace Google.OrTools.ConstraintSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - public partial class Solver : IDisposable +public partial class Solver : IDisposable +{ + public IntVar[] MakeIntVarArray(int count, long min, long max) { - public IntVar[] MakeIntVarArray(int count, long min, long max) + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeIntVar(min, max); - } - return array; + array[i] = MakeIntVar(min, max); } - - public IntVar[] MakeIntVarArray(int count, long min, long max, string name) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - string var_name = name + i; - array[i] = MakeIntVar(min, max, var_name); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, long[] values) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeIntVar(values); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, long[] values, string name) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - string var_name = name + i; - array[i] = MakeIntVar(values, var_name); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, int[] values) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeIntVar(values); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, int[] values, string name) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - string var_name = name + i; - array[i] = MakeIntVar(values, var_name); - } - return array; - } - - public IntVar[] MakeBoolVarArray(int count) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeBoolVar(); - } - return array; - } - - public IntVar[] MakeBoolVarArray(int count, string name) - { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) - { - string var_name = name + i; - array[i] = MakeBoolVar(var_name); - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - array[i, j] = MakeIntVar(min, max); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max, string name) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - array[i, j] = MakeIntVar(min, max, var_name); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - array[i, j] = MakeIntVar(values); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values, string name) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - array[i, j] = MakeIntVar(values, var_name); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - array[i, j] = MakeIntVar(values); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values, string name) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - array[i, j] = MakeIntVar(values, var_name); - } - } - return array; - } - - public IntVar[,] MakeBoolVarMatrix(int rows, int cols) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - array[i, j] = MakeBoolVar(); - } - } - return array; - } - - public IntVar[,] MakeBoolVarMatrix(int rows, int cols, string name) - { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - array[i, j] = MakeBoolVar(var_name); - } - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, - bool optional) - { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeFixedDurationIntervalVar(start_min, start_max, duration, optional, ""); - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, - bool optional, string name) - { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeFixedDurationIntervalVar(start_min, start_max, duration, optional, name + i); - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long[] start_min, long[] start_max, - long[] duration, bool optional, string name) - { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeFixedDurationIntervalVar(start_min[i], start_max[i], duration[i], optional, name + i); - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, int[] start_min, int[] start_max, - int[] duration, bool optional, string name) - { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeFixedDurationIntervalVar(start_min[i], start_max[i], duration[i], optional, name + i); - } - return array; - } - public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, int[] durations, string name) - { - int count = starts.Length; - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeFixedDurationIntervalVar(starts[i], durations[i], name + i); - } - return array; - } - public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, long[] durations, string name) - { - int count = starts.Length; - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeFixedDurationIntervalVar(starts[i], durations[i], name + i); - } - return array; - } - public void NewSearch(DecisionBuilder db) - { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - NewSearchAux(db); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor sm1) - { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - NewSearchAux(db, sm1); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2) - { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - pinned_search_monitors_.Add(sm2); - NewSearchAux(db, sm1, sm2); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2, SearchMonitor sm3) - { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - pinned_search_monitors_.Add(sm2); - pinned_search_monitors_.Add(sm3); - NewSearchAux(db, sm1, sm2, sm3); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2, SearchMonitor sm3, - SearchMonitor sm4) - { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - pinned_search_monitors_.Add(sm2); - pinned_search_monitors_.Add(sm3); - pinned_search_monitors_.Add(sm4); - NewSearchAux(db, sm1, sm2, sm3, sm4); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor[] monitors) - { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.AddRange(monitors); - NewSearchAux(db, monitors); - } - - public void EndSearch() - { - pinned_decision_builder_ = null; - pinned_search_monitors_.Clear(); - EndSearchAux(); - } - - private System.Collections.Generic.List pinned_search_monitors_ = - new System.Collections.Generic.List(); - private DecisionBuilder pinned_decision_builder_; + return array; } - public partial class IntExpr : PropagationBaseObject + public IntVar[] MakeIntVarArray(int count, long min, long max, string name) { - public static IntExpr operator +(IntExpr a, IntExpr b) + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) { - return a.solver().MakeSum(a, b); - } - public static IntExpr operator +(IntExpr a, long v) - { - return a.solver().MakeSum(a, v); - } - public static IntExpr operator +(long v, IntExpr a) - { - return a.solver().MakeSum(a, v); - } - public static IntExpr operator -(IntExpr a, IntExpr b) - { - return a.solver().MakeDifference(a, b); - } - public static IntExpr operator -(IntExpr a, long v) - { - return a.solver().MakeSum(a, -v); - } - public static IntExpr operator -(long v, IntExpr a) - { - return a.solver().MakeDifference(v, a); - } - public static IntExpr operator *(IntExpr a, IntExpr b) - { - return a.solver().MakeProd(a, b); - } - public static IntExpr operator *(IntExpr a, long v) - { - return a.solver().MakeProd(a, v); - } - public static IntExpr operator *(long v, IntExpr a) - { - return a.solver().MakeProd(a, v); - } - public static IntExpr operator /(IntExpr a, long v) - { - return a.solver().MakeDiv(a, v); - } - public static IntExpr operator %(IntExpr a, long v) - { - return a.solver().MakeModulo(a, v); - } - public static IntExpr operator -(IntExpr a) - { - return a.solver().MakeOpposite(a); - } - public IntExpr Abs() - { - return this.solver().MakeAbs(this); - } - public IntExpr Square() - { - return this.solver().MakeSquare(this); - } - public static IntExprEquality operator ==(IntExpr a, IntExpr b) - { - return new IntExprEquality(a, b, true); - } - public static IntExprEquality operator !=(IntExpr a, IntExpr b) - { - return new IntExprEquality(a, b, false); - } - public static WrappedConstraint operator ==(IntExpr a, long v) - { - return new WrappedConstraint(a.solver().MakeEquality(a, v)); - } - public static WrappedConstraint operator !=(IntExpr a, long v) - { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator >=(IntExpr a, long v) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a, v)); - } - public static WrappedConstraint operator>(IntExpr a, long v) - { - return new WrappedConstraint(a.solver().MakeGreater(a, v)); - } - public static WrappedConstraint operator <=(IntExpr a, long v) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a, v)); - } - public static WrappedConstraint operator<(IntExpr a, long v) - { - return new WrappedConstraint(a.solver().MakeLess(a, v)); - } - public static WrappedConstraint operator >=(IntExpr a, IntExpr b) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator>(IntExpr a, IntExpr b) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); - } - public static WrappedConstraint operator <=(IntExpr a, IntExpr b) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator<(IntExpr a, IntExpr b) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); + string var_name = name + i; + array[i] = MakeIntVar(min, max, var_name); } + return array; } - public partial class Constraint : PropagationBaseObject, IConstraintWithStatus + public IntVar[] MakeIntVarArray(int count, long[] values) { - public static implicit operator IntVar(Constraint eq) + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) { - return eq.Var(); - } - - public static implicit operator IntExpr(Constraint eq) - { - return eq.Var(); - } - public static IntExpr operator +(Constraint a, Constraint b) - { - return a.solver().MakeSum(a.Var(), b.Var()); - } - public static IntExpr operator +(Constraint a, long v) - { - return a.solver().MakeSum(a.Var(), v); - } - public static IntExpr operator +(long v, Constraint a) - { - return a.solver().MakeSum(a.Var(), v); - } - public static IntExpr operator -(Constraint a, Constraint b) - { - return a.solver().MakeDifference(a.Var(), b.Var()); - } - public static IntExpr operator -(Constraint a, long v) - { - return a.solver().MakeSum(a.Var(), -v); - } - public static IntExpr operator -(long v, Constraint a) - { - return a.solver().MakeDifference(v, a.Var()); - } - public static IntExpr operator *(Constraint a, Constraint b) - { - return a.solver().MakeProd(a.Var(), b.Var()); - } - public static IntExpr operator *(Constraint a, long v) - { - return a.solver().MakeProd(a.Var(), v); - } - public static IntExpr operator *(long v, Constraint a) - { - return a.solver().MakeProd(a.Var(), v); - } - public static IntExpr operator /(Constraint a, long v) - { - return a.solver().MakeDiv(a.Var(), v); - } - public static IntExpr operator -(Constraint a) - { - return a.solver().MakeOpposite(a.Var()); - } - public IntExpr Abs() - { - return this.solver().MakeAbs(this.Var()); - } - public IntExpr Square() - { - return this.solver().MakeSquare(this.Var()); - } - public static WrappedConstraint operator ==(Constraint a, long v) - { - return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); - } - public static WrappedConstraint operator ==(long v, Constraint a) - { - return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); - } - public static WrappedConstraint operator !=(Constraint a, long v) - { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator !=(long v, Constraint a) - { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator >=(Constraint a, long v) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator >=(long v, Constraint a) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator>(Constraint a, long v) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); - } - public static WrappedConstraint operator>(long v, Constraint a) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); - } - public static WrappedConstraint operator <=(Constraint a, long v) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator <=(long v, Constraint a) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator<(Constraint a, long v) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); - } - public static WrappedConstraint operator<(long v, Constraint a) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); - } - public static WrappedConstraint operator >=(Constraint a, Constraint b) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator>(Constraint a, Constraint b) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); - } - public static WrappedConstraint operator <=(Constraint a, Constraint b) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator<(Constraint a, Constraint b) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); - } - public static ConstraintEquality operator ==(Constraint a, Constraint b) - { - return new ConstraintEquality(a, b, true); - } - public static ConstraintEquality operator !=(Constraint a, Constraint b) - { - return new ConstraintEquality(a, b, false); + array[i] = MakeIntVar(values); } + return array; } + + public IntVar[] MakeIntVarArray(int count, long[] values, string name) + { + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) + { + string var_name = name + i; + array[i] = MakeIntVar(values, var_name); + } + return array; + } + + public IntVar[] MakeIntVarArray(int count, int[] values) + { + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeIntVar(values); + } + return array; + } + + public IntVar[] MakeIntVarArray(int count, int[] values, string name) + { + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) + { + string var_name = name + i; + array[i] = MakeIntVar(values, var_name); + } + return array; + } + + public IntVar[] MakeBoolVarArray(int count) + { + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeBoolVar(); + } + return array; + } + + public IntVar[] MakeBoolVarArray(int count, string name) + { + IntVar[] array = new IntVar[count]; + for (int i = 0; i < count; ++i) + { + string var_name = name + i; + array[i] = MakeBoolVar(var_name); + } + return array; + } + + public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + array[i, j] = MakeIntVar(min, max); + } + } + return array; + } + + public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max, string name) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + array[i, j] = MakeIntVar(min, max, var_name); + } + } + return array; + } + + public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + array[i, j] = MakeIntVar(values); + } + } + return array; + } + + public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values, string name) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + array[i, j] = MakeIntVar(values, var_name); + } + } + return array; + } + + public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + array[i, j] = MakeIntVar(values); + } + } + return array; + } + + public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values, string name) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + array[i, j] = MakeIntVar(values, var_name); + } + } + return array; + } + + public IntVar[,] MakeBoolVarMatrix(int rows, int cols) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + array[i, j] = MakeBoolVar(); + } + } + return array; + } + + public IntVar[,] MakeBoolVarMatrix(int rows, int cols, string name) + { + IntVar[,] array = new IntVar[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + array[i, j] = MakeBoolVar(var_name); + } + } + return array; + } + + public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, + bool optional) + { + IntervalVar[] array = new IntervalVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeFixedDurationIntervalVar(start_min, start_max, duration, optional, ""); + } + return array; + } + + public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, + bool optional, string name) + { + IntervalVar[] array = new IntervalVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeFixedDurationIntervalVar(start_min, start_max, duration, optional, name + i); + } + return array; + } + + public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long[] start_min, long[] start_max, + long[] duration, bool optional, string name) + { + IntervalVar[] array = new IntervalVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeFixedDurationIntervalVar(start_min[i], start_max[i], duration[i], optional, name + i); + } + return array; + } + + public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, int[] start_min, int[] start_max, int[] duration, + bool optional, string name) + { + IntervalVar[] array = new IntervalVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeFixedDurationIntervalVar(start_min[i], start_max[i], duration[i], optional, name + i); + } + return array; + } + public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, int[] durations, string name) + { + int count = starts.Length; + IntervalVar[] array = new IntervalVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeFixedDurationIntervalVar(starts[i], durations[i], name + i); + } + return array; + } + public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, long[] durations, string name) + { + int count = starts.Length; + IntervalVar[] array = new IntervalVar[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeFixedDurationIntervalVar(starts[i], durations[i], name + i); + } + return array; + } + public void NewSearch(DecisionBuilder db) + { + pinned_decision_builder_ = db; + pinned_search_monitors_.Clear(); + NewSearchAux(db); + } + + public void NewSearch(DecisionBuilder db, SearchMonitor sm1) + { + pinned_decision_builder_ = db; + pinned_search_monitors_.Clear(); + pinned_search_monitors_.Add(sm1); + NewSearchAux(db, sm1); + } + + public void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2) + { + pinned_decision_builder_ = db; + pinned_search_monitors_.Clear(); + pinned_search_monitors_.Add(sm1); + pinned_search_monitors_.Add(sm2); + NewSearchAux(db, sm1, sm2); + } + + public void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2, SearchMonitor sm3) + { + pinned_decision_builder_ = db; + pinned_search_monitors_.Clear(); + pinned_search_monitors_.Add(sm1); + pinned_search_monitors_.Add(sm2); + pinned_search_monitors_.Add(sm3); + NewSearchAux(db, sm1, sm2, sm3); + } + + public void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2, SearchMonitor sm3, + SearchMonitor sm4) + { + pinned_decision_builder_ = db; + pinned_search_monitors_.Clear(); + pinned_search_monitors_.Add(sm1); + pinned_search_monitors_.Add(sm2); + pinned_search_monitors_.Add(sm3); + pinned_search_monitors_.Add(sm4); + NewSearchAux(db, sm1, sm2, sm3, sm4); + } + + public void NewSearch(DecisionBuilder db, SearchMonitor[] monitors) + { + pinned_decision_builder_ = db; + pinned_search_monitors_.Clear(); + pinned_search_monitors_.AddRange(monitors); + NewSearchAux(db, monitors); + } + + public void EndSearch() + { + pinned_decision_builder_ = null; + pinned_search_monitors_.Clear(); + EndSearchAux(); + } + + private System.Collections.Generic.List pinned_search_monitors_ = + new System.Collections.Generic.List(); + private DecisionBuilder pinned_decision_builder_; +} + +public partial class IntExpr : PropagationBaseObject +{ + public static IntExpr operator +(IntExpr a, IntExpr b) + { + return a.solver().MakeSum(a, b); + } + public static IntExpr operator +(IntExpr a, long v) + { + return a.solver().MakeSum(a, v); + } + public static IntExpr operator +(long v, IntExpr a) + { + return a.solver().MakeSum(a, v); + } + public static IntExpr operator -(IntExpr a, IntExpr b) + { + return a.solver().MakeDifference(a, b); + } + public static IntExpr operator -(IntExpr a, long v) + { + return a.solver().MakeSum(a, -v); + } + public static IntExpr operator -(long v, IntExpr a) + { + return a.solver().MakeDifference(v, a); + } + public static IntExpr operator *(IntExpr a, IntExpr b) + { + return a.solver().MakeProd(a, b); + } + public static IntExpr operator *(IntExpr a, long v) + { + return a.solver().MakeProd(a, v); + } + public static IntExpr operator *(long v, IntExpr a) + { + return a.solver().MakeProd(a, v); + } + public static IntExpr operator /(IntExpr a, long v) + { + return a.solver().MakeDiv(a, v); + } + public static IntExpr operator %(IntExpr a, long v) + { + return a.solver().MakeModulo(a, v); + } + public static IntExpr operator -(IntExpr a) + { + return a.solver().MakeOpposite(a); + } + public IntExpr Abs() + { + return this.solver().MakeAbs(this); + } + public IntExpr Square() + { + return this.solver().MakeSquare(this); + } + public static IntExprEquality operator ==(IntExpr a, IntExpr b) + { + return new IntExprEquality(a, b, true); + } + public static IntExprEquality operator !=(IntExpr a, IntExpr b) + { + return new IntExprEquality(a, b, false); + } + public static WrappedConstraint operator ==(IntExpr a, long v) + { + return new WrappedConstraint(a.solver().MakeEquality(a, v)); + } + public static WrappedConstraint operator !=(IntExpr a, long v) + { + return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); + } + public static WrappedConstraint operator >=(IntExpr a, long v) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a, v)); + } + public static WrappedConstraint operator>(IntExpr a, long v) + { + return new WrappedConstraint(a.solver().MakeGreater(a, v)); + } + public static WrappedConstraint operator <=(IntExpr a, long v) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a, v)); + } + public static WrappedConstraint operator<(IntExpr a, long v) + { + return new WrappedConstraint(a.solver().MakeLess(a, v)); + } + public static WrappedConstraint operator >=(IntExpr a, IntExpr b) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); + } + public static WrappedConstraint operator>(IntExpr a, IntExpr b) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); + } + public static WrappedConstraint operator <=(IntExpr a, IntExpr b) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); + } + public static WrappedConstraint operator<(IntExpr a, IntExpr b) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); + } +} + +public partial class Constraint : PropagationBaseObject, IConstraintWithStatus +{ + public static implicit operator IntVar(Constraint eq) + { + return eq.Var(); + } + + public static implicit operator IntExpr(Constraint eq) + { + return eq.Var(); + } + public static IntExpr operator +(Constraint a, Constraint b) + { + return a.solver().MakeSum(a.Var(), b.Var()); + } + public static IntExpr operator +(Constraint a, long v) + { + return a.solver().MakeSum(a.Var(), v); + } + public static IntExpr operator +(long v, Constraint a) + { + return a.solver().MakeSum(a.Var(), v); + } + public static IntExpr operator -(Constraint a, Constraint b) + { + return a.solver().MakeDifference(a.Var(), b.Var()); + } + public static IntExpr operator -(Constraint a, long v) + { + return a.solver().MakeSum(a.Var(), -v); + } + public static IntExpr operator -(long v, Constraint a) + { + return a.solver().MakeDifference(v, a.Var()); + } + public static IntExpr operator *(Constraint a, Constraint b) + { + return a.solver().MakeProd(a.Var(), b.Var()); + } + public static IntExpr operator *(Constraint a, long v) + { + return a.solver().MakeProd(a.Var(), v); + } + public static IntExpr operator *(long v, Constraint a) + { + return a.solver().MakeProd(a.Var(), v); + } + public static IntExpr operator /(Constraint a, long v) + { + return a.solver().MakeDiv(a.Var(), v); + } + public static IntExpr operator -(Constraint a) + { + return a.solver().MakeOpposite(a.Var()); + } + public IntExpr Abs() + { + return this.solver().MakeAbs(this.Var()); + } + public IntExpr Square() + { + return this.solver().MakeSquare(this.Var()); + } + public static WrappedConstraint operator ==(Constraint a, long v) + { + return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); + } + public static WrappedConstraint operator ==(long v, Constraint a) + { + return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); + } + public static WrappedConstraint operator !=(Constraint a, long v) + { + return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); + } + public static WrappedConstraint operator !=(long v, Constraint a) + { + return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); + } + public static WrappedConstraint operator >=(Constraint a, long v) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator >=(long v, Constraint a) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator>(Constraint a, long v) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); + } + public static WrappedConstraint operator>(long v, Constraint a) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); + } + public static WrappedConstraint operator <=(Constraint a, long v) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator <=(long v, Constraint a) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator<(Constraint a, long v) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); + } + public static WrappedConstraint operator<(long v, Constraint a) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); + } + public static WrappedConstraint operator >=(Constraint a, Constraint b) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); + } + public static WrappedConstraint operator>(Constraint a, Constraint b) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); + } + public static WrappedConstraint operator <=(Constraint a, Constraint b) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); + } + public static WrappedConstraint operator<(Constraint a, Constraint b) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); + } + public static ConstraintEquality operator ==(Constraint a, Constraint b) + { + return new ConstraintEquality(a, b, true); + } + public static ConstraintEquality operator !=(Constraint a, Constraint b) + { + return new ConstraintEquality(a, b, false); + } +} } // namespace Google.OrTools.ConstraintSolver diff --git a/ortools/constraint_solver/csharp/ValCstPair.cs b/ortools/constraint_solver/csharp/ValCstPair.cs index 86c3d315dc..c8e2a854dc 100644 --- a/ortools/constraint_solver/csharp/ValCstPair.cs +++ b/ortools/constraint_solver/csharp/ValCstPair.cs @@ -13,316 +13,316 @@ namespace Google.OrTools.ConstraintSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - public interface IConstraintWithStatus +public interface IConstraintWithStatus +{ + Solver solver(); + IntVar Var(); +} + +public abstract class BaseEquality : IConstraintWithStatus +{ + abstract public Solver solver(); + abstract public IntVar Var(); + + public static IntExpr operator +(BaseEquality a, BaseEquality b) + { + return a.solver().MakeSum(a.Var(), b.Var()); + } + public static IntExpr operator +(BaseEquality a, long v) + { + return a.solver().MakeSum(a.Var(), v); + } + public static IntExpr operator +(long v, BaseEquality a) + { + return a.solver().MakeSum(a.Var(), v); + } + public static IntExpr operator -(BaseEquality a, BaseEquality b) + { + return a.solver().MakeDifference(a.Var(), b.Var()); + } + public static IntExpr operator -(BaseEquality a, long v) + { + return a.solver().MakeSum(a.Var(), -v); + } + public static IntExpr operator -(long v, BaseEquality a) + { + return a.solver().MakeDifference(v, a.Var()); + } + public static IntExpr operator *(BaseEquality a, BaseEquality b) + { + return a.solver().MakeProd(a.Var(), b.Var()); + } + public static IntExpr operator *(BaseEquality a, long v) + { + return a.solver().MakeProd(a.Var(), v); + } + public static IntExpr operator *(long v, BaseEquality a) + { + return a.solver().MakeProd(a.Var(), v); + } + public static IntExpr operator /(BaseEquality a, long v) + { + return a.solver().MakeDiv(a.Var(), v); + } + public static IntExpr operator -(BaseEquality a) + { + return a.solver().MakeOpposite(a.Var()); + } + public IntExpr Abs() + { + return this.solver().MakeAbs(this.Var()); + } + public IntExpr Square() + { + return this.solver().MakeSquare(this.Var()); + } + public static WrappedConstraint operator ==(BaseEquality a, long v) + { + return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); + } + public static WrappedConstraint operator ==(long v, BaseEquality a) + { + return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); + } + public static WrappedConstraint operator !=(BaseEquality a, long v) + { + return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); + } + public static WrappedConstraint operator !=(long v, BaseEquality a) + { + return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); + } + public static WrappedConstraint operator >=(BaseEquality a, long v) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator >=(long v, BaseEquality a) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator>(BaseEquality a, long v) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); + } + public static WrappedConstraint operator>(long v, BaseEquality a) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); + } + public static WrappedConstraint operator <=(BaseEquality a, long v) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator <=(long v, BaseEquality a) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); + } + public static WrappedConstraint operator<(BaseEquality a, long v) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); + } + public static WrappedConstraint operator<(long v, BaseEquality a) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); + } + public static WrappedConstraint operator >=(BaseEquality a, BaseEquality b) + { + return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); + } + public static WrappedConstraint operator>(BaseEquality a, BaseEquality b) + { + return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); + } + public static WrappedConstraint operator <=(BaseEquality a, BaseEquality b) + { + return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); + } + public static WrappedConstraint operator<(BaseEquality a, BaseEquality b) + { + return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); + } + public static ConstraintEquality operator ==(BaseEquality a, BaseEquality b) + { + return new ConstraintEquality(a, b, true); + } + public static ConstraintEquality operator !=(BaseEquality a, BaseEquality b) + { + return new ConstraintEquality(a, b, false); + } +} + +public class WrappedConstraint : BaseEquality +{ + public bool Val { get; set; } + + public Constraint Cst { get; set; } + + public WrappedConstraint(Constraint cst) : this(true, cst) { - Solver solver(); - IntVar Var(); } - public abstract class BaseEquality : IConstraintWithStatus + public WrappedConstraint(bool val) : this(val, null) { - abstract public Solver solver(); - abstract public IntVar Var(); - - public static IntExpr operator +(BaseEquality a, BaseEquality b) - { - return a.solver().MakeSum(a.Var(), b.Var()); - } - public static IntExpr operator +(BaseEquality a, long v) - { - return a.solver().MakeSum(a.Var(), v); - } - public static IntExpr operator +(long v, BaseEquality a) - { - return a.solver().MakeSum(a.Var(), v); - } - public static IntExpr operator -(BaseEquality a, BaseEquality b) - { - return a.solver().MakeDifference(a.Var(), b.Var()); - } - public static IntExpr operator -(BaseEquality a, long v) - { - return a.solver().MakeSum(a.Var(), -v); - } - public static IntExpr operator -(long v, BaseEquality a) - { - return a.solver().MakeDifference(v, a.Var()); - } - public static IntExpr operator *(BaseEquality a, BaseEquality b) - { - return a.solver().MakeProd(a.Var(), b.Var()); - } - public static IntExpr operator *(BaseEquality a, long v) - { - return a.solver().MakeProd(a.Var(), v); - } - public static IntExpr operator *(long v, BaseEquality a) - { - return a.solver().MakeProd(a.Var(), v); - } - public static IntExpr operator /(BaseEquality a, long v) - { - return a.solver().MakeDiv(a.Var(), v); - } - public static IntExpr operator -(BaseEquality a) - { - return a.solver().MakeOpposite(a.Var()); - } - public IntExpr Abs() - { - return this.solver().MakeAbs(this.Var()); - } - public IntExpr Square() - { - return this.solver().MakeSquare(this.Var()); - } - public static WrappedConstraint operator ==(BaseEquality a, long v) - { - return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); - } - public static WrappedConstraint operator ==(long v, BaseEquality a) - { - return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); - } - public static WrappedConstraint operator !=(BaseEquality a, long v) - { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator !=(long v, BaseEquality a) - { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator >=(BaseEquality a, long v) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator >=(long v, BaseEquality a) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator>(BaseEquality a, long v) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); - } - public static WrappedConstraint operator>(long v, BaseEquality a) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); - } - public static WrappedConstraint operator <=(BaseEquality a, long v) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator <=(long v, BaseEquality a) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator<(BaseEquality a, long v) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); - } - public static WrappedConstraint operator<(long v, BaseEquality a) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); - } - public static WrappedConstraint operator >=(BaseEquality a, BaseEquality b) - { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator>(BaseEquality a, BaseEquality b) - { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); - } - public static WrappedConstraint operator <=(BaseEquality a, BaseEquality b) - { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator<(BaseEquality a, BaseEquality b) - { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); - } - public static ConstraintEquality operator ==(BaseEquality a, BaseEquality b) - { - return new ConstraintEquality(a, b, true); - } - public static ConstraintEquality operator !=(BaseEquality a, BaseEquality b) - { - return new ConstraintEquality(a, b, false); - } } - public class WrappedConstraint : BaseEquality + public WrappedConstraint(bool val, Constraint cst) { - public bool Val { get; set; } - - public Constraint Cst { get; set; } - - public WrappedConstraint(Constraint cst) : this(true, cst) - { - } - - public WrappedConstraint(bool val) : this(val, null) - { - } - - public WrappedConstraint(bool val, Constraint cst) - { - this.Val = val; - this.Cst = cst; - } - - public static implicit operator bool(WrappedConstraint valCstPair) - { - return valCstPair.Val; - } - - public static implicit operator Constraint(WrappedConstraint valCstPair) - { - return valCstPair.Cst; - } - - public static implicit operator IntVar(WrappedConstraint eq) - { - return eq.Var(); - } - - public static implicit operator IntExpr(WrappedConstraint eq) - { - return eq.Var(); - } - - public override Solver solver() - { - return this.Cst.solver(); - } - - public override IntVar Var() - { - return Cst.Var(); - } + this.Val = val; + this.Cst = cst; } - public class IntExprEquality : BaseEquality + public static implicit operator bool(WrappedConstraint valCstPair) { - public IntExprEquality(IntExpr a, IntExpr b, bool equality) - { - this.left_ = a; - this.right_ = b; - this.equality_ = equality; - } - - bool IsTrue() - { - return (object)left_ == (object)right_ ? equality_ : !equality_; - } - - Constraint ToConstraint() - { - return equality_ ? left_.solver().MakeEquality(left_.Var(), right_.Var()) - : left_.solver().MakeNonEquality(left_.Var(), right_.Var()); - } - - public static bool operator true(IntExprEquality eq) - { - return eq.IsTrue(); - } - - public static bool operator false(IntExprEquality eq) - { - return !eq.IsTrue(); - } - - public static implicit operator Constraint(IntExprEquality eq) - { - return eq.ToConstraint(); - } - - public override IntVar Var() - { - return equality_ ? left_.solver().MakeIsEqualVar(left_, right_) - : left_.solver().MakeIsDifferentVar(left_, right_); - } - - public static implicit operator IntVar(IntExprEquality eq) - { - return eq.Var(); - } - - public static implicit operator IntExpr(IntExprEquality eq) - { - return eq.Var(); - } - - public override Solver solver() - { - return left_.solver(); - } - - private IntExpr left_; - private IntExpr right_; - private bool equality_; + return valCstPair.Val; } - public class ConstraintEquality : BaseEquality + public static implicit operator Constraint(WrappedConstraint valCstPair) { - public ConstraintEquality(IConstraintWithStatus a, IConstraintWithStatus b, bool equality) - { - this.left_ = a; - this.right_ = b; - this.equality_ = equality; - } - - bool IsTrue() - { - return (object)left_ == (object)right_ ? equality_ : !equality_; - } - - Constraint ToConstraint() - { - return equality_ ? left_.solver().MakeEquality(left_.Var(), right_.Var()) - : left_.solver().MakeNonEquality(left_.Var(), right_.Var()); - } - - public static bool operator true(ConstraintEquality eq) - { - return eq.IsTrue(); - } - - public static bool operator false(ConstraintEquality eq) - { - return !eq.IsTrue(); - } - - public static implicit operator Constraint(ConstraintEquality eq) - { - return eq.ToConstraint(); - } - - public override IntVar Var() - { - return equality_ ? left_.solver().MakeIsEqualVar(left_.Var(), right_.Var()) - : left_.solver().MakeIsDifferentVar(left_.Var(), right_.Var()); - } - - public static implicit operator IntVar(ConstraintEquality eq) - { - return eq.Var(); - } - - public static implicit operator IntExpr(ConstraintEquality eq) - { - return eq.Var(); - } - - public override Solver solver() - { - return left_.solver(); - } - - private IConstraintWithStatus left_; - private IConstraintWithStatus right_; - private bool equality_; + return valCstPair.Cst; } + + public static implicit operator IntVar(WrappedConstraint eq) + { + return eq.Var(); + } + + public static implicit operator IntExpr(WrappedConstraint eq) + { + return eq.Var(); + } + + public override Solver solver() + { + return this.Cst.solver(); + } + + public override IntVar Var() + { + return Cst.Var(); + } +} + +public class IntExprEquality : BaseEquality +{ + public IntExprEquality(IntExpr a, IntExpr b, bool equality) + { + this.left_ = a; + this.right_ = b; + this.equality_ = equality; + } + + bool IsTrue() + { + return (object)left_ == (object)right_ ? equality_ : !equality_; + } + + Constraint ToConstraint() + { + return equality_ ? left_.solver().MakeEquality(left_.Var(), right_.Var()) + : left_.solver().MakeNonEquality(left_.Var(), right_.Var()); + } + + public static bool operator true(IntExprEquality eq) + { + return eq.IsTrue(); + } + + public static bool operator false(IntExprEquality eq) + { + return !eq.IsTrue(); + } + + public static implicit operator Constraint(IntExprEquality eq) + { + return eq.ToConstraint(); + } + + public override IntVar Var() + { + return equality_ ? left_.solver().MakeIsEqualVar(left_, right_) + : left_.solver().MakeIsDifferentVar(left_, right_); + } + + public static implicit operator IntVar(IntExprEquality eq) + { + return eq.Var(); + } + + public static implicit operator IntExpr(IntExprEquality eq) + { + return eq.Var(); + } + + public override Solver solver() + { + return left_.solver(); + } + + private IntExpr left_; + private IntExpr right_; + private bool equality_; +} + +public class ConstraintEquality : BaseEquality +{ + public ConstraintEquality(IConstraintWithStatus a, IConstraintWithStatus b, bool equality) + { + this.left_ = a; + this.right_ = b; + this.equality_ = equality; + } + + bool IsTrue() + { + return (object)left_ == (object)right_ ? equality_ : !equality_; + } + + Constraint ToConstraint() + { + return equality_ ? left_.solver().MakeEquality(left_.Var(), right_.Var()) + : left_.solver().MakeNonEquality(left_.Var(), right_.Var()); + } + + public static bool operator true(ConstraintEquality eq) + { + return eq.IsTrue(); + } + + public static bool operator false(ConstraintEquality eq) + { + return !eq.IsTrue(); + } + + public static implicit operator Constraint(ConstraintEquality eq) + { + return eq.ToConstraint(); + } + + public override IntVar Var() + { + return equality_ ? left_.solver().MakeIsEqualVar(left_.Var(), right_.Var()) + : left_.solver().MakeIsDifferentVar(left_.Var(), right_.Var()); + } + + public static implicit operator IntVar(ConstraintEquality eq) + { + return eq.Var(); + } + + public static implicit operator IntExpr(ConstraintEquality eq) + { + return eq.Var(); + } + + public override Solver solver() + { + return left_.solver(); + } + + private IConstraintWithStatus left_; + private IConstraintWithStatus right_; + private bool equality_; +} } // namespace Google.OrTools.ConstraintSolver diff --git a/ortools/constraint_solver/samples/CpIsFunCp.java b/ortools/constraint_solver/samples/CpIsFunCp.java index c6c735698c..cafbe0f4d6 100644 --- a/ortools/constraint_solver/samples/CpIsFunCp.java +++ b/ortools/constraint_solver/samples/CpIsFunCp.java @@ -66,16 +66,16 @@ public final class CpIsFunCp { // CP + IS + FUN = TRUE final IntVar sum1 = - solver.makeSum(new IntVar[] { - p, s, n, - solver.makeProd(solver.makeSum(new IntVar[]{c, i, u}).var(), base).var(), - solver.makeProd(f, base * base).var()}).var(); - final IntVar sum2 = - solver.makeSum(new IntVar[] { - e, - solver.makeProd(u, base).var(), - solver.makeProd(r, base * base).var(), - solver.makeProd(t, base * base * base).var()}).var(); + solver + .makeSum(new IntVar[] {p, s, n, + solver.makeProd(solver.makeSum(new IntVar[] {c, i, u}).var(), base).var(), + solver.makeProd(f, base * base).var()}) + .var(); + final IntVar sum2 = solver + .makeSum(new IntVar[] {e, solver.makeProd(u, base).var(), + solver.makeProd(r, base * base).var(), + solver.makeProd(t, base * base * base).var()}) + .var(); solver.addConstraint(solver.makeEquality(sum1, sum2)); // [END constraints] diff --git a/ortools/constraint_solver/samples/SimpleRoutingProgram.cs b/ortools/constraint_solver/samples/SimpleRoutingProgram.cs index 055f16d7c6..4d90414057 100644 --- a/ortools/constraint_solver/samples/SimpleRoutingProgram.cs +++ b/ortools/constraint_solver/samples/SimpleRoutingProgram.cs @@ -43,12 +43,14 @@ public class SimpleRoutingProgram // Create and register a transit callback. // [START transit_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); - }); + 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); + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/TspCircuitBoard.cs b/ortools/constraint_solver/samples/TspCircuitBoard.cs index 67f0304a3d..cc6953ef54 100644 --- a/ortools/constraint_solver/samples/TspCircuitBoard.cs +++ b/ortools/constraint_solver/samples/TspCircuitBoard.cs @@ -146,12 +146,14 @@ public class TspCircuitBoard // Define cost of each arc. // [START transit_callback] long[,] distanceMatrix = ComputeEuclideanDistanceMatrix(data.Locations); - 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 distanceMatrix[fromNode, toNode]; - }); + 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 distanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // [START arc_cost] diff --git a/ortools/constraint_solver/samples/TspCities.cs b/ortools/constraint_solver/samples/TspCities.cs index 804655c26f..f85afd5af1 100644 --- a/ortools/constraint_solver/samples/TspCities.cs +++ b/ortools/constraint_solver/samples/TspCities.cs @@ -88,12 +88,14 @@ public class TspCities // [END routing_model] // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/TspDistanceMatrix.cs b/ortools/constraint_solver/samples/TspDistanceMatrix.cs index 3dd1c2e38c..82fd5b1b5a 100644 --- a/ortools/constraint_solver/samples/TspDistanceMatrix.cs +++ b/ortools/constraint_solver/samples/TspDistanceMatrix.cs @@ -93,12 +93,14 @@ public class TspDistanceMatrix // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/Vrp.cs b/ortools/constraint_solver/samples/Vrp.cs index 80feb7b750..8f4e49cfaa 100644 --- a/ortools/constraint_solver/samples/Vrp.cs +++ b/ortools/constraint_solver/samples/Vrp.cs @@ -100,12 +100,14 @@ public class Vrp // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpBreaks.cs b/ortools/constraint_solver/samples/VrpBreaks.cs index 8e661cc118..883726bef7 100644 --- a/ortools/constraint_solver/samples/VrpBreaks.cs +++ b/ortools/constraint_solver/samples/VrpBreaks.cs @@ -120,12 +120,14 @@ public class VrpBreaks // Create and register a transit callback. // [START transit_callback] - int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) => { - // Convert from routing variable Index to time matrix NodeIndex. - var fromNode = manager.IndexToNode(fromIndex); - var toNode = manager.IndexToNode(toIndex); - return data.TimeMatrix[fromNode, toNode] + data.ServiceTime[fromNode]; - }); + int transitCallbackIndex = + routing.RegisterTransitCallback((long fromIndex, long toIndex) => + { + // Convert from routing variable Index to time matrix NodeIndex. + var fromNode = manager.IndexToNode(fromIndex); + var toNode = manager.IndexToNode(toIndex); + return data.TimeMatrix[fromNode, toNode] + data.ServiceTime[fromNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpCapacity.cs b/ortools/constraint_solver/samples/VrpCapacity.cs index 4065069605..c1f3ab574c 100644 --- a/ortools/constraint_solver/samples/VrpCapacity.cs +++ b/ortools/constraint_solver/samples/VrpCapacity.cs @@ -112,12 +112,14 @@ public class VrpCapacity // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. @@ -127,11 +129,14 @@ public class VrpCapacity // Add Capacity constraint. // [START capacity_constraint] - int demandCallbackIndex = routing.RegisterUnaryTransitCallback((long fromIndex) => { - // Convert from routing variable Index to demand NodeIndex. - var fromNode = manager.IndexToNode(fromIndex); - return data.Demands[fromNode]; - }); + int demandCallbackIndex = routing.RegisterUnaryTransitCallback((long fromIndex) => + { + // Convert from routing variable Index to + // demand NodeIndex. + var fromNode = + manager.IndexToNode(fromIndex); + return data.Demands[fromNode]; + }); routing.AddDimensionWithVehicleCapacity(demandCallbackIndex, 0, // null capacity slack data.VehicleCapacities, // vehicle maximum capacities true, // start cumul to zero diff --git a/ortools/constraint_solver/samples/VrpDropNodes.cs b/ortools/constraint_solver/samples/VrpDropNodes.cs index 9f7c2f7b22..d6323415b2 100644 --- a/ortools/constraint_solver/samples/VrpDropNodes.cs +++ b/ortools/constraint_solver/samples/VrpDropNodes.cs @@ -127,12 +127,14 @@ public class VrpDropNodes // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. @@ -142,11 +144,14 @@ public class VrpDropNodes // Add Capacity constraint. // [START capacity_constraint] - int demandCallbackIndex = routing.RegisterUnaryTransitCallback((long fromIndex) => { - // Convert from routing variable Index to demand NodeIndex. - var fromNode = manager.IndexToNode(fromIndex); - return data.Demands[fromNode]; - }); + int demandCallbackIndex = routing.RegisterUnaryTransitCallback((long fromIndex) => + { + // Convert from routing variable Index to + // demand NodeIndex. + var fromNode = + manager.IndexToNode(fromIndex); + return data.Demands[fromNode]; + }); routing.AddDimensionWithVehicleCapacity(demandCallbackIndex, 0, // null capacity slack data.VehicleCapacities, // vehicle maximum capacities true, // start cumul to zero diff --git a/ortools/constraint_solver/samples/VrpGlobalSpan.cs b/ortools/constraint_solver/samples/VrpGlobalSpan.cs index 9587732d00..66878d393a 100644 --- a/ortools/constraint_solver/samples/VrpGlobalSpan.cs +++ b/ortools/constraint_solver/samples/VrpGlobalSpan.cs @@ -102,12 +102,14 @@ public class VrpGlobalSpan // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpInitialRoutes.cs b/ortools/constraint_solver/samples/VrpInitialRoutes.cs index fa0df0e3d7..2db2a2eafe 100644 --- a/ortools/constraint_solver/samples/VrpInitialRoutes.cs +++ b/ortools/constraint_solver/samples/VrpInitialRoutes.cs @@ -109,12 +109,14 @@ public class InitialRoutes // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpInitialRoutes.java b/ortools/constraint_solver/samples/VrpInitialRoutes.java index d3cee481f9..827eeb6bc5 100644 --- a/ortools/constraint_solver/samples/VrpInitialRoutes.java +++ b/ortools/constraint_solver/samples/VrpInitialRoutes.java @@ -140,8 +140,7 @@ public class VrpInitialRoutes { // Setting first solution heuristic. // [START parameters] - RoutingSearchParameters searchParameters = - main.defaultRoutingSearchParameters().toBuilder().build(); + RoutingSearchParameters searchParameters = main.defaultRoutingSearchParameters(); // [END parameters] // Solve the problem. diff --git a/ortools/constraint_solver/samples/VrpPickupDelivery.cs b/ortools/constraint_solver/samples/VrpPickupDelivery.cs index ec161a99d0..58bb5598a9 100644 --- a/ortools/constraint_solver/samples/VrpPickupDelivery.cs +++ b/ortools/constraint_solver/samples/VrpPickupDelivery.cs @@ -108,12 +108,14 @@ public class VrpPickupDelivery // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpPickupDeliveryFifo.cs b/ortools/constraint_solver/samples/VrpPickupDeliveryFifo.cs index c918efa83b..73aaada7f0 100644 --- a/ortools/constraint_solver/samples/VrpPickupDeliveryFifo.cs +++ b/ortools/constraint_solver/samples/VrpPickupDeliveryFifo.cs @@ -108,12 +108,14 @@ public class VrpPickupDeliveryFifo // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpPickupDeliveryLifo.cs b/ortools/constraint_solver/samples/VrpPickupDeliveryLifo.cs index f34b480cb6..9e73d954d3 100644 --- a/ortools/constraint_solver/samples/VrpPickupDeliveryLifo.cs +++ b/ortools/constraint_solver/samples/VrpPickupDeliveryLifo.cs @@ -107,12 +107,14 @@ public class VrpPickupDeliveryLifo // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpResources.cs b/ortools/constraint_solver/samples/VrpResources.cs index 592e0e8de8..88246eb991 100644 --- a/ortools/constraint_solver/samples/VrpResources.cs +++ b/ortools/constraint_solver/samples/VrpResources.cs @@ -128,12 +128,14 @@ public class VrpResources // Create and register a transit callback. // [START transit_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 data.TimeMatrix[fromNode, toNode]; - }); + 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 data.TimeMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpStartsEnds.cs b/ortools/constraint_solver/samples/VrpStartsEnds.cs index 1ea4a7c623..f20dd24f88 100644 --- a/ortools/constraint_solver/samples/VrpStartsEnds.cs +++ b/ortools/constraint_solver/samples/VrpStartsEnds.cs @@ -104,12 +104,14 @@ public class VrpStartsEnds // Create and register a transit callback. // [START transit_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 data.DistanceMatrix[fromNode, toNode]; - }); + 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 data.DistanceMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpTimeWindows.cs b/ortools/constraint_solver/samples/VrpTimeWindows.cs index a76d150a4d..13ecc26f1a 100644 --- a/ortools/constraint_solver/samples/VrpTimeWindows.cs +++ b/ortools/constraint_solver/samples/VrpTimeWindows.cs @@ -123,12 +123,14 @@ public class VrpTimeWindows // Create and register a transit callback. // [START transit_callback] - int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) => { - // Convert from routing variable Index to time matrix NodeIndex. - var fromNode = manager.IndexToNode(fromIndex); - var toNode = manager.IndexToNode(toIndex); - return data.TimeMatrix[fromNode, toNode]; - }); + int transitCallbackIndex = routing.RegisterTransitCallback((long fromIndex, long toIndex) => + { + // Convert from routing variable Index to time + // matrix NodeIndex. + var fromNode = manager.IndexToNode(fromIndex); + var toNode = manager.IndexToNode(toIndex); + return data.TimeMatrix[fromNode, toNode]; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/VrpWithTimeLimit.cs b/ortools/constraint_solver/samples/VrpWithTimeLimit.cs index ba0c143a19..aded97345f 100644 --- a/ortools/constraint_solver/samples/VrpWithTimeLimit.cs +++ b/ortools/constraint_solver/samples/VrpWithTimeLimit.cs @@ -76,12 +76,14 @@ public class Vrp // Create and register a transit callback. // [START transit_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 1; - }); + 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 1; + }); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/nqueens_cp.cc b/ortools/constraint_solver/samples/nqueens_cp.cc index 76c57b3a01..964b82bdf5 100644 --- a/ortools/constraint_solver/samples/nqueens_cp.cc +++ b/ortools/constraint_solver/samples/nqueens_cp.cc @@ -24,79 +24,80 @@ namespace operations_research { - void NQueensCp(const int board_size) { - // Instantiate the solver. - // [START solver] - Solver solver("N-Queens"); - // [END solver] +void NQueensCp(const int board_size) { + // Instantiate the solver. + // [START solver] + Solver solver("N-Queens"); + // [END solver] - // [START variables] - std::vector queens; - queens.reserve(board_size); - for (int i=0; i < board_size; ++i) { - queens.push_back(solver.MakeIntVar(0, board_size - 1, absl::StrCat("x",i))); - } - // [END variables] - - // Define constraints. - // [START constraints] - // The following sets the constraint that all queens are in different rows. - solver.AddConstraint(solver.MakeAllDifferent(queens)); - - // All columns must be different because the indices of queens are all different. - // No two queens can be on the same diagonal. - std::vector diag_1; - diag_1.reserve(board_size); - std::vector diag_2; - diag_2.reserve(board_size); - for (int i=0; i < board_size; ++i) { - diag_1.push_back(solver.MakeSum(queens[i], i)->Var()); - diag_2.push_back(solver.MakeSum(queens[i], -i)->Var()); - } - solver.AddConstraint(solver.MakeAllDifferent(diag_1)); - solver.AddConstraint(solver.MakeAllDifferent(diag_2)); - // [END constraints] - - // [START db] - DecisionBuilder* const db = solver.MakePhase( - queens, Solver::CHOOSE_FIRST_UNBOUND, Solver::ASSIGN_MIN_VALUE); - // [END db] - - // [START solve] - // Iterates through the solutions, displaying each. - int num_solutions = 0; - - solver.NewSearch(db); - while (solver.NextSolution()) { - // Displays the solution just computed. - LOG(INFO) << "Solution " << num_solutions; - for (int i=0; i < board_size; ++i) { - std::stringstream ss; - for (int j=0; j < board_size; ++j) { - if (queens[j]->Value() == i) { - // There is a queen in column j, row i. - ss << "Q"; - } else { - ss << "_"; - } - if (j != board_size-1) ss << " "; - } - LOG(INFO) << ss.str(); - } - num_solutions++; - } - solver.EndSearch(); - // [END solve] - - // Statistics. - // [START statistics] - LOG(INFO) << "Statistics"; - LOG(INFO) << " failures: " << solver.failures(); - LOG(INFO) << " branches: " << solver.branches(); - LOG(INFO) << " wall time: " << solver.wall_time() << " ms"; - LOG(INFO) << " Solutions found: " << num_solutions; - // [END statistics] + // [START variables] + std::vector queens; + queens.reserve(board_size); + for (int i = 0; i < board_size; ++i) { + queens.push_back( + solver.MakeIntVar(0, board_size - 1, absl::StrCat("x", i))); } + // [END variables] + + // Define constraints. + // [START constraints] + // The following sets the constraint that all queens are in different rows. + solver.AddConstraint(solver.MakeAllDifferent(queens)); + + // All columns must be different because the indices of queens are all + // different. No two queens can be on the same diagonal. + std::vector diag_1; + diag_1.reserve(board_size); + std::vector diag_2; + diag_2.reserve(board_size); + for (int i = 0; i < board_size; ++i) { + diag_1.push_back(solver.MakeSum(queens[i], i)->Var()); + diag_2.push_back(solver.MakeSum(queens[i], -i)->Var()); + } + solver.AddConstraint(solver.MakeAllDifferent(diag_1)); + solver.AddConstraint(solver.MakeAllDifferent(diag_2)); + // [END constraints] + + // [START db] + DecisionBuilder* const db = solver.MakePhase( + queens, Solver::CHOOSE_FIRST_UNBOUND, Solver::ASSIGN_MIN_VALUE); + // [END db] + + // [START solve] + // Iterates through the solutions, displaying each. + int num_solutions = 0; + + solver.NewSearch(db); + while (solver.NextSolution()) { + // Displays the solution just computed. + LOG(INFO) << "Solution " << num_solutions; + for (int i = 0; i < board_size; ++i) { + std::stringstream ss; + for (int j = 0; j < board_size; ++j) { + if (queens[j]->Value() == i) { + // There is a queen in column j, row i. + ss << "Q"; + } else { + ss << "_"; + } + if (j != board_size - 1) ss << " "; + } + LOG(INFO) << ss.str(); + } + num_solutions++; + } + solver.EndSearch(); + // [END solve] + + // Statistics. + // [START statistics] + LOG(INFO) << "Statistics"; + LOG(INFO) << " failures: " << solver.failures(); + LOG(INFO) << " branches: " << solver.branches(); + LOG(INFO) << " wall time: " << solver.wall_time() << " ms"; + LOG(INFO) << " Solutions found: " << num_solutions; + // [END statistics] +} } // namespace operations_research diff --git a/ortools/linear_solver/csharp/LinearConstraint.cs b/ortools/linear_solver/csharp/LinearConstraint.cs index 221f957102..ba572bebd7 100644 --- a/ortools/linear_solver/csharp/LinearConstraint.cs +++ b/ortools/linear_solver/csharp/LinearConstraint.cs @@ -13,156 +13,156 @@ namespace Google.OrTools.LinearSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - public class LinearConstraint +public class LinearConstraint +{ + public virtual String ToString() { - public virtual String ToString() - { - return "LinearConstraint"; - } - - public virtual Constraint Extract(Solver solver) - { - return null; - } + return "LinearConstraint"; } - public class RangeConstraint : LinearConstraint + public virtual Constraint Extract(Solver solver) { - public RangeConstraint(LinearExpr expr, double lb, double ub) - { - this.expr_ = expr; - this.lb_ = lb; - this.ub_ = ub; - } + return null; + } +} - public override String ToString() - { - return "" + lb_ + " <= " + expr_.ToString() + " <= " + ub_; - } - - public override Constraint Extract(Solver solver) - { - Dictionary coefficients = new Dictionary(); - double constant = expr_.Visit(coefficients); - Constraint ct = solver.MakeConstraint(lb_ - constant, ub_ - constant); - foreach (KeyValuePair pair in coefficients) - { - ct.SetCoefficient(pair.Key, pair.Value); - } - return ct; - } - - public static implicit operator bool(RangeConstraint ct) - { - return false; - } - - private LinearExpr expr_; - private double lb_; - private double ub_; +public class RangeConstraint : LinearConstraint +{ + public RangeConstraint(LinearExpr expr, double lb, double ub) + { + this.expr_ = expr; + this.lb_ = lb; + this.ub_ = ub; } - public class Equality : LinearConstraint + public override String ToString() { - public Equality(LinearExpr left, LinearExpr right, bool equality) - { - this.left_ = left; - this.right_ = right; - this.equality_ = equality; - } - - public override String ToString() - { - return "" + left_.ToString() + " == " + right_.ToString(); - } - - public override Constraint Extract(Solver solver) - { - if (!equality_) - { - throw new ArgumentException("Operator != not supported for LinearExpression"); - } - Dictionary coefficients = new Dictionary(); - double constant = left_.Visit(coefficients); - constant += right_.DoVisit(coefficients, -1); - Constraint ct = solver.MakeConstraint(-constant, -constant); - foreach (KeyValuePair pair in coefficients) - { - ct.SetCoefficient(pair.Key, pair.Value); - } - return ct; - } - - public static implicit operator bool(Equality ct) - { - return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_; - } - - private LinearExpr left_; - private LinearExpr right_; - private bool equality_; + return "" + lb_ + " <= " + expr_.ToString() + " <= " + ub_; } - public class VarEquality : LinearConstraint + public override Constraint Extract(Solver solver) { - public VarEquality(Variable left, Variable right, bool equality) + Dictionary coefficients = new Dictionary(); + double constant = expr_.Visit(coefficients); + Constraint ct = solver.MakeConstraint(lb_ - constant, ub_ - constant); + foreach (KeyValuePair pair in coefficients) { - this.left_ = left; - this.right_ = right; - this.equality_ = equality; + ct.SetCoefficient(pair.Key, pair.Value); } - - public override String ToString() - { - return "" + left_.Name() + " == " + right_.Name(); - } - - public override Constraint Extract(Solver solver) - { - Constraint ct = solver.MakeConstraint(0.0, 0.0); - ct.SetCoefficient(left_, 1.0); - ct.SetCoefficient(right_, -1.0); - return ct; - } - - public static implicit operator bool(VarEquality ct) - { - return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_; - } - - private Variable left_; - private Variable right_; - private bool equality_; + return ct; } - // TODO(user): Try to move this code back to the .swig with @define macros. - public partial class MPConstraintVector : IDisposable, - System.Collections.IEnumerable + public static implicit operator bool(RangeConstraint ct) + { + return false; + } + + private LinearExpr expr_; + private double lb_; + private double ub_; +} + +public class Equality : LinearConstraint +{ + public Equality(LinearExpr left, LinearExpr right, bool equality) + { + this.left_ = left; + this.right_ = right; + this.equality_ = equality; + } + + public override String ToString() + { + return "" + left_.ToString() + " == " + right_.ToString(); + } + + public override Constraint Extract(Solver solver) + { + if (!equality_) + { + throw new ArgumentException("Operator != not supported for LinearExpression"); + } + Dictionary coefficients = new Dictionary(); + double constant = left_.Visit(coefficients); + constant += right_.DoVisit(coefficients, -1); + Constraint ct = solver.MakeConstraint(-constant, -constant); + foreach (KeyValuePair pair in coefficients) + { + ct.SetCoefficient(pair.Key, pair.Value); + } + return ct; + } + + public static implicit operator bool(Equality ct) + { + return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_; + } + + private LinearExpr left_; + private LinearExpr right_; + private bool equality_; +} + +public class VarEquality : LinearConstraint +{ + public VarEquality(Variable left, Variable right, bool equality) + { + this.left_ = left; + this.right_ = right; + this.equality_ = equality; + } + + public override String ToString() + { + return "" + left_.Name() + " == " + right_.Name(); + } + + public override Constraint Extract(Solver solver) + { + Constraint ct = solver.MakeConstraint(0.0, 0.0); + ct.SetCoefficient(left_, 1.0); + ct.SetCoefficient(right_, -1.0); + return ct; + } + + public static implicit operator bool(VarEquality ct) + { + return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_; + } + + private Variable left_; + private Variable right_; + private bool equality_; +} + +// TODO(user): Try to move this code back to the .swig with @define macros. +public partial class MPConstraintVector : IDisposable, + System.Collections.IEnumerable #if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList + , + System.Collections.Generic.IList #endif +{ + // cast from C# MPConstraint array + public static implicit operator MPConstraintVector(Constraint[] inVal) { - // cast from C# MPConstraint array - public static implicit operator MPConstraintVector(Constraint[] inVal) + var outVal = new MPConstraintVector(); + foreach (Constraint element in inVal) { - var outVal = new MPConstraintVector(); - foreach (Constraint element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# MPConstraint array - public static implicit operator Constraint[](MPConstraintVector inVal) - { - var outVal = new Constraint[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; + outVal.Add(element); } + return outVal; } + + // cast to C# MPConstraint array + public static implicit operator Constraint[](MPConstraintVector inVal) + { + var outVal = new Constraint[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} } // namespace Google.OrTools.LinearSolver diff --git a/ortools/linear_solver/csharp/LinearExpr.cs b/ortools/linear_solver/csharp/LinearExpr.cs index 5966ee2b24..4647c8c577 100644 --- a/ortools/linear_solver/csharp/LinearExpr.cs +++ b/ortools/linear_solver/csharp/LinearExpr.cs @@ -13,324 +13,324 @@ namespace Google.OrTools.LinearSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - public class LinearExpr +public class LinearExpr +{ + public virtual double DoVisit(Dictionary coefficients, double multiplier) { - public virtual double DoVisit(Dictionary coefficients, double multiplier) - { - return 0; - } + return 0; + } - public double Visit(Dictionary coefficients) - { - return DoVisit(coefficients, 1.0); - } + public double Visit(Dictionary coefficients) + { + return DoVisit(coefficients, 1.0); + } - public static LinearExpr operator +(LinearExpr a, double v) - { - return new SumCst(a, v); - } + public static LinearExpr operator +(LinearExpr a, double v) + { + return new SumCst(a, v); + } - public static LinearExpr operator +(double v, LinearExpr a) - { - return new SumCst(a, v); - } + public static LinearExpr operator +(double v, LinearExpr a) + { + return new SumCst(a, v); + } - public static LinearExpr operator +(LinearExpr a, LinearExpr b) - { - return new Sum(a, b); - } + public static LinearExpr operator +(LinearExpr a, LinearExpr b) + { + return new Sum(a, b); + } - public static LinearExpr operator -(LinearExpr a, double v) - { - return new SumCst(a, -v); - } + public static LinearExpr operator -(LinearExpr a, double v) + { + return new SumCst(a, -v); + } - public static LinearExpr operator -(double v, LinearExpr a) - { - return new SumCst(new ProductCst(a, -1.0), v); - } + public static LinearExpr operator -(double v, LinearExpr a) + { + return new SumCst(new ProductCst(a, -1.0), v); + } - public static LinearExpr operator -(LinearExpr a, LinearExpr b) - { - return new Sum(a, new ProductCst(b, -1.0)); - } + public static LinearExpr operator -(LinearExpr a, LinearExpr b) + { + return new Sum(a, new ProductCst(b, -1.0)); + } - public static LinearExpr operator -(LinearExpr a) - { - return new ProductCst(a, -1.0); - } + public static LinearExpr operator -(LinearExpr a) + { + return new ProductCst(a, -1.0); + } - public static LinearExpr operator *(LinearExpr a, double v) - { - return new ProductCst(a, v); - } + public static LinearExpr operator *(LinearExpr a, double v) + { + return new ProductCst(a, v); + } - public static LinearExpr operator /(LinearExpr a, double v) - { - return new ProductCst(a, 1 / v); - } + public static LinearExpr operator /(LinearExpr a, double v) + { + return new ProductCst(a, 1 / v); + } - public static LinearExpr operator *(double v, LinearExpr a) - { - return new ProductCst(a, v); - } + public static LinearExpr operator *(double v, LinearExpr a) + { + return new ProductCst(a, v); + } - public static RangeConstraint operator ==(LinearExpr a, double v) - { - return new RangeConstraint(a, v, v); - } + public static RangeConstraint operator ==(LinearExpr a, double v) + { + return new RangeConstraint(a, v, v); + } - public static RangeConstraint operator ==(double v, LinearExpr a) - { - return new RangeConstraint(a, v, v); - } + public static RangeConstraint operator ==(double v, LinearExpr a) + { + return new RangeConstraint(a, v, v); + } - public static RangeConstraint operator !=(LinearExpr a, double v) - { - throw new ArgumentException("Operator != not supported for LinearExpression"); - } + public static RangeConstraint operator !=(LinearExpr a, double v) + { + throw new ArgumentException("Operator != not supported for LinearExpression"); + } - public static RangeConstraint operator !=(double v, LinearExpr a) - { - throw new ArgumentException("Operator != not supported for LinearExpression"); - } + public static RangeConstraint operator !=(double v, LinearExpr a) + { + throw new ArgumentException("Operator != not supported for LinearExpression"); + } - public static Equality operator ==(LinearExpr a, LinearExpr b) - { - return new Equality(a, b, true); - } + public static Equality operator ==(LinearExpr a, LinearExpr b) + { + return new Equality(a, b, true); + } - public static Equality operator !=(LinearExpr a, LinearExpr b) - { - return new Equality(a, b, false); - } + public static Equality operator !=(LinearExpr a, LinearExpr b) + { + return new Equality(a, b, false); + } - public static RangeConstraint operator <=(LinearExpr a, double v) - { - return new RangeConstraint(a, double.NegativeInfinity, v); - } + public static RangeConstraint operator <=(LinearExpr a, double v) + { + return new RangeConstraint(a, double.NegativeInfinity, v); + } - public static RangeConstraint operator >=(LinearExpr a, double v) - { - return new RangeConstraint(a, v, double.PositiveInfinity); - } + public static RangeConstraint operator >=(LinearExpr a, double v) + { + return new RangeConstraint(a, v, double.PositiveInfinity); + } - public static RangeConstraint operator <=(LinearExpr a, LinearExpr b) - { - return a - b <= 0; - } + public static RangeConstraint operator <=(LinearExpr a, LinearExpr b) + { + return a - b <= 0; + } - public static RangeConstraint operator >=(LinearExpr a, LinearExpr b) - { - return a - b >= 0; - } + public static RangeConstraint operator >=(LinearExpr a, LinearExpr b) + { + return a - b >= 0; + } - public static implicit operator LinearExpr(Variable a) + public static implicit operator LinearExpr(Variable a) + { + return new VarWrapper(a); + } +} + +public static class LinearExprArrayHelper +{ + public static LinearExpr Sum(this LinearExpr[] exprs) + { + return new SumArray(exprs); + } + + public static LinearExpr Sum(this Variable[] vars) + { + return new SumVarArray(vars); + } +} + +// def __ge__(self, arg): +// if isinstance(arg, (int, long, float)): +// return LinearConstraint(self, arg, 1e308) +// else: +// return LinearConstraint(Sum(self, ProductCst(arg, -1)), 0.0, 1e308) + +// def __le__(self, arg): +// if isinstance(arg, (int, long, float)): +// return LinearConstraint(self, -1e308, arg) +// else: +// return LinearConstraint(Sum(self, ProductCst(arg, -1)), -1e308, 0.0) + +class ProductCst : LinearExpr +{ + public ProductCst(LinearExpr expr, double coeff) + { + this.coeff_ = coeff; + this.expr_ = expr; + } + + public override String ToString() + { + return "(" + expr_.ToString() + " * " + coeff_ + ")"; + } + + public override double DoVisit(Dictionary coefficients, double multiplier) + { + double current_multiplier = multiplier * coeff_; + if (current_multiplier != 0.0) { - return new VarWrapper(a); + return expr_.DoVisit(coefficients, current_multiplier); + } + else + { + return 0.0; } } - public static class LinearExprArrayHelper - { - public static LinearExpr Sum(this LinearExpr[] exprs) - { - return new SumArray(exprs); - } + private LinearExpr expr_; + private double coeff_; +} - public static LinearExpr Sum(this Variable[] vars) +class SumCst : LinearExpr +{ + public SumCst(LinearExpr expr, double coeff) + { + this.coeff_ = coeff; + this.expr_ = expr; + } + + public override String ToString() + { + return "(" + expr_.ToString() + " + " + coeff_ + ")"; + } + + public override double DoVisit(Dictionary coefficients, double multiplier) + { + if (multiplier != 0.0) { - return new SumVarArray(vars); + return coeff_ * multiplier + expr_.DoVisit(coefficients, multiplier); + } + else + { + return 0.0; } } - // def __ge__(self, arg): - // if isinstance(arg, (int, long, float)): - // return LinearConstraint(self, arg, 1e308) - // else: - // return LinearConstraint(Sum(self, ProductCst(arg, -1)), 0.0, 1e308) + private LinearExpr expr_; + private double coeff_; +} - // def __le__(self, arg): - // if isinstance(arg, (int, long, float)): - // return LinearConstraint(self, -1e308, arg) - // else: - // return LinearConstraint(Sum(self, ProductCst(arg, -1)), -1e308, 0.0) - - class ProductCst : LinearExpr +class VarWrapper : LinearExpr +{ + public VarWrapper(Variable var) { - public ProductCst(LinearExpr expr, double coeff) - { - this.coeff_ = coeff; - this.expr_ = expr; - } + this.var_ = var; + } - public override String ToString() - { - return "(" + expr_.ToString() + " * " + coeff_ + ")"; - } + public override String ToString() + { + return var_.Name(); + } - public override double DoVisit(Dictionary coefficients, double multiplier) + public override double DoVisit(Dictionary coefficients, double multiplier) + { + if (multiplier != 0.0) { - double current_multiplier = multiplier * coeff_; - if (current_multiplier != 0.0) + if (coefficients.ContainsKey(var_)) { - return expr_.DoVisit(coefficients, current_multiplier); + coefficients[var_] += multiplier; } else { - return 0.0; + coefficients[var_] = multiplier; } } - - private LinearExpr expr_; - private double coeff_; + return 0.0; } - class SumCst : LinearExpr + private Variable var_; +} + +class Sum : LinearExpr +{ + public Sum(LinearExpr left, LinearExpr right) { - public SumCst(LinearExpr expr, double coeff) - { - this.coeff_ = coeff; - this.expr_ = expr; - } - - public override String ToString() - { - return "(" + expr_.ToString() + " + " + coeff_ + ")"; - } - - public override double DoVisit(Dictionary coefficients, double multiplier) - { - if (multiplier != 0.0) - { - return coeff_ * multiplier + expr_.DoVisit(coefficients, multiplier); - } - else - { - return 0.0; - } - } - - private LinearExpr expr_; - private double coeff_; + this.left_ = left; + this.right_ = right; } - class VarWrapper : LinearExpr + public override String ToString() { - public VarWrapper(Variable var) - { - this.var_ = var; - } + return "(" + left_.ToString() + " + " + right_.ToString() + ")"; + } - public override String ToString() + public override double DoVisit(Dictionary coefficients, double multiplier) + { + if (multiplier != 0.0) { - return var_.Name(); + return left_.DoVisit(coefficients, multiplier) + right_.DoVisit(coefficients, multiplier); } - - public override double DoVisit(Dictionary coefficients, double multiplier) + else { - if (multiplier != 0.0) + return 0.0; + } + } + + private LinearExpr left_; + private LinearExpr right_; +} + +public class SumArray : LinearExpr +{ + public SumArray(LinearExpr[] array) + { + this.array_ = array; + } + + public override double DoVisit(Dictionary coefficients, double multiplier) + { + if (multiplier != 0.0) + { + double constant = 0.0; + foreach (LinearExpr expr in array_) { - if (coefficients.ContainsKey(var_)) + constant += expr.DoVisit(coefficients, multiplier); + } + return constant; + } + else + { + return 0.0; + } + } + + private LinearExpr[] array_; +} + +public class SumVarArray : LinearExpr +{ + public SumVarArray(Variable[] array) + { + this.array_ = array; + } + + public override double DoVisit(Dictionary coefficients, double multiplier) + { + if (multiplier != 0.0) + { + foreach (Variable var in array_) + { + if (coefficients.ContainsKey(var)) { - coefficients[var_] += multiplier; + coefficients[var] += multiplier; } else { - coefficients[var_] = multiplier; + coefficients[var] = multiplier; } } - return 0.0; } - - private Variable var_; + return 0.0; } - class Sum : LinearExpr - { - public Sum(LinearExpr left, LinearExpr right) - { - this.left_ = left; - this.right_ = right; - } - - public override String ToString() - { - return "(" + left_.ToString() + " + " + right_.ToString() + ")"; - } - - public override double DoVisit(Dictionary coefficients, double multiplier) - { - if (multiplier != 0.0) - { - return left_.DoVisit(coefficients, multiplier) + right_.DoVisit(coefficients, multiplier); - } - else - { - return 0.0; - } - } - - private LinearExpr left_; - private LinearExpr right_; - } - - public class SumArray : LinearExpr - { - public SumArray(LinearExpr[] array) - { - this.array_ = array; - } - - public override double DoVisit(Dictionary coefficients, double multiplier) - { - if (multiplier != 0.0) - { - double constant = 0.0; - foreach (LinearExpr expr in array_) - { - constant += expr.DoVisit(coefficients, multiplier); - } - return constant; - } - else - { - return 0.0; - } - } - - private LinearExpr[] array_; - } - - public class SumVarArray : LinearExpr - { - public SumVarArray(Variable[] array) - { - this.array_ = array; - } - - public override double DoVisit(Dictionary coefficients, double multiplier) - { - if (multiplier != 0.0) - { - foreach (Variable var in array_) - { - if (coefficients.ContainsKey(var)) - { - coefficients[var] += multiplier; - } - else - { - coefficients[var] = multiplier; - } - } - } - return 0.0; - } - - private Variable[] array_; - } + private Variable[] array_; +} } // namespace Google.OrTools.LinearSolver diff --git a/ortools/linear_solver/csharp/SolverHelper.cs b/ortools/linear_solver/csharp/SolverHelper.cs index 9e199abe29..b5aeb676fc 100644 --- a/ortools/linear_solver/csharp/SolverHelper.cs +++ b/ortools/linear_solver/csharp/SolverHelper.cs @@ -13,217 +13,217 @@ namespace Google.OrTools.LinearSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - // Patch the MPSolver class to: - // - support custom versions of the array-based APIs (MakeVarArray, etc). - // - customize the construction, and the OptimizationProblemType enum. - // - support the natural language API. - public partial class Solver +// Patch the MPSolver class to: +// - support custom versions of the array-based APIs (MakeVarArray, etc). +// - customize the construction, and the OptimizationProblemType enum. +// - support the natural language API. +public partial class Solver +{ + public Variable[] MakeVarArray(int count, double lb, double ub, bool integer) { - public Variable[] MakeVarArray(int count, double lb, double ub, bool integer) + Variable[] array = new Variable[count]; + for (int i = 0; i < count; ++i) { - Variable[] array = new Variable[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeVar(lb, ub, integer, ""); - } - return array; - } - - public Variable[] MakeVarArray(int count, double lb, double ub, bool integer, string var_name) - { - Variable[] array = new Variable[count]; - for (int i = 0; i < count; ++i) - { - array[i] = MakeVar(lb, ub, integer, var_name + i); - } - return array; - } - - public Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - matrix[i, j] = MakeVar(lb, ub, integer, ""); - } - } - return matrix; - } - - public Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer, string name) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - matrix[i, j] = MakeVar(lb, ub, integer, var_name); - } - } - return matrix; - } - - public Variable[] MakeNumVarArray(int count, double lb, double ub) - { - return MakeVarArray(count, lb, ub, false); - } - - public Variable[] MakeNumVarArray(int count, double lb, double ub, string var_name) - { - return MakeVarArray(count, lb, ub, false, var_name); - } - - public Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - matrix[i, j] = MakeNumVar(lb, ub, ""); - } - } - return matrix; - } - - public Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub, string name) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - matrix[i, j] = MakeNumVar(lb, ub, var_name); - } - } - return matrix; - } - - public Variable[] MakeIntVarArray(int count, double lb, double ub) - { - return MakeVarArray(count, lb, ub, true); - } - - public Variable[] MakeIntVarArray(int count, double lb, double ub, string var_name) - { - return MakeVarArray(count, lb, ub, true, var_name); - } - - public Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - matrix[i, j] = MakeIntVar(lb, ub, ""); - } - } - return matrix; - } - - public Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub, string name) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - matrix[i, j] = MakeIntVar(lb, ub, var_name); - } - } - return matrix; - } - - public Variable[] MakeBoolVarArray(int count) - { - return MakeVarArray(count, 0.0, 1.0, true); - } - - public Variable[] MakeBoolVarArray(int count, string var_name) - { - return MakeVarArray(count, 0.0, 1.0, true, var_name); - } - - public Variable[,] MakeBoolVarMatrix(int rows, int cols) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - matrix[i, j] = MakeBoolVar(""); - } - } - return matrix; - } - - public Variable[,] MakeBoolVarMatrix(int rows, int cols, string name) - { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) - { - for (int j = 0; j < cols; ++j) - { - string var_name = name + "[" + i + ", " + j + "]"; - matrix[i, j] = MakeBoolVar(var_name); - } - } - return matrix; - } - - public Constraint Add(LinearConstraint constraint) - { - return constraint.Extract(this); - } - - public void Minimize(LinearExpr expr) - { - Objective().Clear(); - Objective().SetMinimization(); - Dictionary coefficients = new Dictionary(); - double constant = expr.Visit(coefficients); - foreach (KeyValuePair pair in coefficients) - { - Objective().SetCoefficient(pair.Key, pair.Value); - } - Objective().SetOffset(constant); - } - - public void Maximize(LinearExpr expr) - { - Objective().Clear(); - Objective().SetMaximization(); - Dictionary coefficients = new Dictionary(); - double constant = expr.Visit(coefficients); - foreach (KeyValuePair pair in coefficients) - { - Objective().SetCoefficient(pair.Key, pair.Value); - } - Objective().SetOffset(constant); - } - - public void Minimize(Variable var) - { - Objective().Clear(); - Objective().SetMinimization(); - Objective().SetCoefficient(var, 1.0); - } - - public void Maximize(Variable var) - { - Objective().Clear(); - Objective().SetMaximization(); - Objective().SetCoefficient(var, 1.0); + array[i] = MakeVar(lb, ub, integer, ""); } + return array; } + public Variable[] MakeVarArray(int count, double lb, double ub, bool integer, string var_name) + { + Variable[] array = new Variable[count]; + for (int i = 0; i < count; ++i) + { + array[i] = MakeVar(lb, ub, integer, var_name + i); + } + return array; + } + + public Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + matrix[i, j] = MakeVar(lb, ub, integer, ""); + } + } + return matrix; + } + + public Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer, string name) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + matrix[i, j] = MakeVar(lb, ub, integer, var_name); + } + } + return matrix; + } + + public Variable[] MakeNumVarArray(int count, double lb, double ub) + { + return MakeVarArray(count, lb, ub, false); + } + + public Variable[] MakeNumVarArray(int count, double lb, double ub, string var_name) + { + return MakeVarArray(count, lb, ub, false, var_name); + } + + public Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + matrix[i, j] = MakeNumVar(lb, ub, ""); + } + } + return matrix; + } + + public Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub, string name) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + matrix[i, j] = MakeNumVar(lb, ub, var_name); + } + } + return matrix; + } + + public Variable[] MakeIntVarArray(int count, double lb, double ub) + { + return MakeVarArray(count, lb, ub, true); + } + + public Variable[] MakeIntVarArray(int count, double lb, double ub, string var_name) + { + return MakeVarArray(count, lb, ub, true, var_name); + } + + public Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + matrix[i, j] = MakeIntVar(lb, ub, ""); + } + } + return matrix; + } + + public Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub, string name) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + matrix[i, j] = MakeIntVar(lb, ub, var_name); + } + } + return matrix; + } + + public Variable[] MakeBoolVarArray(int count) + { + return MakeVarArray(count, 0.0, 1.0, true); + } + + public Variable[] MakeBoolVarArray(int count, string var_name) + { + return MakeVarArray(count, 0.0, 1.0, true, var_name); + } + + public Variable[,] MakeBoolVarMatrix(int rows, int cols) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + matrix[i, j] = MakeBoolVar(""); + } + } + return matrix; + } + + public Variable[,] MakeBoolVarMatrix(int rows, int cols, string name) + { + Variable[,] matrix = new Variable[rows, cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + string var_name = name + "[" + i + ", " + j + "]"; + matrix[i, j] = MakeBoolVar(var_name); + } + } + return matrix; + } + + public Constraint Add(LinearConstraint constraint) + { + return constraint.Extract(this); + } + + public void Minimize(LinearExpr expr) + { + Objective().Clear(); + Objective().SetMinimization(); + Dictionary coefficients = new Dictionary(); + double constant = expr.Visit(coefficients); + foreach (KeyValuePair pair in coefficients) + { + Objective().SetCoefficient(pair.Key, pair.Value); + } + Objective().SetOffset(constant); + } + + public void Maximize(LinearExpr expr) + { + Objective().Clear(); + Objective().SetMaximization(); + Dictionary coefficients = new Dictionary(); + double constant = expr.Visit(coefficients); + foreach (KeyValuePair pair in coefficients) + { + Objective().SetCoefficient(pair.Key, pair.Value); + } + Objective().SetOffset(constant); + } + + public void Minimize(Variable var) + { + Objective().Clear(); + Objective().SetMinimization(); + Objective().SetCoefficient(var, 1.0); + } + + public void Maximize(Variable var) + { + Objective().Clear(); + Objective().SetMaximization(); + Objective().SetCoefficient(var, 1.0); + } +} + } // namespace Google.OrTools.LinearSolver diff --git a/ortools/linear_solver/csharp/VariableHelper.cs b/ortools/linear_solver/csharp/VariableHelper.cs index 535de1d250..7b21708241 100644 --- a/ortools/linear_solver/csharp/VariableHelper.cs +++ b/ortools/linear_solver/csharp/VariableHelper.cs @@ -13,209 +13,209 @@ namespace Google.OrTools.LinearSolver { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - // Patch the MPVariable class to support the natural language API. - public partial class Variable +// Patch the MPVariable class to support the natural language API. +public partial class Variable +{ + public static LinearExpr operator +(Variable a, double v) { - public static LinearExpr operator +(Variable a, double v) - { - return new VarWrapper(a) + v; - } - - public static LinearExpr operator +(double v, Variable a) - { - return a + v; - } - - public static LinearExpr operator +(Variable a, LinearExpr b) - { - return new VarWrapper(a) + b; - } - - public static LinearExpr operator +(Variable a, Variable b) - { - return new VarWrapper(a) + new VarWrapper(b); - } - - public static LinearExpr operator +(LinearExpr a, Variable b) - { - return a + new VarWrapper(b); - } - - public static LinearExpr operator -(Variable a, double v) - { - return new VarWrapper(a) - v; - } - - public static LinearExpr operator -(double v, Variable a) - { - return v - new VarWrapper(a); - } - - public static LinearExpr operator -(Variable a, LinearExpr b) - { - return new VarWrapper(a) - b; - } - - public static LinearExpr operator -(LinearExpr a, Variable b) - { - return a - new VarWrapper(b); - } - - public static LinearExpr operator -(Variable a, Variable b) - { - return new VarWrapper(a) - new VarWrapper(b); - } - - public static LinearExpr operator -(Variable a) - { - return -new VarWrapper(a); - } - - public static LinearExpr operator *(Variable a, double v) - { - return new VarWrapper(a) * v; - } - - public static LinearExpr operator /(Variable a, double v) - { - return new VarWrapper(a) / v; - } - - public static LinearExpr operator *(double v, Variable a) - { - return v * new VarWrapper(a); - } - - public static RangeConstraint operator ==(Variable a, double v) - { - return new VarWrapper(a) == v; - } - - public static RangeConstraint operator ==(double v, Variable a) - { - return v == new VarWrapper(a); - } - - public static RangeConstraint operator !=(Variable a, double v) - { - return new VarWrapper(a) != v; - } - - public static RangeConstraint operator !=(double v, Variable a) - { - return new VarWrapper(a) != v; - } - - public static Equality operator ==(Variable a, LinearExpr b) - { - return new VarWrapper(a) == b; - } - - public static Equality operator ==(LinearExpr a, Variable b) - { - return a == new VarWrapper(b); - } - - public static VarEquality operator ==(Variable a, Variable b) - { - return new VarEquality(a, b, true); - } - - public static Equality operator !=(Variable a, LinearExpr b) - { - return new VarWrapper(a) != b; - } - - public static Equality operator !=(LinearExpr a, Variable b) - { - return a != new VarWrapper(b); - } - - public static VarEquality operator !=(Variable a, Variable b) - { - return new VarEquality(a, b, false); - } - - public static RangeConstraint operator <=(Variable a, double v) - { - return new VarWrapper(a) <= v; - } - - public static RangeConstraint operator >=(Variable a, double v) - { - return new VarWrapper(a) >= v; - } - - public static RangeConstraint operator <=(double v, Variable a) - { - return new VarWrapper(a) >= v; - } - - public static RangeConstraint operator >=(double v, Variable a) - { - return new VarWrapper(a) <= v; - } - - public static RangeConstraint operator <=(Variable a, LinearExpr b) - { - return new VarWrapper(a) <= b; - } - - public static RangeConstraint operator >=(Variable a, LinearExpr b) - { - return new VarWrapper(a) >= b; - } - - public static RangeConstraint operator <=(Variable a, Variable b) - { - return new VarWrapper(a) <= new VarWrapper(b); - } - - public static RangeConstraint operator >=(Variable a, Variable b) - { - return new VarWrapper(a) >= new VarWrapper(b); - } - - public static RangeConstraint operator <=(LinearExpr a, Variable b) - { - return a <= new VarWrapper(b); - } - - public static RangeConstraint operator >=(LinearExpr a, Variable b) - { - return a >= new VarWrapper(b); - } + return new VarWrapper(a) + v; } - // TODO(user): Try to move this code back to the .swig with @define macros. - public partial class MPVariableVector : IDisposable, - System.Collections.IEnumerable + public static LinearExpr operator +(double v, Variable a) + { + return a + v; + } + + public static LinearExpr operator +(Variable a, LinearExpr b) + { + return new VarWrapper(a) + b; + } + + public static LinearExpr operator +(Variable a, Variable b) + { + return new VarWrapper(a) + new VarWrapper(b); + } + + public static LinearExpr operator +(LinearExpr a, Variable b) + { + return a + new VarWrapper(b); + } + + public static LinearExpr operator -(Variable a, double v) + { + return new VarWrapper(a) - v; + } + + public static LinearExpr operator -(double v, Variable a) + { + return v - new VarWrapper(a); + } + + public static LinearExpr operator -(Variable a, LinearExpr b) + { + return new VarWrapper(a) - b; + } + + public static LinearExpr operator -(LinearExpr a, Variable b) + { + return a - new VarWrapper(b); + } + + public static LinearExpr operator -(Variable a, Variable b) + { + return new VarWrapper(a) - new VarWrapper(b); + } + + public static LinearExpr operator -(Variable a) + { + return -new VarWrapper(a); + } + + public static LinearExpr operator *(Variable a, double v) + { + return new VarWrapper(a) * v; + } + + public static LinearExpr operator /(Variable a, double v) + { + return new VarWrapper(a) / v; + } + + public static LinearExpr operator *(double v, Variable a) + { + return v * new VarWrapper(a); + } + + public static RangeConstraint operator ==(Variable a, double v) + { + return new VarWrapper(a) == v; + } + + public static RangeConstraint operator ==(double v, Variable a) + { + return v == new VarWrapper(a); + } + + public static RangeConstraint operator !=(Variable a, double v) + { + return new VarWrapper(a) != v; + } + + public static RangeConstraint operator !=(double v, Variable a) + { + return new VarWrapper(a) != v; + } + + public static Equality operator ==(Variable a, LinearExpr b) + { + return new VarWrapper(a) == b; + } + + public static Equality operator ==(LinearExpr a, Variable b) + { + return a == new VarWrapper(b); + } + + public static VarEquality operator ==(Variable a, Variable b) + { + return new VarEquality(a, b, true); + } + + public static Equality operator !=(Variable a, LinearExpr b) + { + return new VarWrapper(a) != b; + } + + public static Equality operator !=(LinearExpr a, Variable b) + { + return a != new VarWrapper(b); + } + + public static VarEquality operator !=(Variable a, Variable b) + { + return new VarEquality(a, b, false); + } + + public static RangeConstraint operator <=(Variable a, double v) + { + return new VarWrapper(a) <= v; + } + + public static RangeConstraint operator >=(Variable a, double v) + { + return new VarWrapper(a) >= v; + } + + public static RangeConstraint operator <=(double v, Variable a) + { + return new VarWrapper(a) >= v; + } + + public static RangeConstraint operator >=(double v, Variable a) + { + return new VarWrapper(a) <= v; + } + + public static RangeConstraint operator <=(Variable a, LinearExpr b) + { + return new VarWrapper(a) <= b; + } + + public static RangeConstraint operator >=(Variable a, LinearExpr b) + { + return new VarWrapper(a) >= b; + } + + public static RangeConstraint operator <=(Variable a, Variable b) + { + return new VarWrapper(a) <= new VarWrapper(b); + } + + public static RangeConstraint operator >=(Variable a, Variable b) + { + return new VarWrapper(a) >= new VarWrapper(b); + } + + public static RangeConstraint operator <=(LinearExpr a, Variable b) + { + return a <= new VarWrapper(b); + } + + public static RangeConstraint operator >=(LinearExpr a, Variable b) + { + return a >= new VarWrapper(b); + } +} + +// TODO(user): Try to move this code back to the .swig with @define macros. +public partial class MPVariableVector : IDisposable, + System.Collections.IEnumerable #if !SWIG_DOTNET_1 - , - System.Collections.Generic.IList + , + System.Collections.Generic.IList #endif +{ + // cast from C# MPVariable array + public static implicit operator MPVariableVector(Variable[] inVal) { - // cast from C# MPVariable array - public static implicit operator MPVariableVector(Variable[] inVal) + var outVal = new MPVariableVector(); + foreach (Variable element in inVal) { - var outVal = new MPVariableVector(); - foreach (Variable element in inVal) - { - outVal.Add(element); - } - return outVal; - } - - // cast to C# MPVariable array - public static implicit operator Variable[](MPVariableVector inVal) - { - var outVal = new Variable[inVal.Count]; - inVal.CopyTo(outVal); - return outVal; + outVal.Add(element); } + return outVal; } + // cast to C# MPVariable array + public static implicit operator Variable[](MPVariableVector inVal) + { + var outVal = new Variable[inVal.Count]; + inVal.CopyTo(outVal); + return outVal; + } +} + } // namespace Google.OrTools.LinearSolver diff --git a/ortools/util/csharp/NestedArrayHelper.cs b/ortools/util/csharp/NestedArrayHelper.cs index 1977ce593e..1714bd9e20 100644 --- a/ortools/util/csharp/NestedArrayHelper.cs +++ b/ortools/util/csharp/NestedArrayHelper.cs @@ -14,54 +14,54 @@ namespace Google.OrTools { - using System; - using System.Collections.Generic; +using System; +using System.Collections.Generic; - public static class NestedArrayHelper +public static class NestedArrayHelper +{ + public static T[] GetFlatArray(T[][] arr) { - public static T[] GetFlatArray(T[][] arr) + int flatLength = 0; + for (var i = 0; i < arr.GetLength(0); i++) + flatLength += arr[i].GetLength(0); + + int idx = 0; + T[] flat = new T[flatLength]; + + for (int i = 0; i < arr.GetLength(0); i++) { - int flatLength = 0; - for (var i = 0; i < arr.GetLength(0); i++) - flatLength += arr[i].GetLength(0); - - int idx = 0; - T[] flat = new T[flatLength]; - - for (int i = 0; i < arr.GetLength(0); i++) - { - for (int j = 0; j < arr[i].GetLength(0); j++) - flat[idx++] = arr[i][j]; - } - - return flat; + for (int j = 0; j < arr[i].GetLength(0); j++) + flat[idx++] = arr[i][j]; } - public static T[] GetFlatArrayFromMatrix(T[,] arr) - { - int flatLength = arr.GetLength(0) * arr.GetLength(1); - - int idx = 0; - T[] flat = new T[flatLength]; - - for (int i = 0; i < arr.GetLength(0); i++) - { - for (int j = 0; j < arr.GetLength(1); j++) - flat[idx++] = arr[i, j]; - } - - return flat; - } - - public static int[] GetArraySecondSize(T[][] arr) - { - var result = new int[arr.GetLength(0)]; - for (var i = 0; i < arr.GetLength(0); i++) - { - if (arr[i] != null) - result[i] = arr[i].Length; - } - return result; - } + return flat; } + + public static T[] GetFlatArrayFromMatrix(T[,] arr) + { + int flatLength = arr.GetLength(0) * arr.GetLength(1); + + int idx = 0; + T[] flat = new T[flatLength]; + + for (int i = 0; i < arr.GetLength(0); i++) + { + for (int j = 0; j < arr.GetLength(1); j++) + flat[idx++] = arr[i, j]; + } + + return flat; + } + + public static int[] GetArraySecondSize(T[][] arr) + { + var result = new int[arr.GetLength(0)]; + for (var i = 0; i < arr.GetLength(0); i++) + { + if (arr[i] != null) + result[i] = arr[i].Length; + } + return result; + } +} } // namespace Google.OrTools diff --git a/ortools/util/csharp/ProtoHelper.cs b/ortools/util/csharp/ProtoHelper.cs index 353f78e0bb..2de2cbc43b 100644 --- a/ortools/util/csharp/ProtoHelper.cs +++ b/ortools/util/csharp/ProtoHelper.cs @@ -14,18 +14,18 @@ namespace Google.OrTools { - using System; - using Google.Protobuf; +using System; +using Google.Protobuf; - public static class ProtoHelper +public static class ProtoHelper +{ + public static byte[] ProtoToByteArray(IMessage message) { - public static byte[] ProtoToByteArray(IMessage message) - { - int size = message.CalculateSize(); - byte[] buffer = new byte[size]; - CodedOutputStream output = new CodedOutputStream(buffer); - message.WriteTo(output); - return buffer; - } + int size = message.CalculateSize(); + byte[] buffer = new byte[size]; + CodedOutputStream output = new CodedOutputStream(buffer); + message.WriteTo(output); + return buffer; } +} } // namespace Google.OrTools