-1

I want to be able to compare the "overall" values of a person with another person. I'm unsure if I'm storing them correctly and I don't know how to compare them correctly. I don't know how to access the "overall" values of any single person, which is what I think is bugging me the most.

Header file

#ifndef Population_h
#define Population_h


class population
{
    friend class person;
private:
    int size;
    int generation;


public:
    void setsize(int x);    
    void tournament_selection(population x, int z);



};



class person
{
    friend class population;
private:
    float fit1;
    float fit2;
    float overall;


public:

    void generatefit();
    void setfit();
    void langerman();
    void printinfo();

};






#endif

Population.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <random>
#include <string>
#include <CMATH>
#include <vector>
#include "Population.h"
using namespace std;

void person ::generatefit()
{
    float randomnumb1;
    float randomnumb2;
    //float((rand() % 10)*0.1);

    randomnumb1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
    randomnumb2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

    fit1 = randomnumb1;
    fit2 = randomnumb2;


}

void person::setfit()
{   
    float x = fit1;
    float y = fit2;
}

void person::langerman()
{
    overall = 3 * pow(fit1, 2) + 2 * fit2 + 0.0252;
    for (overall; overall > 1; overall--);
}


void person::printinfo()
{
    cout << overall << " " << endl;
}



void population::setsize(int x)
{
    size = x;
}


void population::tournament_selection(population x, int total)
{
    float best = 0;
    for (int i = 0; i <= total; i++)
    {

    }


}

main.cpp

#include "Population.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <random>
#include <vector>
#include <stdlib.h>
using namespace std;

int main()
{
    cout << "Program is starting " << endl;

    srand(static_cast <unsigned> (time(0)));
    population pop;
    vector<person> popvector;
    vector<person> survivor;
    person *p1;

    int popsize = 500;
    pop.setsize(popsize);

    for (int i = 0; i <= popsize; i++)
    {
        p1 = new person;

        p1 ->generatefit();
        p1->setfit();
        p1->langerman();
        popvector.push_back(*p1);
        delete p1;
    }
    cout << "The fit values of the person are listed here: " << endl;
    vector<person> ::iterator it; //iterator to print everything in the vector 
    for (it = popvector.begin(); it != popvector.end(); ++it)
    {
        it->printinfo();
    }

    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); // generate a seed for the shuffle process of the vector.

    cout << "Beggining selection process" << endl;

    shuffle(popvector.begin(), popvector.end(), std::default_random_engine(seed));

    //want to pick consecutive parents 

    int j = 0;



}

I want to be able to compare people, store the "winners" into the "survivors" vector and then proceed to use the "survivor" vector to create a new population with the use of crossover and mutation for X generations.

Ploxzx
  • 53
  • 3
  • Just a suggestion, but retool this to use the C++ [**``**](http://en.cppreference.com/w/cpp/numeric/random) offerings. They're really the cat's whiskers. – WhozCraig Apr 11 '16 at 03:23
  • You can't delete p1 right after you put it in the vector. Now the vector doesn't have a valid person anymore. – Millie Smith Apr 11 '16 at 03:23
  • @MillieSmith I removed the delete, that was a precautionary step i had put there for memory leak. It was still able to print out the overall of the individual though. – Ploxzx Apr 11 '16 at 03:28
  • Ah oh I was wrong @Ploxzx. I missed that you dereferenced it when you put it into the vector. Might as well allocate it on the stack then though. – Millie Smith Apr 11 '16 at 03:29
  • @MillieSmith I'm not quite familiar with allocating memory on the stack. When i generate a new person and then compute the functions and store it into a vector, how would I be able to access the "overall" value of any specific person in the popvector? – Ploxzx Apr 11 '16 at 03:41
  • If you just want to print it, then `popvector[i].printinfo()`. You allocated `population pop` on the stack. anything that isn't created with the `new` (or `malloc`) is created on the stack. – Millie Smith Apr 11 '16 at 03:50

1 Answers1

1

You could use operator overloading to set a customized "Rule" of comparing the fitness level of two human beings. std::string is a perfect example": equal operations can be carried out directly by if(str1 == str2) instead of if(!strcmp(str1, str2)), demonstrating the virtue of operator overloading technique.

The following code should suit your needs:

class person {
    friend class population;
private:
    float fit1;
    float fit2;
    float overall;

public:
    void generatefit();
    void setfit();
    void langerman();
    void printinfo();

    //Overloading '<' operator
    bool operator(const person&);
};

//The following function defines
//rule of fitness in the jungle
bool person::operator < (const person& p2){
    //This is just an example, you can define your own rules
    return overall < p2.overall;
}

Once the comparing rule has been established, you can sort your population by that very rule:

//The "less fit" rule is defined so the population will be sorted
//in ascending order, if you want to sort them by descending order,
//just change the definition of your fitness rules accordingly.

sort(popvector, popvector + popsize);

Or you can use an ordered container to store population in the first place. Such choice can be set, map or priority_queue. The elements in ordered container will always follow the exact order you designated when you declared such container objects.

In your case I would suggest priority_queue because you can easily pop out the most unfitful human being from the top of the pile, like this:

#include<priority_queue>

//Still, the definition of "priority" is required beforehand
priority_queue<person> pqPerson;

person tmp;
for(int i = 0; i < popsize; ++i){
    tmp.setfit(fit1, fit2, overall);
    pqPerson.push(tmp);
}

for(int generation = 0; generation < X; +=generation){
    //The weakest group will perish
    for(int j = 0; j < tollSize; ++j){
        pqPerson.pop();
    }

    //Crossover and Mutation process
}
Lotayou
  • 162
  • 11