00001
00020 #include <realea/problem/problemcec2005.h>
00021 #include <realea/common/ilocalsearch.h>
00022 #include <realea/common/cross.h>
00023 #include <realea/common/ea.h>
00024 #include <ea/ssga/ssga.h>
00025 #include <localsearch/cmaeshan.h>
00026 #include <realea/common/srandom.h>
00027 #include "malschains.h"
00028 #include <iostream>
00029 #include <cstdio>
00030 #include <cassert>
00031
00032 using namespace realea;
00033 using namespace std;
00034 using std::auto_ptr;
00035
00036 void usage(char *prog) {
00037 printf("This algorithm applies the MA-LS-Chains algorithm to the Workshop Real Continuous Optimization Problems from CEC'2005.\n");
00038 printf("usage: %s fun dim [maxrun=25]\n", prog);
00039 printf("Where\tfun is the number of function (1 to 25)\n\tdim is the dimensionality (10|30|50)");
00040 printf("\n\tmaxrun is the maximum times (default=25)\n");
00041 }
00042
00043 Hybrid *processData(int argc, char **argv, DomainReal *domain, Random &random) {
00044 string alg,arg_ls, arg_effort, arg_maxeval;
00045
00046 if (argc > 1) {
00047 alg = argv[1];
00048 }
00049 if (argc > 2) {
00050 arg_ls = argv[2];
00051 }
00052
00053
00054 SSGA *ssga = new SSGA(&random);
00055 ssga->setCross(new CrossBLX(0.5));
00056 ssga->setMutation(new MutationBGA());
00057 ssga->setSelect(new SelectNAM(3));
00058 ssga->setReplacement(new ReplaceWorst());
00059
00060 IEA *ea = ssga;
00061 CMAESHansen *cmaes = new CMAESHansen("cmaesinit.par");
00062 cmaes->searchNeighborhood(0.5);
00063 ILocalSearch *ls = cmaes;
00064
00065 MALSChains *ma = new MALSChains(ea, ls);
00066 ma->setDebug();
00067 ma->setRestart(new RestartBest());
00068 Hybrid *hybrid = ma;
00069 hybrid->setEffortRatio(0.5);
00070 hybrid->setIntensity(500);
00071 return hybrid;
00072 }
00073
00074 int main(int argc, char **argv) {
00075 unsigned int dim = 100;
00076
00077 try {
00078 Random random(new SRandom(12345679));
00079 int fun;
00080
00081 if (argc < 3) {
00082 usage(argv[0]);
00083 exit(0);
00084 }
00085
00086 fun = atoi(argv[1]);
00087 dim = atoi(argv[2]);
00088
00089 if (dim > 1000) {
00090 dim = 1000;
00091 }
00092
00093 printf("Fun: %d[%d]\n", fun, dim);
00094
00095 ProblemCEC2005 problem(&random, dim);
00096 ProblemPtr prob = problem.get(fun);
00097
00098 tChromosomeReal sol(dim);
00099 tFitness fitness=0;
00100
00101 Hybrid *hybrid = processData(argc-2, argv+2, prob->getDomain(), random);
00102 string algname(argv[1]);
00103 EA alg(hybrid, prob);
00104 unsigned maxRun;
00105
00106 if (argc > 3) {
00107 maxRun = atoi(argv[3]);
00108 }
00109 else {
00110 maxRun = 25;
00111 }
00112
00113 tFitness sum=0;
00114
00115
00116 for (int num = 1; num <= maxRun; num++) {
00117 alg.apply(sol, &fitness);
00118 printf("%Le\n", fitness);
00119 sum += fitness;
00120 }
00121 printf("mean:%Le\n", sum/maxRun);
00122
00123 } catch (ConfigException *e) {
00124 cerr <<"ConfigException : " <<e->what() <<endl;
00125 delete e;
00126 } catch (RunningException *e) {
00127 cerr <<"RunningException : " <<e->what() <<endl;
00128 delete e;
00129 } catch (runtime_error *e) {
00130 cerr <<"runtime_error: " <<e->what() <<endl;
00131 delete e;
00132 } catch (exception &e) {
00133 cerr <<"Exception : " <<e.what() <<endl;
00134 }
00135 catch (string &str) {
00136 cerr <<"StrException : " <<str <<endl;
00137 }
00138 catch (string *str) {
00139 cerr <<"StrException : " <<*str <<endl;
00140 delete str;
00141 }
00142
00143 return 0;
00144 }