Hi, I'm learning proc optmodel and I have a question. Suppose we have this problem:
proc fcmp outlib=work.funcs.f;
function f1(Q, H);
return (
-21.570992+
Q*1.972536+
H*0.719408+
Q**2*-0.03148+
Q*H*-0.057026+
H**2*-0.008104+
Q**3*0.00117+
Q**2*H*0.000098997+
Q*H**2*0.000642+
H**3*0.000030146+
Q**4*-0.000015012+
Q**3*H*-0.000001948+
Q**2*H**2*-0.000000285+
Q*H**3*-0.0000024+
H**4*0
);
endsub;
function f2(Q, H);
return (
-11.381931+
Q*-0.176563+
H*0.49236+
Q**2*0.006618+
Q*H*0.004691+
H**2*-0.007303+
Q**3*0.001559+
Q**2*H*-0.001057+
Q*H**2*0.00014+
H**3*0.000035108+
Q**4*-0.000015803+
Q**3*H*-0.000005786+
Q**2*H**2*0.000007708+
Q*H**3*-0.000001467+
H**4*0
);
endsub;
run;
options cmplib=work.funcs;
options nonotes;
proc optmodel;
number P = 30.440000534;
number H = 83.698883057;
var Q1 >= 0;
var Q2 >= 0;
impvar P1 = Q1 * H * f1(Q1, H) * 9.81 / 1000;
impvar P2 = Q2 * H * f2(Q2, H) * 9.81 / 1000;
minimize Q = Q1 + Q2;
constraint P = P1 + P2;
solve with nlp / maxiter=10000000 nthreads=32;
print Q Q1 Q2 (Q1/Q*100) (Q2/Q*100) P P1 P2 ;
run;
The input variables are P and H (given as constants in the example). The minimization objective function is Q.
If I run this code, I get (in the result window) the minimum Q for that specific values of P and H.
I would like to run the optimization for each P in range (0,50) by step 0.1 and for each H in range (50,100) by step 0.1 and output the results in one dataset like:
P | H | Q
0 50.0 <the optimal Q with these parameters>
0 50.1 <the optimal Q with these parameters>
...
0.1 50.0 <the optimal Q with these parameters>
0.1 50.1 <the optimal Q with these parameters>
...
How would I do that? Should I prepare a dataset with all the combinations of P and H, and then tell somehow to proc optmodel to use this dataset as input?
Thanks a lot
Regards
Yes, an input data set with P and H values is one approach, as demonstrated in this SAS Usage Note. Alternatively, you can use nested DO loops over P and H as follows:
proc optmodel printlevel=0;
number P;
number H;
var Q1 >= 0;
var Q2 >= 0;
impvar P1 = Q1 * H * f1(Q1, H) * 9.81 / 1000;
impvar P2 = Q2 * H * f2(Q2, H) * 9.81 / 1000;
minimize Q = Q1 + Q2;
constraint P = P1 + P2;
set PSET = 0..50 by 0.1;
set HSET = 50..100 by 0.1;
num Qopt {PSET, HSET};
do P = PSET;
do H = HSET;
put P= H=;
solve;
Qopt[P,H] = Q.sol;
end;
end;
create data Qdata from [P H] Q=Qopt;
quit;
Yes, an input data set with P and H values is one approach, as demonstrated in this SAS Usage Note. Alternatively, you can use nested DO loops over P and H as follows:
proc optmodel printlevel=0;
number P;
number H;
var Q1 >= 0;
var Q2 >= 0;
impvar P1 = Q1 * H * f1(Q1, H) * 9.81 / 1000;
impvar P2 = Q2 * H * f2(Q2, H) * 9.81 / 1000;
minimize Q = Q1 + Q2;
constraint P = P1 + P2;
set PSET = 0..50 by 0.1;
set HSET = 50..100 by 0.1;
num Qopt {PSET, HSET};
do P = PSET;
do H = HSET;
put P= H=;
solve;
Qopt[P,H] = Q.sol;
end;
end;
create data Qdata from [P H] Q=Qopt;
quit;
Thanks a lot!
One last question: how can I output in the dataset Qdata information about the solution, like the solution status, the optimality error etc, for each combination of (P,H)?
The solver is nlp, with maxiter=100; I need the status because for some (P,H) the solution can be found in a few iteration, but for other (P,H) the solution is unfeasable (so the Qsol is meaningless). Hence, in the final dataset Qdata I must know about how good is Qsol, to do other things for the values where the solution was unfeasable.
Thanks again
Regards
Before the DO loops, declare new parameters:
str solstatus {PSET, HSET};
num opterror {PSET, HSET};
Inside the DO loops, populate these values:
solstatus[P,H] = _solution_status_;
opterror[P,H] = _OROPTMODEL_NUM_['OPTIMALITY_ERROR'];
After the DO loops, include the new parameters in the CREATE DATA statement:
create data Qdata from [P H] Q=Qopt solstatus opterror;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.