Skip to content

Commit 8ef400d

Browse files
authored
Merge pull request #4323 from CADBIMDeveloper/literal-as-expr
Add ILiteral.AsExpr() method
2 parents b19a39c + 817c9b3 commit 8ef400d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

ortools/sat/csharp/IntegerExpressions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public interface ILiteral
2828
ILiteral Not();
2929
/** <summary>Returns the logical index of the literal. </summary> */
3030
int GetIndex();
31+
/** <summary>Returns the literal as a linear expression.</summary> */
32+
LinearExpr AsExpr();
3133
/** <summary>Returns the Boolean negation of the literal as a linear expression.</summary> */
3234
LinearExpr NotAsExpr();
3335
}
@@ -803,6 +805,12 @@ public ILiteral Not()
803805
return negation_ ??= new NotBoolVar(this);
804806
}
805807

808+
/** <summary>Returns the literal as a linear expression.</summary> */
809+
public LinearExpr AsExpr()
810+
{
811+
return this;
812+
}
813+
806814
/** <summary> Returns the Boolean negation of that variable as a linear expression.</summary> */
807815
public LinearExpr NotAsExpr()
808816
{
@@ -836,6 +844,11 @@ public ILiteral Not()
836844
return boolvar_;
837845
}
838846

847+
public LinearExpr AsExpr()
848+
{
849+
return 1 - boolvar_;
850+
}
851+
839852
public LinearExpr NotAsExpr()
840853
{
841854
return (LinearExpr)Not();

ortools/sat/csharp/SatSolverTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,54 @@ public void LinearExprBoolVarOperatorTest()
495495
Console.WriteLine(e);
496496
}
497497

498+
[Fact]
499+
public void TrueLiteralAsExpressionTest()
500+
{
501+
Console.WriteLine("TrueLiteralAsExpressionTest");
502+
CpModel model = new CpModel();
503+
ILiteral v = model.TrueLiteral();
504+
LinearExpr e = v.AsExpr() * 2;
505+
Console.WriteLine(e);
506+
e = 2 * v.AsExpr();
507+
Console.WriteLine(e);
508+
e = v.AsExpr() + 2;
509+
Console.WriteLine(e);
510+
e = 2 + v.AsExpr();
511+
Console.WriteLine(e);
512+
e = v.AsExpr();
513+
Console.WriteLine(e);
514+
e = -v.AsExpr();
515+
Console.WriteLine(e);
516+
e = 1 - v.AsExpr();
517+
Console.WriteLine(e);
518+
e = v.AsExpr() - 1;
519+
Console.WriteLine(e);
520+
}
521+
522+
[Fact]
523+
public void FalseLiteralAsExpressionTest()
524+
{
525+
Console.WriteLine("FalseLiteralAsExpressionTest");
526+
CpModel model = new CpModel();
527+
ILiteral v = model.FalseLiteral();
528+
LinearExpr e = v.AsExpr() * 2;
529+
Console.WriteLine(e);
530+
e = 2 * v.AsExpr();
531+
Console.WriteLine(e);
532+
e = v.AsExpr() + 2;
533+
Console.WriteLine(e);
534+
e = 2 + v.AsExpr();
535+
Console.WriteLine(e);
536+
e = v.AsExpr();
537+
Console.WriteLine(e);
538+
e = -v.AsExpr();
539+
Console.WriteLine(e);
540+
e = 1 - v.AsExpr();
541+
Console.WriteLine(e);
542+
e = v.AsExpr() - 1;
543+
Console.WriteLine(e);
544+
}
545+
498546
[Fact]
499547
public void LinearExprNotBoolVarOperatorTest()
500548
{

0 commit comments

Comments
 (0)