fix ShiftSchedulingSat.cs; improve linear code in CP-SAT C# to make it more robust

This commit is contained in:
Laurent Perron
2019-05-16 11:45:46 +02:00
parent dbef2bb42f
commit b886f1ddbc
2 changed files with 9 additions and 7 deletions

View File

@@ -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)

View File

@@ -223,8 +223,10 @@ namespace Google.OrTools.Sat
{
List<LinearExpr> exprs = new List<LinearExpr>();
List<long> coeffs = new List<long>();
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)
{