Hello!
The problem is to assign a number of persons to 5 tasks while maximizing an objective using OPTMODEL. 10% or less of the persons can be assigned 2 tasks while the rest only 1. If the constraint of "no more than 2" can be set up this way,
con no_more_2 {i in persons}: sum {j in tasks} assign[i,j] <= 2;
how should I specify the constraint of "only <= 10% can be assigned 2 tasks?" I tried the following
con percent: sum {i in persons} (if sum {j in tasks}assign[i,j] > 0 then 1 else 0) <= 10%*total ;
It did not work and gave this message:
ERROR: The specified optimization technique does not allow nonlinear constraints.
Thank you for your help!
Glad to help. Yes, please use IsTwo[i]. I edited my reply again to correct that.
You can do it by introducing binary variables as follows:
var IsTwo {persons} binary;
con no_more_2 {i in persons}: sum {j in tasks} assign[i,j] <= 1 + IsTwo[i];
con percent: sum {i in persons} IsTwo[i] <= 0.1*total ;
Hi Mr. Pratt,
Thanks so much for the help! I replaced
con no_more_2 {i in persons}: sum {j in tasks} assign[i,j] <= 1 + IsTwo[i,j];
con percent: sum {i in persons} IsTwo[i] <= 10%*total ;
with
con no_more_2 {i in persons}: sum {j in tasks} assign[i,j] <= 1 + IsTwo[i];
con percent {j in tasks}: sum {i in persons} IsTwo[i] <= 10%*total ;
It seems to have worked. Hope it is correct. An extended question. If I need to replace "total" with the total of persons who are assigned a task, guess it could be specified like this
con percent: sum {i in persons} IsTwo[i] <= 10%*sum {i in persons} assign[i,j] ;
But this dynamic way of defining the constraint does not wok. What am I missing? Thank you!
Thanks for the correction of IsTwo[i,j] to IsTwo[i]. I edited my reply.
For the percent constraint, there is no reason to have a separate constraint for each j. If you use the EXPAND statement, you will see that the same constraint appears several times, once for each j.
Your revised constraint declaration has two errors:
1. Use 0.1 instead of 10%.
2. The j index is unknown.
Here's one way to model what you want:
con percent:
sum {i in persons} IsTwo[i] <= 0.1*sum {i in persons} (sum {j in tasks} assign[i,j] - IsTwo[i]);
The idea is that the expression in parentheses is 1 if person i is assigned to at least once task.
Glad to help. Yes, please use IsTwo[i]. I edited my reply again to correct that.
I see that you have declared the percent_1 constraint twice, so that should have generated an error.
Can you please also attach the test data set?
Fixed and now the percentage of persons assigned two task is about 17 to 18. Are the two percent constraints set up correctly? Data file attached. Many thanks!
The formulation I suggested enforces only the implication "if person i is assigned two tasks then IsTwo[i] = 1," which is fine if you have only the percent_1 constraint. But the percent_2 constraint also requires the converse implication "if IsTwo[i] = 1 then person i is assigned two tasks." Otherwise, the solver can "cheat" by setting IsTwo[i] = 1 to help satisfy percent_2 even if person i is assigned fewer than 2 tasks. The code below implements a more robust formulation that enforces both implications. Notice that I used tasks instead of 1..5 throughout, and I also combined d1 and d2 into a range constraint.
proc optmodel;
set persons;
number id{persons}, x{persons}, cond1{persons}, cond2{persons};
read data test into
persons=[id] x cond1 cond2;
number sumX=sum{i in persons} x[i];
number target=0.1*sumX;
set tasks = 1..5;
var assign {persons, tasks} binary;
var surplus {tasks} >= 0;
var slack {tasks} >= 0;
min objabs = sum {j in tasks} (surplus[j] + slack[j]);
con obj_alt {j in tasks}: sum {i in persons} assign[i,j]* x[i] - surplus[j] + slack[j] = target;
con taskId {j in tasks}: sum {i in persons} assign[i,j] = 100;
set counts = 0..2;
var isCount {persons, counts} binary;
con onecount {i in persons}: sum {c in counts} isCount[i,c] = 1;
con isCountDef {i in persons}: sum {j in tasks} assign[i,j] = sum {c in counts} c*isCount[i,c];
con percent_1: sum {i in persons} isCount[i,2] <= 0.22*sum {i in persons} (1 - isCount[i,0]);
con percent_2: sum {i in persons} isCount[i,2] >= 0.18*sum {i in persons} (1 - isCount[i,0]);
con con_d1_d2 {j in tasks}: .10*100 <= sum {i in persons} assign[i,j]* cond1[i] <= .15*100;
con con_d3 {j in tasks}: sum {i in persons} assign[i,j]* cond2[i] >= .10*100;
solve;
put ((sum {i in persons} isCount[i,2]) / (sum {i in persons} (1 - isCount[i,0])));
num numTasks {i in persons} = round(sum {j in tasks} assign[i,j].sol);
put (card({i in persons: numTasks[i] = 2}) / card({i in persons: numTasks[i] > 0}));
create data output from [persons task] in=assign;
quit;
Here are a few suggestions:
for {i in persons} do;
onecount[i].block = i;
isCountDef[i].block = i;
end;
solve with milp / decomp;
The PUT and NUM statements after the SOLVE in the previous code display the ratio of people assigned two tasks to people assigned at least one task. The two PUT statements should show the same value in the log, and that value should be between 0.18 and 0.22, as enforced by the optimization model.
The CREATE DATA statement saves the values of the assign variable in the resulting solution to a SAS data set.
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.