Skip to content

Commit

Permalink
[CP-SAT] add hinting API for literals + test
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Aug 13, 2024
1 parent 1d4c84f commit de27013
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ortools/sat/csharp/CpModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,23 @@ public void AddHint(IntVar var, long value)
model_.SolutionHint.Values.Add(value);
}

/** <summary>Adds variable hinting to the model.</summary>*/
public void AddHint(ILiteral lit, bool value)
{
model_.SolutionHint ??= new PartialVariableAssignment();
int index = lit.GetIndex();
if (index >= 0)
{
model_.SolutionHint.Vars.Add(index);
model_.SolutionHint.Values.Add(value ? 1 : 0);
}
else
{
model_.SolutionHint.Vars.Add(Negated(index));
model_.SolutionHint.Values.Add(value ? 0 : 1);
}
}

/** <summary>Clears all hinting from the model.</summary>*/
public void ClearHints()
{
Expand Down
20 changes: 20 additions & 0 deletions ortools/sat/csharp/SatSolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,5 +752,25 @@ public void TestInterval()
Assert.Equal("3", i.SizeExpr().ToString());
Assert.Equal("(v + 3)", i.EndExpr().ToString());
}

[Fact]
public void TestHint()
{
output_.WriteLine("TestInterval test");
CpModel model = new CpModel();
IntVar v = model.NewIntVar(-10, 10, "v");
BoolVar b = model.NewBoolVar("b");
BoolVar c = model.NewBoolVar("c");
model.AddHint(v, 3);
model.AddHint(b, false);
model.AddHint(c.Not(), false);
Assert.Equal(3, model.Model.SolutionHint.Vars.Count);
Assert.Equal(0, model.Model.SolutionHint.Vars[0]);
Assert.Equal(3, model.Model.SolutionHint.Values[0]);
Assert.Equal(1, model.Model.SolutionHint.Vars[1]);
Assert.Equal(0, model.Model.SolutionHint.Values[1]);
Assert.Equal(2, model.Model.SolutionHint.Vars[2]);
Assert.Equal(1, model.Model.SolutionHint.Values[2]);
}
}
} // namespace Google.OrTools.Tests

0 comments on commit de27013

Please sign in to comment.