Init version

This commit is contained in:
2024-10-03 18:43:04 +07:00
commit f80052961f
186 changed files with 71676 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
#include "Individ.h"
#define _USE_MATH_DEFINES
#include <cmath>
float Individ::sigmaX = 1.f;
float Individ::sigmaE = 0.1f;
float Individ::sigmaC1 = 1.f;
float Individ::sigmaC2 = 1.f;
float Individ::sigmaC3= 1.f;
float Individ::sigmaC4= 1.f;
float Individ::sigmaC5= 1.f;
float Individ::Xopt = 110.f;
std::vector<float> Individ::c(5);
Individ::Individ(float _E) : E(_E) {};
IndividFeedBack::IndividFeedBack(float _E) : Individ(_E), C2(Individ::c[1]) {}
//IndividFeedBack(const IndividFeedBack& parent) : Individ(parent.E), C2(parent.C2) {}
float Individ::getFitness() const {
float X = c[0]/c[2] * (E+c[3]);
float fitness = 1/(sqrt(2*M_PI)) * exp(-0.5*pow((X-Xopt)/sigmaX,2.f));
return fitness;
}
float IndividFeedBack::getFitness() const {
//float X = -c[1]/2 + sqrt( c[1]*c[1]/4 + c[4]*c[1]/c[2]*(E+c[3]));
float X = -C2/2 + sqrt( C2*C2/4 + c[4]*C2/c[2]*(E+c[3]));
float fitness = 1/(sqrt(2*M_PI)) * exp(-0.5*pow((X-Xopt)/sigmaX,2.f));
return fitness;
}
void Individ::initConstants(){
c[0] = 10.f;
c[1] = 100.f;
c[2] = 1.f;
c[3] = 1.f;
c[4] = 21.f;
Xopt = 110.f;
sigmaX = 1.f;
sigmaE = 0.1f;
}
void Individ::initConstantsFromGUI(float c1, float c2, float c3, float c4, float c5, float Xoptim, float sigX, float sigE, float sigC1, float sigC2,
float sigC3, float sigC4, float sigC5)
{
c[0] = c1;
c[1] = c2;
c[2] = c3;
c[3] = c4;
c[4] = c5;
Xopt = Xoptim;
sigmaX = sigX;
sigmaE = sigE;
sigmaC1 = sigC1;
sigmaC2 = sigC2;
sigmaC3 = sigC3;
sigmaC4 = sigC4;
sigmaC5 = sigC5;
}
float IndividFeedBack::getOptimalE(){
float a = (Xopt + c[1]/2)*(Xopt + c[1]/2);
float b = c[1]*c[1]/4;
float ans = (a-b)*c[2]/(c[4]*c[1]) - c[3];
return ans;
}
bool compareOnFitness(const Individ* a, const Individ* b){
return a->getFitness() < b->getFitness();
}

View File

@@ -0,0 +1,39 @@
#pragma once
#include <vector>
class Individ {
protected:
float E;
public:
static std::vector<float> c;
static float Xopt, sigmaX, sigmaE, sigmaC1, sigmaC2, sigmaC3, sigmaC4, sigmaC5;
static void initConstants();
static void initConstantsFromGUI(float c1, float c2, float c3, float c4, float c5, float Xoptim, float sigX, float sigE, float sigC1, float sigC2,
float sigC3, float sigC4, float sigC5);
Individ(float _E);
//virtual Individ~();
float getE() const { return E;}
virtual float getFitness() const;
virtual bool hasFeedBack() const {return false;}
virtual void mutate (float deltaE) { E += deltaE;}
virtual void mutateC2(float deltaC2) {}
virtual void setC2(float v){}
virtual float getC2() const { return c[1];}
static float getOptimalE() { return Xopt*c[2]/c[0] - c[3];}
};
class IndividFeedBack : public Individ {
float C2;
public:
IndividFeedBack(float _E);
IndividFeedBack(const IndividFeedBack& parent);
virtual float getFitness() const;
virtual bool hasFeedBack() const {return true;}
virtual void mutateC2(float deltaC2) { this->C2 += deltaC2;}
static float getOptimalE();
virtual void setC2(float v){ this->C2 = v;}
virtual float getC2() const { return this->C2;}
};
bool compareOnFitness(const Individ* a, const Individ* b);

