File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ public interface ILiteral
28
28
ILiteral Not ( ) ;
29
29
/** <summary>Returns the logical index of the literal. </summary> */
30
30
int GetIndex ( ) ;
31
+ /** <summary>Returns the literal as a linear expression.</summary> */
32
+ LinearExpr AsExpr ( ) ;
31
33
/** <summary>Returns the Boolean negation of the literal as a linear expression.</summary> */
32
34
LinearExpr NotAsExpr ( ) ;
33
35
}
@@ -803,6 +805,12 @@ public ILiteral Not()
803
805
return negation_ ??= new NotBoolVar ( this ) ;
804
806
}
805
807
808
+ /** <summary>Returns the literal as a linear expression.</summary> */
809
+ public LinearExpr AsExpr ( )
810
+ {
811
+ return this ;
812
+ }
813
+
806
814
/** <summary> Returns the Boolean negation of that variable as a linear expression.</summary> */
807
815
public LinearExpr NotAsExpr ( )
808
816
{
@@ -836,6 +844,11 @@ public ILiteral Not()
836
844
return boolvar_ ;
837
845
}
838
846
847
+ public LinearExpr AsExpr ( )
848
+ {
849
+ return 1 - boolvar_ ;
850
+ }
851
+
839
852
public LinearExpr NotAsExpr ( )
840
853
{
841
854
return ( LinearExpr ) Not ( ) ;
Original file line number Diff line number Diff line change @@ -495,6 +495,54 @@ public void LinearExprBoolVarOperatorTest()
495
495
Console . WriteLine ( e ) ;
496
496
}
497
497
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
+
498
546
[ Fact ]
499
547
public void LinearExprNotBoolVarOperatorTest ( )
500
548
{
You can’t perform that action at this time.
0 commit comments