/ Published in: MatLab
Simulated annealing heuristics applied to solve continuous location allocation problem.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function [report,result] = solve_problems() data_files={'bon287';'p654'}; p=[2 4 5 6]; n_runs=5; k=0; for run=1:n_runs k=k+1; end end end function [z_best,x_best,cycles,debug]=solve_location_allocation(custLocs,demands,p,acceptanceRule) acceptanceRule=@probabilisticAcceptance; end tc=0; % termination counter ip=0.3; % updates per cycle bound for temperature r=0.9; % cooling ratio fp=0.02; % updates per cycle bound for termination p_1=0.95; % initial probability k=1; % number of changes in the neighborhood function thresholdParameter=0.1; T=initializeTemperature(p_1,p,k,custLocs,demands); L=2*ns; x=generateArbitrarySolution(n_c,p); % customer/facility assignments z_best=inf; cycle=1; cycles(1,:)=[z_best L T tc]; % parameters stored for debugging per cycle while(~stoppingCondition(tc,cycles)) j=0; facLocs=single_facility_optimization(x,custLocs,demands); x=findAssignments(facLocs,custLocs); z=f(x,demands,facLocs,custLocs); x_=pickANeighbor(x,k,p); facLocs_=single_facility_optimization(x_,custLocs,demands); x_=findAssignments(facLocs_,custLocs); z_=f(x_,demands,facLocs_,custLocs); delta=z-z_; if(delta<0) x=x_; if(z_<z_best) z_best=z_ x_best=x_; facLocs_best=facLocs_; end else if(acceptanceRule(delta,T,z_,z,thresholdParameter)) x=x_; end end end thresholdParameter=thresholdParameter*r; cycle=cycle+1 cycles(cycle,:)=[z_best L T tc]; end function result=thresholdAcceptance(delta,T,z_,z,mu) result=z_<=(1+mu)*z; function result=probabilisticAcceptance(delta,T,z_,z,mu) function result=stoppingCondition(terminationCounter,cycles) % result = terminationCounter >= 5; % alternative stopping condition z_best=cycles(:,1); result=false; return; end change=(z_best(end)-z_best(end-1))/z_best(end); eps_1=0.03; if(change < eps_1) result=true; else result=false; end result=1; else result=0; end T=T/2; else T=r*T; end function result=distance(x,y,degree) degree=2; % default: euclidean distance end % neighborhood structure is based on the customer-facility assignments % pick a neighbor of the current solution x where k is the number of changing assignments, p is the number of facilities function x_=pickANeighbor(x,k,p) x_=x; newFacility=unidrnd(p-1); if (newFacility >= oldFacility) newFacility=newFacility+1; end end function T=initializeTemperature(p_1,p,k,custLocs,demands) n=100; x=generateArbitrarySolution(n_c,p); % customer-facility assignments x_=pickANeighbor(x,k,p); facLocs=single_facility_optimization(x,custLocs,demands); facLocs_=single_facility_optimization(x_,custLocs,demands); end function x=generateArbitrarySolution(n_c,p) x=unidrnd(p,1,n_c); function result=findAssignments(facLocs,custLocs) distances=distancesFromFacilities(facLocs,custLocs); % objective function value of a solution x function result=f(x,demands,facLocs,custLocs) result=0; for cust=1:n_c facility=x(cust); dist=distance(facLocs(facility,:),custLocs(cust,:)); result=result+dist*demands(cust); end function result=distancesFromSingleFacility(facilityLocation,customerLocations,degree) degree=2; % default: euclidean distance end y=customerLocations(cust,1:2); result(cust)=distance(facilityLocation,y,degree); end function distances=distancesFromFacilities(facLocs,custLocs) distances(1:n_c,fac)=distancesFromSingleFacility(facLocs(fac,:),custLocs); end