View File

@@ -0,0 +1,10 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H
namespace
{
static const int WIDGET_XT = 1;
static const int WIDGET_XY = 2;
}
#endif // CONSTANTS_H

View File

@@ -0,0 +1,44 @@
#include "createrangedialog.h"
#include "limits.h"
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QGridLayout>
CreateRangeDialog::CreateRangeDialog(QWidget *parent, int flag)
: QDialog(parent)
{
if (flag == 1) //iterate window
{
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(new QLabel(tr("Add new iterations:")), 0, 0);
iterations = new QLineEdit;
layout->addWidget(iterations, 0, 1);
QHBoxLayout *buttonLayout = new QHBoxLayout;
layout->addLayout(buttonLayout, 1, 0, 1, 0);
buttonLayout->addStretch();
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
buttonLayout->addWidget(cancelButton);
QPushButton *okButton = new QPushButton(tr("OK"));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
buttonLayout->addWidget(okButton);
okButton->setDefault(true);
}
}
QString CreateRangeDialog::iterField()
{
return iterations->text();
}

View File

@@ -0,0 +1,33 @@
#ifndef CREATERANGEDIALOG_H
#define CREATERANGEDIALOG_H
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QDialog>
#include <QSpinBox>
#include <QCheckBox>
#include "constants.h"
class CreateRangeDialog : public QDialog
{
Q_OBJECT
public:
CreateRangeDialog(QWidget *parent, int flag);
QString iterField();
private slots:
private:
QLineEdit *iterations;
};
#endif // CREATERANGEDIALOG_H

View File

@@ -0,0 +1,16 @@
#include "history.h"
History::History()
{
}
QVector <data> History::getAllHistory()
{
return singleModelHistory;
}
data History::getSingleModelHistory(int i)
{
return singleModelHistory[i];
}

View File

@@ -0,0 +1,33 @@
#ifndef HISTORY_H
#define HISTORY_H
#include <QVector>
struct data{
QVector<double> withFeedbackVect;
QVector<double> withoutFeedbackVect;
QVector<double> firstXVect;
QVector<double> meanE;
QVector<double> meanF;
QVector<double> meanC2;
QVector<double> individ;
QVector<double> individFeedback;
};
class History
{
public:
History();
QVector <data> getAllHistory();
data getSingleModelHistory(int i);
data vectors;
QVector <data> singleModelHistory;
private:
};
#endif // HISTORY_H

View File

