00001
00020 #include "ssga.h"
00021 #include <cassert>
00022
00023 using namespace realea;
00024
00025 SSGA::SSGA(Random *random) : ICrossEAlgorithm(random), m_select(NULL), m_replace(NULL), m_mutation(NULL),
00026 m_imutation(NULL) {
00027 }
00028
00029 void SSGA::setSelect(ISelect *sel) {
00030 m_select = sel;
00031 m_select->setRandom(m_random);
00032 appendSignal(m_select);
00033 }
00034
00035 void SSGA::setReplacement(IReplace *replace) {
00036 m_replace = replace;
00037 appendSignal(m_replace);
00038 }
00039
00040 void SSGA::setMutation(IMutation *mutation) {
00041 m_imutation = mutation;
00042
00043 if (m_problem) {
00044 mutation->setDomain(m_problem->getDomain());
00045 m_mutation = new Mutation(mutation);
00046 m_mutation->setRandom(m_random);
00047 m_mutation->setDomain(m_problem->getDomain());
00048 }
00049
00050 }
00051
00052 void SSGA::setProblem(Problem *problem) {
00053 ICrossEAlgorithm::setProblem(problem);
00054 m_select->setDomain(m_problem->getDomain());
00055
00056 if (!m_mutation && m_imutation) {
00057 m_imutation->setDomain(m_problem->getDomain());
00058 m_mutation = new Mutation(m_imutation);
00059 m_mutation->setRandom(m_random);
00060 m_mutation->setDomain(m_problem->getDomain());
00061 }
00062 }
00063
00064
00065
00066 SSGA::~SSGA(void) {
00067 if (m_select)
00068 delete m_select;
00069
00070 if (m_replace)
00071 delete m_replace;
00072
00073 if (m_mutation)
00074 delete m_mutation;
00075 }
00076
00077 unsigned SSGA::init(void) {
00078
00079 m_pop->reset(m_problem->getDomain());
00080
00081 reset();
00082
00083
00084 if (m_select == NULL) {
00085 throw new ConfigException("select");
00086 }
00087 if (m_replace == NULL) {
00088 throw new ConfigException("replace");
00089 }
00090 if (m_cross == NULL) {
00091 throw new ConfigException("cross");
00092 }
00093
00094 m_pop->eval(m_init_eval);
00095 return m_running->numEval();
00096 }
00097
00098 unsigned SSGA::realApply(tChromosomeReal &sol, tFitness &fitness) {
00099 unsigned pos_mom, pos_dad;
00100 tIndividualRealPtr mom, dad;
00101 tChromosomeReal crom(m_problem->getDimension());
00102 tFitness best_fit;
00103 unsigned better;
00104
00105 unsigned initMax = m_running->numEval();
00106
00107
00108 while (!m_running->isFinish()) {
00109 if (m_stat)
00110 m_stat->newGeneration();
00111
00112
00113 m_select->select(m_pop, &pos_mom, &pos_dad);
00114 mom = m_pop->getInd(pos_mom);
00115 dad = m_pop->getInd(pos_dad);
00116
00117 assert(mom != dad);
00118
00119
00120 cross(pos_mom, pos_dad, crom);
00121
00122 if (m_mutation) {
00123 m_mutation->apply(crom);
00124
00125 if (m_stat)
00126 m_stat->newEvent("Mutation");
00127 }
00128
00129
00130 tIndividualRealPtr newind = m_pop->getInstance(crom);
00131
00132 m_new_eval->eval(newind);
00133
00134
00135 unsigned candreplace = m_replace->getCandidate(m_pop, newind);
00136
00137 if (m_replace->mustBeReplace(m_pop->getInd(candreplace), newind))
00138 m_pop->replace(candreplace, newind);
00139 else
00140 delete newind;
00141
00142 better = m_pop->getBest();
00143 best_fit = m_pop->getInd(better)->perf();
00144
00145 if (m_stat)
00146 m_stat->endGeneration(best_fit);
00147
00148 }
00149
00150
00151 unsigned pos = m_pop->getBest();
00152 tIndividualRealPtr best= m_pop->getInd(pos);
00153
00154 tChromosomeReal bestsol= best->sol();
00155 copy(bestsol.begin(), bestsol.end(), sol.begin());
00156 fitness = best->perf();
00157 unsigned neval = m_running->numEval()-initMax;
00158 m_running->reset();
00159 return neval;
00160 }
00161
00162 void SSGA::cross(unsigned pos_mom, unsigned pos_dad, tChromosomeReal &crom) {
00163 tIndividualReal *mom, *dad;
00164
00165 mom = m_pop->getInd(pos_mom);
00166 dad = m_pop->getInd(pos_dad);
00167
00168 (*m_cross)(mom, dad, crom);
00169 }
00170
00171