The SAS code is as following,
dm log 'clear' ;
dm output 'clear';
PROC IMPORT OUT= WORK.cal10_1
DATAFILE= "D:\nir\cal_dat(1).csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
run;
PROC IMPORT OUT= WORK.cal10_2
DATAFILE= "D:\nir\cal_dat(2).csv"
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
run;
proc iml;
/*Section 2*/
/* "start" finish module, cross_uniform*//*============================================*/
start uniform_cross(child1, child2, parent1, parent2) global(i);
child1 = parent1;
child2 = parent2;
do ii = 1 to ncol(parent1);
r = uniform(1234);
if r<=0.5 then do;
child1[ii] = parent2[ii];
child2[ii] = parent1[ii];
end;
end;
finish;
/*Section 3*/
start knapsack( x ) global(i);
dsNames = {cal10_1 cal10_2};
use (dsNames[i]);
read all var _NUM_ into nir;
close (dsNames[i]);
print nir;
X_new=j (140, 351, 0); /*@@@140: number of observations*/
do n=1 to 351;
if x[, n] =1
then do;
a=n+1;
X_new[,n]=nir[,a ];
end;
end;
data_new=nir[,1]||X_new;
create data_new from data_new;
append from data_new;
close data_new;
free data_new nir X_NEW;
SUBMIT;
ods select none; /*==========Close the output results==========*/
proc pls data=data_new method=pls cv=one NOCENTER NOSCALE;
model COL1= COL2-COL352 / SOLUTION;
ods output ResidualSummary=RS;
ods output CVResults=CVResults;
output out=score XSCORE=XSCORE;
RUN;
ENDSUBMIT;
PRINT X i;
X_sum=X[,+]; /*==========calculate the variable size==========*/
PRINT X_sum;
EDIT RS var _all_;
READ all var _all_ into RS;
CLOSE RS;
EDIT CVResults var _all_;
read all var _all_ into CVResults;
CLOSE CVResults;
EDIT score var _all_;
READ all var _all_ into score;
CLOSE score;
/*calculating PRESS*/
RMPress=RS[, 2];
create RMPress from RMPress; /*P*/
append from RMPress;
close RMPress; /*P*/
/*factor numbers*/ /*x: extract only factor number from CVResults*/
f_no=CVResults[2,1];
create f_no from f_no; /*P*/
append from f_no;
close f_no; /*P*/
/*n*/
n=nrow(score);
/*RMSECV*/
/*factor numbers*/ /*x: extract only factor number from CVResults*/
minRMPress=RS[f_no+1, 2];
RMSECV=SQRT(((n-1)*(minRMPress##2))/n);
RMSECV_T=iii//RMSECV;
return(RMSECV);
free RS CVResults score RMPress f_no minRMPress RMSECV_T RMSECV;
finish;
/*Section 4*/
/*GA module*//*=============================================================*/
dsNames = {cal10_1 cal10_2};
k=1;
do i = 1 to ncol(dsNames);
print i;
id = gasetup(2, 351, );
call gasetobj(id, 0, "knapsack" ); /* minimize objective module */
call gasetcro(id, 0.95, 0,"uniform_cross"); /* user crossover module */
call gasetmut(id,
0.01, /* mutation probabilty */
1);
call gasetsel(id, 2, /* carry 3 over to next generation */
0, /* dual tournament */
2 /* best-player-wins probabilty */
);
call gainit(id, 30, repeat({0,1},1, 351));
niter = 500; /*@@@@ now is 5, change niter to 500*/
summary = j(niter,2);
mattrib summary [c = {"bestValue", "avgValue"}];
call gagetval(value, id);
summary[1,1] = value[1];
summary[1,2] = value[:];
do iii = 1 to niter;
print k iii;
call garegen(id);
call gagetval(value, id);
summary[iii,1] = value[1];
summary[iii,2] = value[:];
end;
call gagetmem(mem, value, id, 1);
print "best member " mem[f = 1.0 l = ""],
"best value " value[l = ""];
iteration = t(1:niter);
print iteration summary;
call gaend(id);
mem_T=mem_T // mem;
value_T=value_T || value;
summary_T=summary_T || summary;
/*-------------------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
print k;
k=k+1;
end;
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------------------------*/
print mem_T value_T summary_T;
create m_1_100 from mem_T; /*@@@change name*/
append from mem_T;
close m_1_100; /*@@@change name*/
create v_1_100 from value_T; /*@@@change name*/
append from value_T;
close v_1_100; /*@@@change name*/
create s_1_100 from summary_T; /*@@@change name*/
append from summary_T;
close s_1_100; /*@@@change name*/
free mem_T;
free value_T;
free summary_T;
PROC EXPORT DATA= WORK.m_1_100
OUTFILE= "D:\nir\Export\gamem.csv"
DBMS=CSV LABEL REPLACE;
PUTNAMES=YES;
PROC EXPORT DATA= WORK.v_1_100
OUTFILE= "D:\nir\Export\gavalue.csv"
DBMS=CSV LABEL REPLACE;
PUTNAMES=YES;
PROC EXPORT DATA= WORK.s_1_100
OUTFILE= "D:\nir\Export\gasummary.csv"
DBMS=CSV LABEL REPLACE;
PUTNAMES=YES;
run;
QUIT;
... View more