No idea why this isn't working

38 views
Skip to first unread message

nye...@gmail.com

unread,
Jan 10, 2018, 7:26:06 AM1/10/18
to AMPL Modeling Language
I'm trying to run this code to determine the quantity of each product to buy (x1, x2, x3). They each have a profit of (1.5, 3.2, 2.7) respectively per unit produced however, they all need to go through a quality assurance process. There are 4 processes. the table of hours it takes for the process of each unit of product is given below:

Processes  Apples(x1) Grapes(x2) Pears(x3)
            1           0.45           0.55           0.70
            2           0.76           0.45           0.40
            3           0.33           0.63           0.72
            4           0.54           0.77           0.48

The two constraints are:
1) Each process is given 3000 hours max.
2) Each product must go through minimum 2 processes.

The objective is to maximize profit.

Here is my .mod file:

set product;
set quality;

param prof{product};
param hrs:=3000;
param qual_hrs_per{quality, product};

var ifprocess{q in quality} binary;
var quantity{p in product}>=0,integer;

maximize

prf: sum{p in product}prof[p]*quantity[p];

subject to 
process_chosen: sum{q in quality}ifprocess[q]>=2;
maxhours1: sum{p in product}qual_hrs_per[1,p]*ifprocess[1]*quantity[p]<= hrs;
maxhours2: sum{p in product}qual_hrs_per[2,p]*ifprocess[2]*quantity[p]<= hrs;
maxhours3: sum{p in product}qual_hrs_per[3,p]*ifprocess[3]*quantity[p]<= hrs;
maxhours4: sum{p in product}qual_hrs_per[4,p]*ifprocess[4]*quantity[p]<= hrs;

Here is my .dat file:

set product:= Apples Grapes Pears;
set quality:= 1 2 3 4;

param prof:= Apples 1.5 Grapes 3.2 Pears 2.7;

param qual_hrs_per:     Apples Grapes Pears:=
1 0.45  0.55  0.70
2 0.76  0.45  0.40
3 0.33  0.63  0.72
4 0.54  0.77  0.48;

When I run it I get the error "QP Hessian is not positive semi-definite." I'm not 100% sure i'm using the binary variable correctly.

Any help would be great!

DragonHeart C

unread,
Jan 10, 2018, 2:08:40 PM1/10/18
to AMPL Modeling Language
You can't multiply binary variable and integer variable, that is not linear and hence CPLEX can't solve it. 

You can try the following modification

maxhours1: sum{p in product}qual_hrs_per[1,p]*quantity[p] <= hrs*ifprocess[1]

so if binary variable takes 1, the hrs is a positive value, otherwise it is a 0. 

ptiwari

unread,
Jan 10, 2018, 4:47:05 PM1/10/18
to AMPL Modeling Language
You can find more about linearizing the problem at https://orinanobworld.blogspot.mx/2010/10/binary-variables-and-quadratic-terms.html. Moreover, you could write your constraint as follows:

subject to maxhours{q in quality}: ....

nye...@gmail.com

unread,
Jan 11, 2018, 5:11:13 AM1/11/18
to AMPL Modeling Language
I've tried that and it's fine when the binary variable takes 1 (when the quality assurance process is chosen) however when it takes 0 (when the process is not chosen) the constraint forces my "quantity[p]" variable to be 0 (as the constraint becomes <=0) which screws it up for the other constraints. I need to find a way to get rid of the constraint (<=hrs) or to ignore it if the process is not included.

ptiwari

unread,
Jan 11, 2018, 1:59:02 PM1/11/18
to AMPL Modeling Language
I think your variable ifprocess doesn't capture the constraint for each product. You should have 
var ifprocess{product,quality};

and 

subject to {p in product}: sum{q in quality} ifprocess[p,q] >=2;

This way at least two process is applied for each product. 

Next thing is to linearize your quadratic constraint.

Does your variably quantity has upper bound? If you have upper bound then you could declare new variable 'z' and add four constraints mentioned at https://orinanobworld.blogspot.mx/2010/10/binary-variables-and-quadratic-terms.html instead of your maxhours1 constraint. 

Thanks
Paras
Reply all
Reply to author
Forward
0 new messages