fslaborg / flips

Fsharp LInear Programming System
https://flipslibrary.com/#/
MIT License
253 stars 32 forks source link

Question - Using objective expression in a constraint #181

Closed Iwke closed 2 years ago

Iwke commented 2 years ago

Hello,

I am rather new to Flips and LP and was doing some reading and trying out stuff in an example. I was reading Model Building in Mathematical Programming and tried modeling first problem in Flips The discussion from the book proposes a following model: image

Now in Fsharp I would do something like: let objectiveExpression = ... // LinearExpression let constraint = Constraint.create "SomeConstraint" (... - 6.0 * objectiveExpression <== 0)

As soon as I would introduce a constraint such as this into the model, all the decisions would evaluate to 0.

In multiple examples I saw on Flips web site I could not find a similar problem. Am I on the right track here? Is this even possible to do with Flips?

Thanks, Ivan.

TysonMN commented 2 years ago

Can you share a link to a GitHub repository containing your code?

Iwke commented 2 years ago

Hey sorry for a bit of delay, but here's the repo https://github.com/Iwke/flips-exercises/

Please ignore the entire month/prices matrix, at the moment I am just trying to get optimal values for January.

matthewcrews commented 2 years ago

I'll be looking at this later today.

matthewcrews commented 2 years ago

Could you provide the page number you are looking at in "Model Building in Mathematical Programming" for me to reference?

I don't see where you need to create a constraint using the Objective Function. What's making you think you need to create something like this...

let constraint = Constraint.create "SomeConstraint" (... - 6.0 * objectiveExpression <== 0)
Iwke commented 2 years ago

Hey kind of difficult to tell the exact page, since I am reading on oreilly. But it's the first problem from Chapter 12 (problem "Food manufacture 1") and Chapter 13 (formulation and discussion).

Regarding using objective in constraints, take a look at constraints on line 34, 35 and 37 (note that all use LinearExpression 'y' which I later use as objectiveExpression). If I add any of those in the solution all decisions will be 0. If I remove all of them I get some numbers that add up, but of course do not follow the constraints and therefore are not really useful.

matthewcrews commented 2 years ago

Here's a gist of how I would formulate it: https://gist.github.com/matthewcrews/a5c0c0c2790f4af66f95336d4e3bb0bc

It seems to solve without issue. Result I got: Profit: 17592.59259 x1: 159.2592593 x2: 40.74074074 x3: 0.0 x4: 250.0 x5: 0.0 y: 450.0