Operations Research topics: SAS/OR,
SAS Optimization, and SAS Simulation Studio

setting up a constraint

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

setting up a constraint

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!


Accepted Solutions
Solution
a week ago
SAS Employee
Posts: 420

Re: setting up a constraint

Glad to help.  Yes, please use IsTwo[i].  I edited my reply again to correct that.

View solution in original post


All Replies
SAS Employee
Posts: 420

Re: setting up a constraint

[ Edited ]

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 ;
Occasional Contributor
Posts: 6

Re: setting up a constraint

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! 

SAS Employee
Posts: 420

Re: setting up a constraint

[ Edited ]

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.

Occasional Contributor
Posts: 6

Re: setting up a constraint

That is brilliant! Thank you!

Should I change IsTwo[i,j] to IsTwo[i]?
Solution
a week ago
SAS Employee
Posts: 420

Re: setting up a constraint

Glad to help.  Yes, please use IsTwo[i].  I edited my reply again to correct that.

Occasional Contributor
Posts: 6

Re: setting up a constraint

Hi Mr. Pratt,
Sorry for the bother again. I don't think this solution works as planned. For example, if I put 2 instead of 0.1, I obtain about the same number of persons assigned 2 tasks as those 1 task. Can you please take another look at this?
Thank you!
SAS Employee
Posts: 420

Re: setting up a constraint

Please post the full code.
Occasional Contributor
Posts: 6

Re: setting up a constraint

Here you go. The solution has about 24% of the persons assigned two tasks and 76% one. Is it a matter of precision? Thank you so much!

proc optmodel forcepresolve=1 presolver=3 fd=central misscheck;

performance details;
profile on percent=0.05;

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; /*targeting pool size information????*/

var assign {persons, 1..5} binary;
var isTwo {persons} binary;

set tasks = 1..5;
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 1..5}: sum {i in persons} assign[i,j] = 100;

con no_more_2 {i in persons}: sum {j in 1..5} assign[i,j] <= 1 + isTwo[i];

con percent_1: sum {i in persons} isTwo[i] <= 0.22*sum {i in persons} (sum {j in 1..5} assign[i,j] - isTwo[i]);
con percent_1: sum {i in persons} isTwo[i] >= 0.18*sum {i in persons} (sum {j in 1..5} assign[i,j] - isTwo[i]);

con con_d1 {j in 1..5}: sum {i in persons} assign[i,j]* cond1[i] >= .10*100;
con con_d2 {j in 1..5}: sum {i in persons} assign[i,j]* cond1[i] <= .15*100;
con con_d3 {j in 1..5}: sum {i in persons} assign[i,j]* cond2[i] >= .10*100;

expand;
solve with milp obj objabs / absobjgap=0.001 primalin ;

create data output from [persons task] = {{i in persons}, {j in 1..5}} in=assign;

quit;

SAS Employee
Posts: 420

Re: setting up a constraint

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?

Occasional Contributor
Posts: 6

Re: setting up a constraint

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!

Attachment
SAS Employee
Posts: 420

Re: setting up a constraint

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 139 views
  • 0 likes
  • 2 in conversation