@@ -0,0 +1,119 @@
#include "paintqwidget.h"
#include "constants.h"
static const int DEFAULT_IMAGE_HEIGHT = 550;
static const int DEFAULT_IMAGE_WIDTH = 800;
static const int AXIS_DASH_STEP = 50;
static const int INDENT_STEP = 50;
static const int TOP_OFFSET = 100;
PaintQWidget::PaintQWidget(QWidget *parent) :
QWidget(parent), image(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, QImage::Format_RGB888)
{
connect(&solver, SIGNAL(sendHistory(QVector<dataSet>, int)), SLOT(setHistory(QVector<dataSet>, int)));
connect(&solver, SIGNAL(sendItersForProgressBar(int)), SLOT(setItersForProgressBar(int)));
image.fill(QColor(Qt::white).rgb());
}
void PaintQWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(0, 0, image);
}
void PaintQWidget::setHistory(QVector<dataSet> history, int iters)
{
hist = history;
maxIters = iters;
}
void PaintQWidget::setItersForProgressBar(int iters)
{
resentItersToProgressBar(iters); //cause I can't send it direct from the solver =(
//std::cout << iters << std::endl;
}
void PaintQWidget::drawSelectedModel(int wgt_type, QCustomPlot *pPlot, int model_num)
{
//std::cout << model_num << std::endl;
if (model_num != -1)
{
draw(pPlot, wgt_type, model_num);
}
}
//draw graphics
void PaintQWidget::draw(QCustomPlot *customPlot, int wgt_type, int model_num)
{
//std::cout << model_num << std::endl;
if (wgt_type == WIDGET_XT)
{
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
customPlot->graph(0)->setData(hist[model_num].xRange, hist[model_num].withFeedbackVect);
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->graph(0)->setName("withFeedback");
customPlot->graph(1)->setData(hist[model_num].xRange, hist[model_num].withoutFeedbackVect);
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->graph(1)->setName("withoutFeedback");
customPlot->xAxis->setRange(0, maxIters);
//std::cout << maxIters << std::endl;
customPlot->xAxis->setLabel("Iterations");
customPlot->yAxis->setLabel("Feedbacks");
customPlot->yAxis->setRange(0, qMax(hist[model_num].maxWithFeedVal, hist[model_num].maxWithoutFeedVal) + TOP_OFFSET);
}
else if (wgt_type == WIDGET_XY)
{
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
customPlot->graph(0)->setName("eMeans");
customPlot->graph(0)->setData(hist[model_num].xRange, hist[model_num].meanE);
customPlot->graph(0)->setPen(QPen(Qt::red));
customPlot->graph(1)->setName("fMeans");
customPlot->graph(1)->setData(hist[model_num].xRange, hist[model_num].meanF);
customPlot->graph(1)->setPen(QPen(Qt::blue));
customPlot->graph(2)->setName("c2Means");
customPlot->graph(2)->setData(hist[model_num].xRange, hist[model_num].meanC2);
customPlot->graph(2)->setPen(QPen(Qt::green));
customPlot->graph(3)->setName("individ");
customPlot->graph(3)->setData(hist[model_num].xRange, hist[model_num].individ);
customPlot->graph(3)->setPen(QPen(Qt::magenta));
customPlot->graph(4)->setName("individFeedback");
customPlot->graph(4)->setData(hist[model_num].xRange, hist[model_num].individFeedback);
customPlot->graph(4)->setPen(QPen(Qt::black));
customPlot->xAxis->setRange(0, maxIters);
//std::cout << maxIters << std::endl;
customPlot->xAxis->setLabel("Iterations");
customPlot->yAxis->setLabel("Means");
customPlot->yAxis->setRange(0, 300);
}
customPlot->replot();
}
void PaintQWidget::setIters(int flag, int iter)
{
itersFlag = flag;
newIters = iter;
}
void PaintQWidget::drawAll(float c1, float c2, float c3, float c4, float c5, float sigC1, float sigC2, float sigC3, float sigC4, float sigC5,
float e, float sigE, float sigX0, float delX0, float sigX, float Xopt, int popSize, int maxSteps, int N,
int wgt_type, QCustomPlot* pPlot, int count, int modelCount, int model_num)
{
if (count == 0) // cause solveModel launch two times other way
{
solver.solveModel(c1, c2, c3, c4, c5, sigC1, sigC2, sigC3, sigC4, sigC5, e, sigE, sigX0, delX0, sigX, Xopt, popSize, maxSteps, N, itersFlag, newIters, modelCount);
}
draw(pPlot, wgt_type, model_num);
}

View File

