open ... 模拟退火算法模板

可用于解决TSP等类似问题,可直接使用.

 

<textarea cols="89" rows="15" name="code" class="cpp">#include &lt;vector&gt; #include &lt;functional&gt; class DynamicCoolingPolicy { public: DynamicCoolingPolicy() { accelerateArgument=.5; totalObjValue=0; totalObjValueSqure=0; counts=0; } void accumulate(double v) { totalObjValue+=v; totalObjValueSqure+=(v*v); counts++; } double newTemperature(double currentTemp,bool accelerate) { double stddrv=sqrt(counts*totalObjValueSqure-totalObjValue*totalObjValue/counts)/counts; currentTemp=currentTemp*exp(-accelerateArgument*currentTemp/stddrv); totalObjValue=0; totalObjValueSqure=0; counts=0; if(accelerate) accelerateArgument*=1.01; return currentTemp; } private: size_t counts; double accelerateArgument; double totalObjValueSqure; double totalObjValue; }; class StaticCoolingPolicy { public: StaticCoolingPolicy(); void accumulate(double v) { } double newTemperature(double currentTemp,bool accelerate) { return currentTemp*0.97; } }; class DynamicSelectPolicy { public: DynamicSelectPolicy(size_t neighborTypes) { for (size_t i=0;i&lt;neighborTypes;++i) { probabilityList.push_back((i+1)*1.0/neighborTypes); } contributionCountList.assign(neighborTypes,0); contributionAmountList.assign(neighborTypes,0.1); } size_t select() const { double selProb=g_get_randomUint(1000)/1000.0; for (size_t i=0;i&lt;probabilityList.size();++i) { if(selProb&lt;probabilityList[i]+0.0001) { return i; } } return 0; } void accumulate(size_t ii,double contribution) { ++contributionCountList[ii]; contributionAmountList[ii]+=contribution; } void update() { double totalContributionOfAveg=0; for (size_t i=0;i&lt;probabilityList.size();++i) { if(contributionCountList[i]&gt;0) contributionAmountList[i]=contributionAmountList[i]/contributionCountList[i]; contributionAmountList[i]+=0.015; totalContributionOfAveg+=contributionAmountList[i]; } double acc=0; for (size_t i=0;i&lt;probabilityList.size();++i) { acc+=contributionAmountList[i]/totalContributionOfAveg; probabilityList[i]=acc; contributionCountList[i]=0; contributionAmountList[i]=0; } } private: std::vector&lt;double&gt; probabilityList; std::vector&lt;size_t&gt; contributionCountList; std::vector&lt;double&gt; contributionAmountList; }; class EvenSelectPolicy { public: EvenSelectPolicy(size_t neighborTypes) :neighbor_types(neighborTypes) { }; size_t select() const { return g_get_randomUint(neighbor_types); } void accumulate(size_t ii,double contribution) { } void update() { } private: size_t neighbor_types; }; template&lt;class RunSolution,class _CoolingPolicy,class _SelectPolicy,class MonitorType&gt; void rapidSa(RunSolution* runSln,boost::function&lt;void(MonitorType)&gt; monitor,double terminateTemp) { MonitorType monitordata; double currentTemp=runSln-&gt;initTemperature(); size_t maxInners=runSln-&gt;maxInners(); size_t currentInners=0; double deltaObjValue=0; double bestObjValue=1e10; _CoolingPolicy coolingPolicy; _SelectPolicy selectPolicy(runSln-&gt;neighborTypes()); //probability init runSln-&gt;prepare(); while (true) { double currentObjValue=runSln-&gt;objValue(); //assert(currentObjValue&gt;0); double prevObjValue=currentObjValue; while(true) { size_t iSel=selectPolicy.select(); runSln-&gt;randAdj(iSel); runSln-&gt;updateSln(); currentObjValue=runSln-&gt;objValue(); //assert(currentObjValue&gt;0); coolingPolicy.accumulate(currentObjValue); deltaObjValue=currentObjValue-prevObjValue; if(deltaObjValue&lt;=0) { prevObjValue=currentObjValue; selectPolicy.accumulate(iSel,-deltaObjValue); if(bestObjValue&gt;currentObjValue ) { bestObjValue=currentObjValue; runSln-&gt;saveBest(monitordata); monitor(monitordata); } } else { double c = exp( -(deltaObjValue/currentTemp) ); double p = g_get_randomUint(10000)/10000.0; if(c &gt; p ) { prevObjValue=currentObjValue; selectPolicy.accumulate(iSel,0); } else { runSln-&gt;rollBack(); } } if(currentInners&gt;maxInners) break; else currentInners++; } if(currentTemp&lt;terminateTemp) break; currentTemp=coolingPolicy.newTemperature(currentTemp,false); currentInners = 0; runSln-&gt;prepare(); runSln-&gt;adjPressure(); selectPolicy.update(); } runSln-&gt;loadBestScene(); } </textarea>

 

enjoy it !!!

你可能感兴趣的:(c,算法,Class,Types)