Car Painting Problem (clp5)
/***************************************************************/
/* */
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: clp5 */
/* TITLE: Car Painting Problem (clp5) */
/* PRODUCT: OR */
/* SYSTEM: ALL */
/* KEYS: OR */
/* PROCS: CLP, GANTT */
/* DATA: */
/* */
/* SUPPORT: UPDATE: */
/* REF: */
/* MISC: Example 5 from the CLP Procedure chapter of the */
/* Constraint Programming book. */
/* */
/***************************************************************/
%macro car_painting(purgings);
proc clp out=car_ds findall;
%do i = 1 %to 10;
var S&i = [1, 10]; /* which car is in slot &i.*/
var C&i = [1, 4]; /* which color the car in slot &i is.*/
/* Red=1; Blue=2; Green=3; Yellow=4 */
element (S&i, (1, 2, 3, 4, 1, 2, 3, 4, 1, 2), C&i);
%end;
/* A car can be painted only once. */
alldiff (S1-S10);
/* A car must be painted within 3 positions of its assembly order. */
%do i = 1 %to 10;
lincon S&i-&i>=-3;
lincon S&i-&i<=3;
%end;
%do i = 1 %to 9;
var P&i = [0, 1]; /* Whether there is a purge after slot &i*/
reify P&i: (C&i <> C%eval(&i+1));
%end;
/* Calculate the number of purgings. */
lincon 0
%do i = 1 %to 9;
+ P&i
%end;
<=&purgings ;
run;
%mend;
%car_painting(5)
/* Produce Gantt Chart */
%macro car_painting_gantt;
/* map colors to patterns */
%let color1 = red;
%let color5 = &color1;
%let color9 = &color5;
%let color2 = blue;
%let color6 = &color2;
%let color10 = &color6;
%let color3 = green;
%let color7 = &color3;
%let color4 = yellow;
%let color8 = &color4;
/* set patterns */
%do i=1 %to 10;
pattern&i. v=s color=&&color&i. repeat=1;
%end;
data card_ds(keep=S1-S10 dist);
set car_ds;
dist = 0;
%do i = 1 %to 10;
dist = dist + abs(S&i-&i);
%end;
run;
proc sort data=card_ds;
by dist;
run;
proc format;
value stage
0.5 = '1'
9.5 = '10'
10.5 = ' '
;
run;
data schedule;
format _label $13.;
format s_start stage.;
label solution='Solution';
label dist='Distance';
set card_ds;
keep _pattern _label segmt_no solution s_start s_finish dist;
retain solution 1;
%do i = 1 %to 10;
s_start=%eval(&i-1)+0.5;
s_finish=&i+0.5;
segmt_no = &i;
nextpat = 0; /* ensure no match */
%do j = 1 %to 10;
if S&i. eq &j then do;
_label = "Car &j: &&color&j";
_pattern = &j;
end;
%if %eval(&i.+1) lt 10 %then %do;
if S%eval(&i+1) eq &j then do;
nextpat = &j;
end;
%end;
%end;
if mod(nextpat - _pattern,4) ne 0 then do;
s_finish = s_finish - 0.05;
end;
output;
%end;
solution = solution + 1;
run;
data labels;
_y=-1;
_lvar="_label";
_xvar="s_start";
_hlabel=0.67;
_yoffset = -0.2;
_xoffset = 0;
run;
proc format;
value stage
0.5 = '1'
9.5 = '10'
10.5 = ' '
;
run;
goptions htext=1.0;
title1 j=c h=3.3pct 'Car Painting Problem';
title2 j=c h=2.3pct 'Solutions with Five Purgings';
proc gantt data=schedule labdata=labels;
chart /
hpages=1 pcompress
scale=5
njobs=15
chartwidth=84
nojobnum
nolegend
mindate=0.5
useformat
labsplit='/'
;
id solution dist;
run;
%mend car_painting_gantt;
%car_painting_gantt;