コード例 #1
0
ファイル: genetic.hpp プロジェクト: poseidon4o/neural-net
void mutationStep(std::vector<std::pair<Net<SIZE>*, double>> & grades) {
    sort(grades.begin(), grades.end(),
    [](const std::pair<Net<SIZE>*, double> & left, const std::pair<Net<SIZE>*, double> & right) {
        return left.second > right.second;
    });

    auto randNet = [&grades]() -> Net<SIZE> & {
        return *grades[randMax(grades.size() - 1)].first;
    };

    const Net<SIZE> & best = *grades.front().first;


    // cross the lower half with low chance of mutation
    for (int c = 0; c < grades.size() / 2; ++c) {
        *grades[c].first = cross(best, randNet());

        if (chance(0.05)) {
            mutate(*grades[c].first, 0.33);
        }
    }

    // mutate 20% of the middle
    for (int c = 0; c < grades.size() / 5; ++c) {
        mutate(*grades[c + grades.size() / 2].first, 0.5);
    }
}
コード例 #2
0
long randMinMax(long howsmall, long howbig)
{
	if (howsmall >= howbig) {
		return howsmall;
	}
	long diff = howbig - howsmall;
	return randMax(diff) + howsmall;
}
コード例 #3
0
ファイル: genetic.hpp プロジェクト: poseidon4o/neural-net
void random(Net<SIZE> & subject) {
    for (int c = 0; c < SIZE; ++c) {
        for (int r = 0; r < SIZE; ++r) {
            if (subject.hasSynapse(c, r)) {
                subject[c][r] = randMax(2) - 1.;
            }
        }
    }
}
コード例 #4
0
ファイル: genetic.hpp プロジェクト: poseidon4o/neural-net
void mutate(Net<SIZE> & subject, double rate) {
    for (int c = 0; c < SIZE; ++c) {
        for (int r = 0; r < SIZE; ++r) {
            if (subject.hasSynapse(c, r) && chance(rate)) {
                if (chance(0.5)) {
                    subject[c][r] += rand() - .5;
                } else {
                    subject[c][r] = randMax(2) - 1.;
                }
            }
        }
    }
}
コード例 #5
0
void EnvironementViewer::mouseMoveEvent ( QMouseEvent * event )//permet de gere les evenement souris lors de son deplacement
{
    if (IsPlay==false)
    {
        if (action == ADD_ONE)
        {
                if (CurrentSelectedEntity != NULL)
                {
                    QPoint position=event->pos();
                    position.setX(position.x()/PAS_DEPLACEMENT);
                    position.setY(position.y()/PAS_DEPLACEMENT);
                    environement->AddEntity(CurrentSelectedEntity,position.x(),position.y());
                }
        }
        else  if (action ==  ADD_LOT)
        {
            if (CurrentSelectedEntity != NULL)
            {
                QPoint position=event->pos();
                position.setX(position.x()/PAS_DEPLACEMENT -max_random/2);
                position.setY(position.y()/PAS_DEPLACEMENT -max_random/2);

                unsigned int nb=randMax(max_random)+1;

                for (unsigned int i=0;i<nb;++i)
                    environement->AddEntity(CurrentSelectedEntity,position.x()+(int)randMax(max_random),position.y()+randMax(max_random));
            }
        }

        else if (action ==  ADD_LINE)
        {
            if (CurrentSelectedEntity != NULL)
            {
                MoussPoint.x2=event->pos().x();
                MoussPoint.y2=event->pos().y();

                sf::ConvexShape Line(4);
                Line.setOutlineThickness(1);
                Line.setPoint(0,sf::Vector2f(MoussPoint.x1,MoussPoint.y1));
                Line.setPoint(3,sf::Vector2f(MoussPoint.x1,MoussPoint.y1));
                Line.setPoint(1,sf::Vector2f(MoussPoint.x2,MoussPoint.y2));
                Line.setPoint(2,sf::Vector2f(MoussPoint.x2,MoussPoint.y2));
                Line.setOutlineColor(sf::Color::Green);
                Line.setFillColor(sf::Color::Green);
                draw(Line);
            }
        }

        else  if (action ==  SELECTED)
        {
            MoussPoint.x2=event->pos().x();
            MoussPoint.y2=event->pos().y();

            sf::ConvexShape shape(4);
            shape.setFillColor(sf::Color::Transparent);
            shape.setOutlineColor(sf::Color::Green);
            shape.setOutlineThickness(1);

            shape.setPoint (0,sf::Vector2f(MoussPoint.x1,MoussPoint.y1));
            shape.setPoint (1,sf::Vector2f(MoussPoint.x2,MoussPoint.y1));
            shape.setPoint (2,sf::Vector2f(MoussPoint.x2,MoussPoint.y2));
            shape.setPoint (3,sf::Vector2f(MoussPoint.x1,MoussPoint.y2));

            draw(shape);
        }
        else  if (action == REMOVE)
        {
            environement->RemoveEntity(event->pos().x()/PAS_DEPLACEMENT,event->pos().y()/PAS_DEPLACEMENT);

        }
    }
};