Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › CPLEX Programming [Problem Solved]
New Posts  All Forums:Forum Nav:

CPLEX Programming [Problem Solved]

post #1 of 8
Thread Starter 
I know this is a long shot, but has anyone here had any experience using CPLEX?

I'm trying to code an ILP, but I'm having trouble with one of the constraints.

In plain english: A company is selling three products (which have other constraints as well) and they don't want any one product to account for more than 40% of the total products manufactured.

Mathematically:

sum( fraction( product[p] ) ) for all p = 1

fraction( product[p] ) <= 0.4

My problem is that I don't think this is a linear constraint, and I'm not sure how to make it one / define it in CPLEX.

This is the code I came up with, but it won't compile, though it does make sense mathematically:

Code:
for (p = 0; p < numProducts; p++) {
IloExpr expr5(env);
IloExpr expr6(env);

for(q = 0; q < numProducts; q++){
expr6 += assign[q];
}
expr5 = assign[p];
model.add( ( expr5 / expr6 ) <= maxFraction);
        expr5.end(); expr6.end();
}


Thanks for the help guys.
Edited by TEntel - 3/5/11 at 8:00pm
post #2 of 8
Makes no sense. Here is your problem:

model.add( ( expr5 / expr6 ) <= maxFraction);


.add (exp1 , exp2)

exp1 is (expr5 / expr6) -- this is OK but the second half <= maxFraction makes no "mathematical" sense.
The New Girlfried
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD 555 X2 BE --------- unlocked to X4 oc. 3.7 GHz ASRock 870 EXTREME3 ATX PowerColor HD6850 Kingston HyperX Blu 4GB 1600 DDR3 
Hard DriveOptical DriveOSMonitor
SAMSUNG Spinpoint F3 500GB SATA 3.0Gb/s LG DVD±RW SuperMulti Drive Black SATA Windows 7 Samsung 23" 
PowerCase
OCZ ModXStream Pro 700W Thermaltake V6 BlackX Edition 
  hide details  
Reply
The New Girlfried
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD 555 X2 BE --------- unlocked to X4 oc. 3.7 GHz ASRock 870 EXTREME3 ATX PowerColor HD6850 Kingston HyperX Blu 4GB 1600 DDR3 
Hard DriveOptical DriveOSMonitor
SAMSUNG Spinpoint F3 500GB SATA 3.0Gb/s LG DVD±RW SuperMulti Drive Black SATA Windows 7 Samsung 23" 
PowerCase
OCZ ModXStream Pro 700W Thermaltake V6 BlackX Edition 
  hide details  
Reply
post #3 of 8
Thread Starter 
My code is trying to divide the amount of a product p, by the sum of all products.

so expr5 = assign[p] = number of a particular product
expr6 = sum of all assign[q]

I have no clue how to add this constraint to CPLEX. Since assign[] is an IloNumVarArray, I'm not sure how to manipulate it beyond the code I've posted.

My other constraints compile fine, so I'm not sure why extending the logic the way I did doesn't work.

Here is an example of working code:

In english: x of product p can be molded in one hour, and n hours will be allocated to molding.

Code:
for (p = 0; p < numProducts; p++) {
IloExpr expr1(env);
expr1 = assign[p];
model.add(expr1 <= mold_per_hour[p] * hoursMolding);
        expr1.end();
}
Compared to that, what exactly is the flaw in my original code? I thought it was the ( expr5 / expr6 ), but you're saying it is the ( <= )?
Edited by TEntel - 3/5/11 at 8:05pm
post #4 of 8
Yes, the <= is the problem.
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #5 of 8
Thread Starter 
But how else can I define this constraint?

It is easy to write on paper, but I don't understand how to show it in CPLEX.

I also don't understand why the <= operator is allowed in my second code snippet, but not the first.

EDIT: Problem solved.

By changing my code to:

Code:
for (p = 0; p < numProducts; p++) {
IloExpr expr5(env);
IloExpr total(env);

for(q = 0; q < numProducts; q++){
total += assign[q];
}
expr5 = assign[p];
model.add(  expr5 <= ( maxFraction * total ) ); expr5.end();total.end(); 
}
Everything works fine now, though I'm not sure why; all I did was algebraically rearrange my constraint in model.add();

If anyone understands why this solution worked, I'd love to know.

EDIT #2:

After thinking about it, I am sure the reason why my original code did not work was because CPLEX didn't like having a mathematical operation as the left side of an equation.
Edited by TEntel - 3/6/11 at 12:04am
post #6 of 8
Quote:
Originally Posted by TEntel View Post
But how else can I define this constraint?

It is easy to write on paper, but I don't understand how to show it in CPLEX.

