Calcite | Level 5

## PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Hi All,

I have just started using the proc optmodel and I need help on calculation of max of a value for all ids in a data set with constraint values of a & b aggregated over all ids.

For example -

data new;
input id\$ a b ;
datalines;
1 5 4
2 6 5
3 5 3
4 6 4
;

Now I want to solve for x that maximizes Z = (a + bx) for all ids in data i.e. for id 1, 2 and etc.

My Code was like below -

proc optmodel;
set <str> DATA;
var X{DATA} >= 0;
num a{DATA}; num b{DATA};
max Z = (a[i] + b[i]*X[i]);

con sum{i in DATA} (a[i]+b[i])=5;
read data new into DATA = [id] a b;
solve with LP /logfreq= 1 ;
print X;
quit;

But I am getting the below error -

ERROR 525-782: The symbol 'i' is unknown.

ERROR 621-782: Subscript 1 must be a string, found a number.

I would really appreciate some help on this.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Here's one way:

``````proc optmodel;
set <str> DATA;
var X >= 0 <= 100;
num a{DATA};
num b{DATA};
str id;
max Z = a[id] + b[id]*X;
read data new into DATA = [id] a b;

num Xsol{DATA};
do id = DATA;
put id=;
solve;
Xsol[id] = X.sol;
end;
print a b Xsol;
quit;``````

Alternatively, you could replace the DO loop with a COFOR loop to solve these independent problems concurrently:

``````   cofor {i in DATA} do;
id = i;
put id=;
solve;
Xsol[id] = X.sol;
end;
``````
7 REPLIES 7
Super User

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

You are very close, but your constraint looks like not right.

``````data new;
input id\$ a b ;
datalines;
1 5 4
2 6 5
3 5 3
4 6 4
;

proc optmodel;
set <str> DATA;
var X{DATA} >= 0;
num a{DATA};
num b{DATA};
max Z =sum{i in DATA} (a[i] + b[i]*X[i]);

read data new into DATA = [id] a b;
solve with LP  ;
print X;
quit;``````
SAS Super FREQ

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Do you want to solve one problem that involves all ids, or do you want to a separate problem for each id?

Also, without additional constraints, the problem is unbounded in the sense that you can make the objective value as large as you want just by making X[i] large.

Calcite | Level 5

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Hi Rob,
I am looking for separate optimisation for each ids. For example -
For id 1, max of z = 5 + 4X
and for id 2, max of z = 6 + 5X and so on.
And the contraints would be X < 100.
Thanks
SAS Super FREQ

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Do you have any other constraints?  Otherwise, the problem still has a trivial optimal solution: X[i] = 100 for each i.

Calcite | Level 5

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Hi Rob,
I will apply other constraint this is just a test constraint. For now I can go with z < 100 and that will suffice.

Thanks
SAS Super FREQ

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Here's one way:

``````proc optmodel;
set <str> DATA;
var X >= 0 <= 100;
num a{DATA};
num b{DATA};
str id;
max Z = a[id] + b[id]*X;
read data new into DATA = [id] a b;

num Xsol{DATA};
do id = DATA;
put id=;
solve;
Xsol[id] = X.sol;
end;
print a b Xsol;
quit;``````

Alternatively, you could replace the DO loop with a COFOR loop to solve these independent problems concurrently:

``````   cofor {i in DATA} do;
id = i;
put id=;
solve;
Xsol[id] = X.sol;
end;
``````
Calcite | Level 5

## Re: PROC OPTMODEL FOR ROW WISE OPTIMIZATION

Thank you so much Rob, this is working perfectly

Discussion stats
• 7 replies
• 1110 views
• 0 likes
• 3 in conversation