-1

I am trying to setup a constraint which depends on the minimized function value.

The problem I have is of the following nature:

fmin = minimize (d1x1 +d2x2 ... +d5x5)

Where I want to optmize with following constraints:

x1+X2+x3+x4+x5 = 1

0.003 <x1 .. X5  < 0.05

d1x1/fmin = y1
(d2x2+d3x4)/fmin = y2
(d4x4+d5x5)/fmin = y3

Here in this case y1.. yn are scalar constants.

   The problem I am having is that I dont know how to setup the A_ub or A_eq 
   In linprog so that B_ub = y1*fmin for d1x1 for example. 

So somehow I need to define:

x1d1/fmin = y1 as one of the constraints.

Here the optimal value vector will be (d1 .. dn). However, this must also satisfy the constraint d1/minimized(d1.. dn) = y1 as an example here.

How should I set this up ? What kind of optimizer do I use ?

I am able to do this very easily using excel solver - but now I want to code this in python. I am trying using scipy.linprog but I am not sure if this is a linear programming problem or do I need to use another approach. I am not able to think of a way to setup the constraints in linprog for this problem. Can anyone help me ?

DrBug
  • 2,004
  • 2
  • 20
  • 21
  • I have tried to use scipy.linprog where I am able to setup the first two constraints but stumped in setting up the latter ones. I am not even sure if I need to use linprog for this or not. More than a code based - its an approach that I am really after. – DrBug Dec 23 '16 at 11:44
  • 1
    @DrBug What are `y1`, `y2` and `y3`? Variables or constants? **If** they are constants then you can rewrite the constraint as `d4x4 + d5x5 = y3 * sumof(...)` which is linear, if `yi`s are variables that constraint is **not** linear, and hence you have to use something more powerful, like a quadratic solver. – Bakuriu Dec 23 '16 at 12:04
  • Thanks Bakuriu. y1.. y3 are constants. However, the sumof term also includes the variables that I am trying to minimize (d1.. dn). In this case the first constraint which I am struggling with is actually (d1x1+d2x2)/(minimized function). – DrBug Dec 23 '16 at 12:08
  • @Bakuriu is right first linearize your constraints then look [here](https://docs.scipy.org/doc/scipy/reference/optimize.linprog-simplex.html) and see if you can follow the example. – pbreach Dec 23 '16 at 12:15
  • @pbreach: your comment does not help. I am not able to figure out how to setup a constraint which depends on the interim values of the minimization function. – DrBug Dec 23 '16 at 12:20
  • @pbreach: I have edited my question to make it more readable based on your response. I am able to explain well now ? – DrBug Dec 23 '16 at 12:31
  • I understand your problem but I'm not jut going to solve it for you. Show a legitimate attempt and we can go from there. – pbreach Dec 23 '16 at 13:02
  • @pbreach. I can put up some code but I need to write custom code for this example as I cannot publish the code I am exactly working on. I am going in the direction of non linear constraints and abandoning linprog. I was looking for an answer like - linprog wont do, use minimize from scipy with non linear custom constraints. At this stage I dont even know if this is the right direction. But I think you are more interested in schooling than helping. – DrBug Dec 23 '16 at 13:12
  • @pbreach: And I am not a school kid trying to get my homework done by you. I am a old hog who just want to get unstuck with python for now. – DrBug Dec 23 '16 at 13:14
  • @pbreach: And your reputation is not that high your highness to preach. People come here to seek help when they are tired of google.And its precisely comments like yours which turn me off. – DrBug Dec 23 '16 at 13:18

1 Answers1

1

Assuming that d1, ..., dn are scalar constants too, then for instance the constraint

d1*x1/fmin==y1

can be rewritten as

d1*x1==y1*d1*x1+y1*d2*x2+...+y1*dn*xn

This can be normalized to

(d1-y1*d1)*x1 - y1*d2*x2 - y1*d3*x3 - ... - y1*dn*xn == 0

which can be used as input for a linear solver.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • Yes I agree. I somehow was thinking that A_eq and A_ub needs to be a binary matrix. I will try it out and if it works I will accept your answer. – DrBug Dec 23 '16 at 14:59