@@ -0,0 +1,67 @@
#ifndef PAINTQWIDGET_H
#define PAINTQWIDGET_H
#include <QWidget>
#include <QImage>
#include <QPainter>
#include <qmath.h>
#include <QMouseEvent>
#include <QVector>
#include <iostream>
#include "solver.h"
#include "qcustomplot.h"
class PaintQWidget : public QWidget
{
Q_OBJECT
public:
explicit PaintQWidget(QWidget *parent = 0);
QVector<dataSet> hist;
private slots:
void drawAll(float c1, float c2, float c3, float c4, float c5, float sigC1, float sigC2, float sigC3, float sigC4, float sigC5,
float e, float sigE, float sigX0, float delX0, float sigX, float Xopt, int popSize, int maxSteps, int N,
int wgt_type, QCustomPlot* pPlot, int count, int modelCount, int model_num);
void setIters(int flag, int iter);
void setHistory(QVector<dataSet> history, int iters);
void drawSelectedModel(int wgt_type, QCustomPlot* pPlot, int model_num);
void setItersForProgressBar(int iters);
signals:
void sendHistoryToPrint(QVector<dataSet> history);
void resentItersToProgressBar(int iters);
private:
void paintEvent(QPaintEvent *);
void solveModel(int x0, int y0, double a, double b, double c, double d, double h, int t);
void draw(QCustomPlot *customPlot, int wgt_type, int model_num);
QVector<double> withFeed;
QVector<double> withoutFeed;
QVector<double> eMeans;
QVector<double> fMeans;
QVector<double> c2Means;
QVector<double> individ;
QVector<double> individFeed;
int newIters;
int itersFlag;
double maxWith;
double maxWithout;
QVector<double> xVals;
QImage image;
int maxIters;
int iters;
Solver solver;
int runCount;
};
#endif // PAINTQWIDGET_H

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,352 @@
#include "solver.h"
#include "iostream"
#include <QTextStream>
Solver::Solver(QObject *parent) :
QObject(parent)
{
}
Solver::~Solver()
{
}
//calculates function values and puts it into vectors(predatorSolveVal, preySolveVal)
void Solver::solveModel(float c1, float c2, float c3, float c4, float c5, float sigC1, float sigC2, float sigC3, float sigC4, float sigC5,
float e, float sigE, float sigX0, float delX0, float sigX, float Xopt, int popSize, int maxSteps, int N, int flag, int iter, int modelCount){
itersFlag = flag;
newIters = iter;
modCount = modelCount;
int k = 0;
if (itersFlag == 0)
{
popVect.clear();
singleModelHistory.clear();
for (int i = 0; i < modelCount; i++)
{
k++;
withFeedbackVect.clear();
withoutFeedbackVect.clear();
firstXVect.clear();
maxWithFeedVal = 0;
maxWithoutFeedVal = 0;
//firstXVect.clear();
meanE.clear();
meanF.clear();
meanC2.clear();
individ.clear();
individFeedback.clear();
int POP_SIZE = popSize;
int MAX_STEPS = maxSteps;
addSteps = maxSteps;
//float DELTA_Xopt = 0.0f;
float DELTA_Xopt = delX0;
//float DELTA_Xopt = 0.4f;
//float DELTA_Xopt = 0.5f;
//float DELTA_Xopt = 1.0f;
N_forFeedback = N;
population.resize(POP_SIZE);
popVect.push_back(population);
//Individ::initConstants();
Individ::initConstantsFromGUI(c1, c2, c3, c4, c5, Xopt, sigX, sigE, sigC1, sigC2, sigC3, sigC4, sigC5);
initialization(popVect[i], N_forFeedback);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(int steps = 0; steps < MAX_STEPS; steps++) {
//mutationSimple(population); // 1) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mutationComplex(popVect[i]);
selection(popVect[i], 0.02f); // 2) <20><><EFBFBD><EFBFBD><EFBFBD>
printStatistics(popVect[i], i, 0, steps);
firstXVect.push_back(steps);
maxWithFeedVal = qMax(withFeedbackVect[steps], maxWithFeedVal);
maxWithoutFeedVal = qMax(withoutFeedbackVect[steps], maxWithoutFeedVal);
Individ::Xopt += DELTA_Xopt;
} // (END) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
lastXopt = Individ::Xopt;
vectors.withFeedbackVect = withFeedbackVect;
vectors.withoutFeedbackVect = withoutFeedbackVect;
vectors.individ = individ;
vectors.individFeedback = individFeedback;
vectors.meanE = meanE;
vectors.meanC2 = meanC2;
vectors.meanF = meanF;
vectors.xRange = firstXVect;
vectors.maxWithFeedVal = maxWithFeedVal;
vectors.maxWithoutFeedVal = maxWithoutFeedVal;
singleModelHistory.push_back(vectors);
emit sendItersForProgressBar(i);
}
if (k == modelCount)
{
for (int m = 0; m < withFeedbackVectAvg.size(); m++)
{
withFeedbackVectAvg[m] /= modCount;
withoutFeedbackVectAvg[m] /= modCount;
individAvg[m] /= modCount;
individFeedbackAvg[m] /= modCount;
meanC2Avg[m] /= modCount;
meanEAvg[m] /= modCount;
meanFAvg[m] /= modCount;
}
vectors.withFeedbackVect = withFeedbackVectAvg;
vectors.withoutFeedbackVect = withoutFeedbackVectAvg;
vectors.individ = individAvg;
vectors.individFeedback = individFeedbackAvg;
vectors.meanE = meanEAvg;
vectors.meanC2 = meanC2Avg;
vectors.meanF = meanFAvg;
singleModelHistory.push_back(vectors);
emit sendItersForProgressBar(modCount);
}
emit sendHistory(singleModelHistory, addSteps);
}
if (itersFlag == 1)
{
int n = 0;
float DELTA_Xopt = delX0;
for (int i = 0; i < modelCount; i++)
{
n++;
Individ::Xopt = lastXopt;
//std::cout << Individ::Xopt << std::endl;
N_forFeedback = N;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(int steps = addSteps; steps < addSteps + newIters; steps++) {
//mutationSimple(population); // 1) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mutationComplex(popVect[i]);
selection(popVect[i], 0.02f); // 2) <20><><EFBFBD><EFBFBD><EFBFBD>
printStatistics(popVect[i], i, 1, steps);
if (i == 0)
{
firstXVect.push_back(steps);
}
Individ::Xopt += DELTA_Xopt;
maxWithFeedVal = qMax(withFeedbackVect[steps], maxWithFeedVal);
maxWithoutFeedVal = qMax(withoutFeedbackVect[steps], maxWithoutFeedVal);
} // (END) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
singleModelHistory[i].xRange = firstXVect;
singleModelHistory[i].maxWithFeedVal = maxWithFeedVal;
singleModelHistory[i].maxWithoutFeedVal = maxWithoutFeedVal;
emit sendItersForProgressBar(i);
}
if (n == modCount) // index n - for last struct in history for averages
{
for (int m = addSteps; m < addSteps + newIters; m++)
{
singleModelHistory[modCount].withFeedbackVect[m] /= modCount;
singleModelHistory[modCount].withoutFeedbackVect[m] /= modCount;
singleModelHistory[modCount].individ[m] /= modCount;
singleModelHistory[modCount].individFeedback[m] /= modCount;
singleModelHistory[modCount].meanC2[m] /= modCount;
singleModelHistory[modCount].meanE[m] /= modCount;
singleModelHistory[modCount].meanF[m] /= modCount;
}
singleModelHistory[modCount].xRange = firstXVect;
}
lastXopt = Individ::Xopt;
addSteps += newIters;
emit sendItersForProgressBar(modCount);
emit sendHistory(singleModelHistory, addSteps);
}
}
void Solver::mutationSimple(std::vector<Individ*>& population){
boost::normal_distribution<> norm(0.0, Individ::sigmaE);
boost::mt19937 rng;
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> >
generator(rng, norm); // glues randomness with mapping
for(int i = 0; i < population.size(); i++){
float deltaE = generator();
//std::cout<<deltaE<<std::endl;
population.at(i)->mutate(deltaE);
}
}
void Solver::mutationComplex(std::vector<Individ*>& population){
boost::normal_distribution<> normE(0.0, Individ::sigmaE);
boost::normal_distribution<> normC2(0.0, Individ::sigmaC2);
boost::mt19937 rng;
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> >
generatorE(rng, normE);
boost::variate_generator<boost::mt19937&, boost::normal_distribution<> >
generatorC2(rng, normC2);
for(int i = 0; i < population.size(); i++){
float flag = generatorE();
if(flag >= 0.0f){
float deltaE = generatorE();
population.at(i)->mutate(deltaE);
}
else{
float deltaC2 = generatorC2();
population.at(i)->mutateC2(deltaC2);
}
}
}
void Solver::selection(std::vector<Individ*>& population, float renewRate){
std::sort(population.begin(),population.end(), compareOnFitness);
int inds = population.size();
float deathRate = renewRate;
float birthRate = renewRate; //Settings::BirthRate;//0.02f;
int toDie = int (inds* deathRate);
std::vector<Individ*>::iterator start = population.begin();
std::vector<Individ*>::iterator end = population.begin()+toDie;
for(std::vector<Individ*>::iterator it = start; it != end; it++){
delete *it;
}
population.erase(start, end);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int toBorn = int (inds * birthRate);
inds = population.size();
srand((unsigned int)time(NULL));
for(int i = 0; i < toBorn; i++) {
int parent = rand() % inds;
Individ* ind;
if(population.at(parent)->hasFeedBack()){
ind = new IndividFeedBack(population.at(parent)->getE());
ind->setC2(population.at(parent)->getC2());
}
else{
ind = new Individ(population.at(parent)->getE());
}
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20>.<2E>.
population.push_back(ind);
}
} // (END) void selection(std::vector<Individ*>& population, float renewRate)
void Solver::initialization(std::vector<Individ*>& population, int N_feedback){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int POP_SIZE = population.size();
float E = Individ::getOptimalE();// Xopt*Individ::c[2]/Individ::c[0] - Individ::c[3];
for(int i = 0; i < N_feedback; i++){
Individ* ind = new Individ(E);
population.at(i) = ind;
}
E = IndividFeedBack::getOptimalE();
for(int i = N_feedback; i < POP_SIZE; i++){
Individ* ind = new IndividFeedBack(E);
population.at(i) = ind;
}
} // (END) void initialization(std::vector<Individ*>& population){
void Solver::printStatistics(const std::vector<Individ*>& population, int i, int iters, int currentStep){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
withFeedback = 0.f;
withoutFeedback = 0.f;
meanC2Feedback = 0.f;
meanEFeedBack = 0.f;
meanENonFeedBack = 0.f;
for(int i = 0; i < population.size(); i++){
if(population.at(i)->hasFeedBack()){
withFeedback++;
meanC2Feedback += population.at(i)->getC2();
meanEFeedBack += population.at(i)->getE();
}
else{
withoutFeedback++;
meanENonFeedBack += population.at(i)->getE();
}
}
if (iters == 1) //count average for additional iters
{
singleModelHistory[i].withFeedbackVect.push_back(withFeedback);
singleModelHistory[i].withoutFeedbackVect.push_back(withoutFeedback);
singleModelHistory[i].individ.push_back(Individ::getOptimalE());
singleModelHistory[i].individFeedback.push_back(IndividFeedBack::getOptimalE());
singleModelHistory[i].meanE.push_back(meanENonFeedBack/withoutFeedback);
singleModelHistory[i].meanC2.push_back(meanC2Feedback/withFeedback);
singleModelHistory[i].meanF.push_back(meanEFeedBack/withFeedback);
if (i == 0)
{
singleModelHistory[modCount].withFeedbackVect.push_back(withFeedback);
singleModelHistory[modCount].withoutFeedbackVect.push_back(withoutFeedback);
singleModelHistory[modCount].meanE.push_back(meanENonFeedBack/withoutFeedback);
singleModelHistory[modCount].meanF.push_back(meanEFeedBack/withFeedback);
singleModelHistory[modCount].meanC2.push_back(meanC2Feedback/withFeedback);
singleModelHistory[modCount].individ.push_back(Individ::getOptimalE());
singleModelHistory[modCount].individFeedback.push_back(IndividFeedBack::getOptimalE());
}
else
{
singleModelHistory[modCount].withFeedbackVect[currentStep] += withFeedback;
singleModelHistory[modCount].withoutFeedbackVect[currentStep] += withoutFeedback;
singleModelHistory[modCount].meanE[currentStep] += meanENonFeedBack/withoutFeedback;
singleModelHistory[modCount].meanF[currentStep] += meanEFeedBack/withFeedback;
singleModelHistory[modCount].meanC2[currentStep] += meanC2Feedback/withFeedback;
singleModelHistory[modCount].individ[currentStep] += Individ::getOptimalE();
singleModelHistory[modCount].individFeedback[currentStep] += IndividFeedBack::getOptimalE();
}
}
if (i == 0 && iters == 0) // count average for new calc
{
withFeedbackVectAvg.push_back(withFeedback);
withoutFeedbackVectAvg.push_back(withoutFeedback);
meanEAvg.push_back(meanENonFeedBack/withoutFeedback);
meanFAvg.push_back(meanEFeedBack/withFeedback);
meanC2Avg.push_back(meanC2Feedback/withFeedback);
individAvg.push_back(Individ::getOptimalE());
individFeedbackAvg.push_back(IndividFeedBack::getOptimalE());
}
else if (iters == 0)
{
withFeedbackVectAvg[currentStep] += withFeedback;
withoutFeedbackVectAvg[currentStep] += withoutFeedback;
meanEAvg[currentStep] += meanENonFeedBack/withoutFeedback;
meanFAvg[currentStep] += meanEFeedBack/withFeedback;
meanC2Avg[currentStep] += meanC2Feedback/withFeedback;
individAvg[currentStep] += Individ::getOptimalE();
individFeedbackAvg[currentStep] += IndividFeedBack::getOptimalE();
}
//add vectors for first calc
withFeedbackVect.push_back(withFeedback);
withoutFeedbackVect.push_back(withoutFeedback);
meanE.push_back(meanENonFeedBack/withoutFeedback);
meanF.push_back(meanEFeedBack/withFeedback);
meanC2.push_back(meanC2Feedback/withFeedback);
individ.push_back(Individ::getOptimalE());
individFeedback.push_back(IndividFeedBack::getOptimalE());
// std::cout<<"W/o fb\t"<<withoutFeedback<<"\tW/fb\t"<<withFeedback;
// std::cout<<"\tOptE(w/o/fb)\t"<<Individ::getOptimalE();
// std::cout<<"\tmE(w/o/fb)\t"<<meanENonFeedBack/withoutFeedback;
// std::cout<<"\tOptE(w/fb)\t"<<IndividFeedBack::getOptimalE();
// std::cout<<"\tmE(w/fb)\t"<<meanEFeedBack/withFeedback;
// std::cout<<"\tmC2(w/fb)\t"<<meanC2Feedback/withFeedback;
// std::cout<<std::endl;
}

