proc optmodel;
/*declare */
set rows = 1..10;
set cols = 1..6;
var X{rows,cols} >=0 <=50;
max z=sum(x[6,1],x[6,2],x[6,3]);
/*constrains */
con varcon1{i in 1..6}: 0<=X[1,i]<=10;
con varcon2{i in 1..6}: 0<=X[1,i]<=25;
con varcon3{i in 1..6}: 0<=X[1,i]<=15;
con varcon4{i in 1..6}: 0<=X[1,i]<=23;
con varcon5{i in 1..6}: 0<=X[1,i]<=10;
con varcon6{i in 1..6}: 0<=X[1,i]<=20;
con varcon7{i in 1..6}: 0<=X[1,i]<=20;
con varcon8{i in 1..6}: 0<=X[1,i]<=20;
con varcon9{i in 1..6}: 0<=X[1,i]<=5;
con varcon10{i in 1..6}: 0<=X[1,i]<=2;
/*constrains */
con colcon1{i in 1..10}: 0<=x[i,1]<=50;
con colcon2{i in 1..10}: 0<=x[i,2]<=35;
con colcon3{i in 1..10}: 0<=x[i,3]<=15;
con colcon4{i in 1..10}: 0<=x[i,4]<=8;
con colcon5{i in 1..10}: 0<=x[i,5]<=12;
con colcon6{i in 1..10}: 0<=x[i,6]<=30;
/*constrains */
con equal1: sum{i in 1..6}(x[1,i]) = 10;
con equal2: sum{i in 1..6}(x[2,i]) = 25;
con equal3: sum{i in 1..6}(x[3,i]) = 15;
con equal4: sum{i in 1..6}(x[4,i]) = 10;
con equal5: sum{i in 1..6}(x[5,i]) = 20;
con equal6: sum{i in 1..6}(x[6,i]) = 20;
con equal7: sum{i in 1..6}(x[7,i]) = 20;
con equal8: sum{i in 1..6}(x[8,i]) = 5;
con equal9: sum{i in 1..6}(x[9,i]) = 2;
con equal10:sum{i in 1..6}(x[10,i]) = 23;
SOLVE;
print x;
QUIT;
Please run this code,
the question is
How do I keep array X two decimal places
The SUM() function triggers the NLP solver to be used. If you change the objective declaration as follows, the LP solver will instead be used, and the (totally unimodular) structure of your problem naturally yields an integer solution:
/* max z=sum(x[6,1],x[6,2],x[6,3]);*/
max z = x[6,1] + x[6,2] + x[6,3];
X | ||||||
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
1 | 2 | 2 | 2 | 2 | 2 | 0 |
2 | 25 | 0 | 0 | 0 | 0 | 0 |
3 | 15 | 0 | 0 | 0 | 0 | 0 |
4 | 10 | 0 | 0 | 0 | 0 | 0 |
5 | 20 | 0 | 0 | 0 | 0 | 0 |
6 | 20 | 0 | 0 | 0 | 0 | 0 |
7 | 20 | 0 | 0 | 0 | 0 | 0 |
8 | 5 | 0 | 0 | 0 | 0 | 0 |
9 | 2 | 0 | 0 | 0 | 0 | 0 |
10 | 23 | 0 | 0 | 0 | 0 | 0 |
Better post it at OR forum. And calling @RobPratt
The SUM() function triggers the NLP solver to be used. If you change the objective declaration as follows, the LP solver will instead be used, and the (totally unimodular) structure of your problem naturally yields an integer solution:
/* max z=sum(x[6,1],x[6,2],x[6,3]);*/
max z = x[6,1] + x[6,2] + x[6,3];
X | ||||||
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
1 | 2 | 2 | 2 | 2 | 2 | 0 |
2 | 25 | 0 | 0 | 0 | 0 | 0 |
3 | 15 | 0 | 0 | 0 | 0 | 0 |
4 | 10 | 0 | 0 | 0 | 0 | 0 |
5 | 20 | 0 | 0 | 0 | 0 | 0 |
6 | 20 | 0 | 0 | 0 | 0 | 0 |
7 | 20 | 0 | 0 | 0 | 0 | 0 |
8 | 5 | 0 | 0 | 0 | 0 | 0 |
9 | 2 | 0 | 0 | 0 | 0 | 0 |
10 | 23 | 0 | 0 | 0 | 0 | 0 |
Also notice that your varcon10 constraint makes the other nine varcon* constraints redundant. Are you sure that this model correctly captures the problem you want to solve?
Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.
Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.