Can't seem to get Constraint.Implies() working.

43 views
Skip to first unread message

s.vanho...@gmail.com

unread,
May 12, 2020, 7:29:33 AM5/12/20
to OPTANO Modeling
As a student ICT & Software Engineering I am currently working on a prototype crop planning optimization with Optano modelling in .NET C# using the GLPK Solver.
Unfortunately I can't seem to get the Constraint.Implies() method working in my model.

To provide you with some context I have setup a simple demo: https://github.com/sandervanhooff1997/OptanoImpliesDemo

What I want to achieve in this demo is to build a conditional constraint with the Constraint.Implies() method:
- if a crop was planted on day 1
- no crops can be planted on day 2 + 3

- if a crop was planted on day 2
- no crops can be planted on day 3 + 4

- etc. etc.

Data:
- horizon of 9 days;
- 1 crop 'Crop A'.

Expected output:
day 1 Crop A
day 2 -
day 3 -
day 4 Crop A
day 5 -
day 6 -
day 7 Crop A
day 8 -
day 9 -

Actual output:
day 1 Crop A
day 2 Crop A
day 3 Crop A
day 4 Crop A
day 5 Crop A
day 6 Crop A
day 7 Crop A
day 8 Crop A
day 9 Crop A

Please look at the demo on Github to better understand the implementation in C#.

Kind regards,
Sander van Hooff

OPTANO Team

unread,
May 12, 2020, 11:12:57 AM5/12/20
to OPTANO Modeling
Well, I am not sure what's wrong with the code, example or glpk adapter.
(we are going to look into this).

You may use this patch to fix the implication on binaries:


---
 
OptanoImplies/Program.cs | 16 ++++++----------
 
1 file changed, 6 insertions(+), 10 deletions(-)

diff
--git a/OptanoImplies/Program.cs b/OptanoImplies/Program.cs
index
8408d7a..50a2a44 100644
--- a/OptanoImplies/Program.cs
+++ b/OptanoImplies/Program.cs
@@ -47,20 +47,16 @@ namespace OptanoImplies
 
                         
if (cropGrowing.Count() == 2)
                         
{
-                            // crop assignment current crop on current day = 1
-                            var cropPlantedOnDayConstraint = new Constraint(CropAssignment[day, crop], "", 1, 1);
-
-                            // sum of crop assignment for all crops on next two days = 0
-                            var cropGrowingOnDaysConstraint = new Constraint(Expression.Sum(cropGrowing.SelectMany(d => crops.Select(c => CropAssignment[d, c]))), "", 0, 0);
-
-                            // if cropPlantedOnDayConstraint is fulfilled, cropGrowingOnDaysConstraint must also be fulfilled
-                            var impliesConstraint = cropPlantedOnDayConstraint.Implies(cropGrowingOnDaysConstraint);
-                            model.AddConstraint(impliesConstraint);
+                            // set otherDay to zero, if the crop is planted on day.
+                            foreach (var otherDay in cropGrowing)
+                            {
+                                model.AddConstraint(-CropAssignment[day, crop] + 1 >= CropAssignment[otherDay, crop]);
+                            }
                         
}
                         
else
                         
{
                             
// the crop can't finish before end of horizon
-                            var cropCantFinishGrowingConstraint = new Constraint(CropAssignment[day, crop], "", 0, 0);
+                            var cropCantFinishGrowingConstraint = CropAssignment[day, crop] == 0;
                             model
.AddConstraint(cropCantFinishGrowingConstraint);
                         
}
                     
}
--
2.26.1.windows.1

Best,
jp

s.vanho...@gmail.com

unread,
May 12, 2020, 11:40:06 AM5/12/20
to OPTANO Modeling
Hi jp,

Thanks for your response.
For now I will implement your workaround and hope you succeed in providing a working solution using the implication.

I will check this post for updates from time to time.

jannic...@optano.com

unread,
Jul 16, 2020, 9:45:52 AM7/16/20
to OPTANO Modeling
Hi Sander,

we just released a new version of Optano Modeling, that should resolve this issue.

Best Regards,
Jannick
Reply all
Reply to author
Forward
0 new messages