I also don't understand why the <= operator is allowed in my second code snippet, but not the first.

HMMMM you're not a programmer, right? I don't even understand what are you trying to do. Add expr5 and expr6 if when divided the result is less than or equal to maxFraction? If yes you should do something like this:

if ((expr5 / expr6) <= maxFraction)
{
model.add (expr5, expr6);
}




Your problem has nothing to do with CPLEX it is the basics of modern programming languages. You have to understand the "for loop" and then you'll see the difference.

Without getting too technical (its late) look here:
for (p = 0; p < numProducts; p++) {
DO SOMETHING;
BLAH BLAH;

model.add( ( expr5 / expr6 ) <= maxFraction);

}

"for loop" requires theree params:
1. starting point - p=0
2. the condition (if true repeat again) - p < numProducts
3. the increment - p++ (selfexplanatory I hope)


".add" function takes two (or more params)
1. this could be the first param/value - (expr 5 / expr6)
2. the second param/value can not be <=maxFraction

IT DOESN'T WORK LOGICALLY. You're telling your function to add two elements, you supply the first element then you say "if less than or equal"!! It need the second param/value to add.

In conclusion, learn how "for loop" works and you'll see.
The New Girlfried
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD 555 X2 BE --------- unlocked to X4 oc. 3.7 GHz ASRock 870 EXTREME3 ATX PowerColor HD6850 Kingston HyperX Blu 4GB 1600 DDR3 
Hard DriveOptical DriveOSMonitor
SAMSUNG Spinpoint F3 500GB SATA 3.0Gb/s LG DVD±RW SuperMulti Drive Black SATA Windows 7 Samsung 23" 
PowerCase
OCZ ModXStream Pro 700W Thermaltake V6 BlackX Edition 
  hide details  
Reply
The New Girlfried
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD 555 X2 BE --------- unlocked to X4 oc. 3.7 GHz ASRock 870 EXTREME3 ATX PowerColor HD6850 Kingston HyperX Blu 4GB 1600 DDR3 
Hard DriveOptical DriveOSMonitor
SAMSUNG Spinpoint F3 500GB SATA 3.0Gb/s LG DVD±RW SuperMulti Drive Black SATA Windows 7 Samsung 23" 
PowerCase
OCZ ModXStream Pro 700W Thermaltake V6 BlackX Edition 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by TEntel View Post

Code:
for (p = 0; p < numProducts; p++) {
IloExpr expr1(env);
expr1 = assign[p];
model.add(expr1 <= mold_per_hour[p] * hoursMolding);
        expr1.end();
}
Compared to that, what exactly is the flaw in my original code? I thought it was the ( expr5 / expr6 ), but you're saying it is the ( <= )?
OK I just spotted the problem. I am not familiar with CPLEX but still...

expr1 <= mold_per_hour[p] ---- this is is not "less than" this is pulling the array element and assigning its value to expr1

Then multiplying expr1 * hoursMolding


So if you look at your code it is a huge mess. I said that you need to understand the "for loop" but you also need to understand how arrays work in this language.
The New Girlfried
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD 555 X2 BE --------- unlocked to X4 oc. 3.7 GHz ASRock 870 EXTREME3 ATX PowerColor HD6850 Kingston HyperX Blu 4GB 1600 DDR3 
Hard DriveOptical DriveOSMonitor
SAMSUNG Spinpoint F3 500GB SATA 3.0Gb/s LG DVD±RW SuperMulti Drive Black SATA Windows 7 Samsung 23" 
PowerCase
OCZ ModXStream Pro 700W Thermaltake V6 BlackX Edition 
  hide details  
Reply
The New Girlfried
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD 555 X2 BE --------- unlocked to X4 oc. 3.7 GHz ASRock 870 EXTREME3 ATX PowerColor HD6850 Kingston HyperX Blu 4GB 1600 DDR3 
Hard DriveOptical DriveOSMonitor
SAMSUNG Spinpoint F3 500GB SATA 3.0Gb/s LG DVD±RW SuperMulti Drive Black SATA Windows 7 Samsung 23" 
PowerCase
OCZ ModXStream Pro 700W Thermaltake V6 BlackX Edition 
  hide details  
Reply
post #8 of 8
Thread Starter 
I really appreciate your help, but I just realized we aren't even speaking the same language.

CPLEX is an optimization tool used to solve integer linear programs.

model.add() does not add two values, it adds a constraint to a mathematical model.

I'm finding the syntax to be a little strange, hence why you were confused too.


Anyways, I did figure out what the problem was. See post #5 for the solution.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › CPLEX Programming [Problem Solved]