dm log 'clear' ; dm output 'clear'; *option nonotes; /*==========Close the LOG results==========*/ PROC IMPORT OUT= WORK.cal10_1 DATAFILE= "D:\cal10_1.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; run; PROC IMPORT OUT= WORK.cal10_2 DATAFILE= "D:\cal10_2.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; run; /*Style 1*/ /*-------------------------------------------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------------------------------*/ proc iml symsize=2147483648 worksize=2147483648; /*---------------------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------*/ /*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, nir); X_new=j (10, 351, 0); /*@@@140 40 : 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; free nir X_NEW; free; /**//**//**//**//**//**//**//**/ SUBMIT; ods select none; /*==========Close the output results==========*/ proc pls data=data_new method=pls cv=one NOCENTER NOSCALE plots=none; model COL1= COL2-COL352; ods output CVResults=CVResults; RUN; ENDSUBMIT; PRINT X i; X_sum=X[,+]; /*==========calculate the variable size==========*/ PRINT X_sum; EDIT CVResults var _all_; read all var _all_ into CVResults; CLOSE CVResults; /*n*/ n=nrow(data_new); /*RMSECV*/ /*factor numbers*/ /*x: extract only factor number from CVResults*/ minRMPress=CVResults[1, 1]; RMSECV=SQRT(((n-1)*(minRMPress##2))/n); RMSECV_T=iii//RMSECV; return(RMSECV); free CVResults f_no minRMPress RMSECV_T RMSECV; FREE nir; FREE; /**//**//**//**//**//**//**//**/ members = { "data_new" "CVResults"}; /**//**//**//**//**//**//**//**/ CALL DELETE (members); /**//**//**//**//**//**//**//**/ finish; /*Section 4*/ /*GA module*//*=============================================================*/ dsNames = {cal10_1 cal10_2}; k=1; do i = 1 to ncol(dsNames); print i; /*dec new*/ use (dsNames[i]); read all var _NUM_ into nir; close (dsNames[i]); print nir; 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 =100; /*@@@@ 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[:]; IF k=1 THEN DO; L_K=k//iii; /**//**//**//**//**//**//**//**/ create L_K from L_K; append from L_K; close L_K; END; IF k=2 THEN DO; L_K2=k//iii; /**//**//**//**//**//**//**//**/ create L_K2 from L_K2; append from L_K2; close L_K2; END; 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; 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*/ end; /*---------------------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------------------------------*/ QUIT; /*------*/