View File

@@ -0,0 +1,112 @@
#ifndef SOLVER_H
#define SOLVER_H
#include <QVector>
#include <QObject>
#include <QThread>
#include <QFile>
#include <iostream>
#include <qmath.h>
#include <list>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include "Individ.h"
#include "history.h"
struct dataSet{
QVector<double> withFeedbackVect;
QVector<double> withoutFeedbackVect;
QVector<double> xRange;
QVector<double> meanE;
QVector<double> meanF;
QVector<double> meanC2;
QVector<double> individ;
QVector<double> individFeedback;
//int maxIters;
double maxWithFeedVal;
double maxWithoutFeedVal;
};
class Solver : public QObject
{
Q_OBJECT
public:
Solver(QObject *parent = 0);
~Solver();
void solveModel(float c1, float c2, float c3, float c4, float c5, float sigC1, float sigC2, float sigC3, float sigC4, float sigC5,
float e, float sigE, float sigX0, float delX0, float sigX, float Xopt, int popSize, int maxSteps, int N, int flag, int iter, int modelCount);
double solveF(int x, int y, int a, double b);
double solveG(int x, int y, int c, double d);
void initialization(std::vector<Individ*>& population, int N_feedback);
void mutationSimple(std::vector<Individ*>& population);
void mutationComplex(std::vector<Individ*>& population);
void selection(std::vector<Individ*>& population, float renewRate);
void printStatistics(const std::vector<Individ*>& population, int i, int iters, int currentStep);
private:
QVector<double> predatorSolveVal;
QVector<double> preySolveVal;
QVector<double> withFeedbackVect;
QVector<double> withoutFeedbackVect;
QVector<double> firstXVect;
QVector<double> meanE;
QVector<double> meanF;
QVector<double> meanC2;
QVector<double> individ;
QVector<double> individFeedback;
QVector<double> withFeedbackVectAvg;
QVector<double> withoutFeedbackVectAvg;
QVector<double> meanEAvg;
QVector<double> meanFAvg;
QVector<double> meanC2Avg;
QVector<double> individAvg;
QVector<double> individFeedbackAvg;
std::vector<Individ*> population;
QVector <std::vector<Individ*> > popVect;
double maxWithFeedVal;
double maxWithoutFeedVal;
int withFeedback;
int withoutFeedback;
int N_forFeedback;
float meanC2Feedback;
float meanEFeedBack;
float meanENonFeedBack;
int newIters;
int addSteps;
int itersFlag;
int modCount;
dataSet vectors;
QVector <dataSet> singleModelHistory;
double lastXopt;
signals:
void sendFeedVectors(QVector<double> withFeed, QVector<double> withoutFeed, QVector<double> xValsVect ,double maxWithFeed, double maxWithoutFeed);
void sendMeanVectors(int iters, QVector<double> meanEV, QVector<double> meanFV, QVector<double> meanC2V, QVector<double> individV, QVector<double> individFeedbackV);
void sendHistory(QVector<dataSet> history, int iters);
void sendItersForProgressBar(int iters);
public slots:
};
#endif // SOLVER_H