From b886f1ddbcfff84a760313b98fc4c33fc8b202b3 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Thu, 16 May 2019 11:45:46 +0200 Subject: [PATCH] fix ShiftSchedulingSat.cs; improve linear code in CP-SAT C# to make it more robust --- examples/dotnet/ShiftSchedulingSat.cs | 7 +++---- ortools/sat/csharp/IntegerExpressions.cs | 9 ++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/dotnet/ShiftSchedulingSat.cs b/examples/dotnet/ShiftSchedulingSat.cs index 9a46d717af..2a06adb572 100644 --- a/examples/dotnet/ShiftSchedulingSat.cs +++ b/examples/dotnet/ShiftSchedulingSat.cs @@ -197,7 +197,7 @@ public class ShiftSchedulingSat { foreach (int w in Range(numWeeks)) { - var works = new IntVar[numDays]; + var works = new IntVar[7]; foreach (int d in Range(7)) { @@ -284,10 +284,9 @@ public class ShiftSchedulingSat // Solve model var solver = new CpSolver(); - solver.StringParameters = "num_search_workers:8"; + solver.StringParameters = "num_search_workers:8, log_search_progress: true"; - var solutionPrinter = new ObjectiveSolutionPrinter(); - var status = solver.SolveWithSolutionCallback(model, solutionPrinter); + var status = solver.Solve(model); // Print solution if (status == CpSolverStatus.Optimal || status == CpSolverStatus.Feasible) diff --git a/ortools/sat/csharp/IntegerExpressions.cs b/ortools/sat/csharp/IntegerExpressions.cs index 278a0558ab..1f7d1f48af 100644 --- a/ortools/sat/csharp/IntegerExpressions.cs +++ b/ortools/sat/csharp/IntegerExpressions.cs @@ -223,8 +223,10 @@ namespace Google.OrTools.Sat { List exprs = new List(); List coeffs = new List(); - exprs.Add(e); - coeffs.Add(initial_coeff); + if ((Object)e != null) { + exprs.Add(e); + coeffs.Add(initial_coeff); + } long constant = 0; while (exprs.Count > 0) @@ -233,7 +235,7 @@ namespace Google.OrTools.Sat exprs.RemoveAt(0); long coeff = coeffs[0]; coeffs.RemoveAt(0); - if (coeff == 0) continue; + if (coeff == 0 || (Object)expr == null) continue; if (expr is ProductCst) { @@ -468,6 +470,7 @@ namespace Google.OrTools.Sat for (int i = 0; i < expressions_.Length; ++i) { LinearExpr expr = expressions_[i]; + if ((Object)expr == null) continue; long coeff = coefficients_[i]; if (i != 0) {