- Main Page
- Classes
- Files

- File List

# OptFrame/Population.hpp

    00001 #ifndef OPTFRAME_POPULATION_HPP_
    00002 #define OPTFRAME_POPULATION_HPP_
    00003
    00004 #include "Solution.hpp"
    00005 #include <vector>
    00006
    00007 template<class R>
    00008 class Population
    00009 {
    00010 protected:
    00011    typedef Solution<R> chromossome;
    00012    typedef vector<chromossome*> population;
    00013    //typedef vector<const chromossome*> constPopulation;
    00014
    00015    population p;
    00016
    00017 public:
    00018
    00019    Population()
    00020    {
    00021    }
    00022
    00023    Population(chromossome& c)
    00024    {
    00025       p.push_back(c);
    00026    }
    00027
    00028    Population(Population& pop) :
    00029       p(pop.p)
    00030    {
    00031    }
    00032
    00033    Population(const Population& pop) :
    00034       p(pop.p)
    00035    {
    00036    }
    00037
    00038    virtual ~Population()
    00039    {
    00040       unsigned sizePop = p.size();
    00041
    00042       for (unsigned i = 0; i < sizePop; i++)
    00043       {
    00044          delete p.at(i);
    00045       }
    00046    }
    00047
    00048    unsigned size() const
    00049    {
    00050       return p.size();
    00051    }
    00052
    00053    chromossome& at(unsigned c)
    00054    {
    00055       return (*p.at(c));
    00056    }
    00057
    00058    const chromossome& at(unsigned c) const
    00059    {
    00060       return (*p.at(c));
    00061    }
    00062
    00063    void insert(unsigned pos, chromossome& c)
    00064    {
    00065       p.insert(p.begin() + pos, new chromossome(c));
    00066    }
    00067
    00068    void push_back(chromossome& c)
    00069    {
    00070       p.push_back(new chromossome(c));
    00071    }
    00072
    00073    chromossome& remove(unsigned pos)
    00074    {
    00075       chromossome *c = new chromossome(*p.at(pos));
    00076       p.erase(p.begin() + pos);
    00077       return *c;
    00078    }
    00079
    00080    void clear()
    00081    {
    00082       p.clear();
    00083    }
    00084
    00085    bool empty()
    00086    {
    00087       return p.empty();
    00088    }
    00089
    00090    virtual Population<R>& operator=(const Population<R>& p)
    00091    {
    00092       if (&p == this) // auto ref check
    00093          return *this;
    00094
    00095       unsigned sizePop = this->p.size();
    00096
    00097       for (unsigned i = 0; i < sizePop; i++)
    00098       {
    00099          if (this->p.at(i)) // If no NULL pointing.
    00100          {
    00101             delete this->p.at(i);
    00102          }
    00103       }
    00104
    00105       this->p.clear();
    00106
    00107       sizePop = p.size();
    00108
    00109       for (unsigned i = 0; i < sizePop; i++)
    00110       {
    00111          if (&p.at(i)) // If no NULL pointing.
    00112          {
    00113             this->p.push_back(new chromossome(p.at(i)));
    00114          }
    00115          else
    00116          {
    00117             this->p.push_back(NULL);
    00118          }
    00119       }
    00120
    00121       return (*this);
    00122    }
    00123
    00124    virtual Population<R>& clone() const
    00125    {
    00126       return *new Population<R> (*this);
    00127    }
    00128
    00129    virtual void print() const
    00130    {
    00131       cout << "Population's printing:" << endl;
    00132
    00133       for (int i = 0; i < p.size(); i++)
    00134       {
    00135          p.at(i)->print();
    00136       }
    00137    }
    00138
    00139 };
    00140
    00141 #endif /* OPTFRAME_POPULATION_HPP_ */

All Classes Functions