Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rocco haro Genetic Alg. Pop->Threads #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ void Bot::checkForBuySignal(double currPrice)
if(getMonies() > currPrice) // can afford?
{
int monInt = (int) getMonies();
int price = (int) currPrice;
updateNumStocks(floor(monInt/price)-1);
int price = (int) currPrice;updateNumStocks(floor(monInt/price)-1);

updateMonies(getMonies()-(getNumStocks()*currPrice));
updateAssets(getAssets() +(getNumStocks()*currPrice));
Expand Down
2 changes: 2 additions & 0 deletions Bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#ifndef BOT_H_
#define BOT_H_
#include <cmath>
#include <iostream>
using namespace std;
class Bot {
public:
Bot();
Expand Down
67 changes: 59 additions & 8 deletions Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Driver.h"
#include "Exchange.h"
#include "GenAlg.h"
#include <iostream>
#include <string>
#include <stdlib.h>
Expand All @@ -29,16 +30,39 @@ int main()
// the type of moving averge 'D' (day) 'H' (hour) 'M' (min),
// and the number of experiments

Exchange* temp = new Exchange(10,40,'A',10);
Exchange* temp = new Exchange(10,40,'A',2);
Results data;
cout <<"Done .1.." << endl;


//Testing Population class
Population test(10000, 5);
test.generateGeneration0();
test.printPop();
test.runIndividualFitnessAlgorithm();
test.printPop();
Individual father = test.randomlySelectFromPop();
Individual mother = test.randomlySelectFromPop();
Individual child;
cout << "Father: " << "Mov1: " << father.getAttributeAt(0) << " Mov2: " << father.getAttributeAt(1) << " SF: " << father.getAttributeAt(2) << endl ;
cout << "Mother: " << "Mov1: " << mother.getAttributeAt(0) << " Mov2: " << mother.getAttributeAt(1)<< " SF: " << mother.getAttributeAt(2) << endl ;
child = test.reproduce(father, mother);
cout << "Child: " << "Mov1: " << child.getAttributeAt(0) << " Mov2: " << child.getAttributeAt(1) << " SF: " << child.getAttributeAt(2) << endl ;
test.addChildToPos(0, child);
cout << "Adding child..." << endl;
test.printPop();
cout << "Testing fitness function" << endl;
// test.runIndividualFitnessAlgorithm();



// data = temp->getFitness(100,10000);
// Within each exchange, a series of different tests will be performed
// that will vary the savings Factor and the initial starting money

// Savings factor will vary from 2% to 50% of initial starting money
// whereas initial starting money will be either 10k, 50k, 100k, 500k
data = temp->getFitness(200, 10000);
//data = temp->getFitness(200, 10000);
// TODO
// Keep track of the range of perfomance for those with high
// fitness values, update results.h with an int array that holds
Expand All @@ -49,16 +73,18 @@ int main()
// the distribution[], starting Money,


cout <<"Done .2.." << endl;
cout << "Fitness: " << data.getTotalValue() << endl;
cout << "Min: " << data.getMin() << " | Max: " << data.getMax() << endl;
// cout <<"Done .2.." << endl;
// cout << "Fitness: " << data.getTotalValue() << endl; // access the fitness value

cout << "Graph attr: min - " << data.getAttributesForDistr()->getMin() << " max - " << data.getAttributesForDistr()->getMax() << " ";
cout << "interval - " << data.getAttributesForDistr()->getInterval() << endl;
// cout << "Min: " << data.getMin() << " | Max: " << data.getMax() << endl;

// cout << "Graph attr: min - " << data.getAttributesForDistr()->getMin() << " max - " << data.getAttributesForDistr()->getMax() << " ";
// cout << "interval - " << data.getAttributesForDistr()->getInterval() << endl;
// Only access the distrubtion graph if it is one of the chosen individuals
// from the population
/*
int num=0;
cout << "Distribution performance " << endl;
int range = data.getAttributesForDistr()->getMin();
for (int j=0; j < 40; j++)
{
Expand All @@ -69,8 +95,33 @@ int main()
printX(num);
cout << endl;
}
delete temp;
//delete temp;
*/
// cout << "+++++++++++++++++++++++++++++++++++++++++++++++++" << endl;

/*
cout << "Graph Perf-vs-Price attr: min - " << data.getAttributesForPerfVsPrice()->getMin() << " max - " << data.getAttributesForPerfVsPrice()->getMax() << " ";
cout << "interval - " << data.getAttributesForPerfVsPrice()->getInterval() << endl;
int range1 =data.getAttributesForPerfVsPrice()->getMin();

for (int k=0; k < 40; k++)
{
//cout << range1 << " - ";
range1+=data.getAttributesForPerfVsPrice()->getInterval();
//cout << range1 << " : ";

for (int p=0; p < data.getSizeOfListAt(k); p++ )
{
//cout << " " << data.getListAt_PositionAt(k,p) << " ";
}
//cout << endl;
}
*/






//tempF = temp->getFitness(20,5,10);
// cout << "Fitness: " << tempF << endl;
Expand Down
152 changes: 134 additions & 18 deletions Exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Results Exchange::startSim()

int prevDay=0;

Queue pricePerDay; // keeps record of all the prices during the
Queue* pricePerDay = new Queue(); // keeps record of all the prices during the
// existence of the stock

while(!yearHasPassed() && !crashed()) // adjust accordingly depending on type of sim
Expand All @@ -70,7 +70,8 @@ Results Exchange::startSim()
// updates queue for moving averages on a new day
if (newDay(prevDay))
{
pricePerDay.enqueue(new Node(getPrice(), getTime()->copy()));

pricePerDay->enqueue(new Node(getPrice(), getTime()->copy()));
//cout << "Monies: " << monies << " | Assets: " << assets << " | Savings: " << savings << endl;

trader.updatePPA1(getMovingAvg(days1));
Expand Down Expand Up @@ -113,9 +114,9 @@ Results Exchange::startSim()

}

/*
cout << "Moving1: " << endl;

// cout << "Moving1: " << endl;
/*
Node *data = days1.dequeue();
while(!days1.isEmpty())
{
Expand All @@ -128,9 +129,10 @@ Results Exchange::startSim()
else { cout << "NULL" << endl; }
data = days1.dequeue();
}
cout << "-----------" << endl;
cout << "Moving2: " << endl;

*/
// cout << "-----------" << endl;
// cout << "Moving2: " << endl;
/*
while(!days2.isEmpty())
{
data = days2.dequeue();
Expand Down Expand Up @@ -161,6 +163,9 @@ ExperimentType* Exchange::getExp() { return exp; }

int Exchange::getCorrespondingPosition(double portfolioVal, int interval, int min)
{
//cout << "Value: " << portfolioVal << " Min: " << min << endl;
//cout << "Subtract: " << portfolioVal-min << endl;
//cout << "Interval: " << interval << endl;
return floor( ( ((int) portfolioVal) - min ) / interval );
}

Expand Down Expand Up @@ -274,6 +279,7 @@ Results Exchange::getFitness(int savingFact, double startMon)
//cout << "working..." << endl;

// Store result from startSim in array()
cout << "Trial# " << count << endl;
data[count] = sim->startSim();
//cout << "Value stored: " << data[count].getTotalValue() << endl;
if (data[count].getTotalValue() < min_max.getMin()) { min_max.setMin(data[count].getTotalValue()); }
Expand All @@ -299,43 +305,52 @@ Results Exchange::getFitness(int savingFact, double startMon)
int min, max, interval, intervalPrice, positionToIncrement; min=max=interval=0;

// Processing for performance distribution
cout << "Pre flooring " << min_max.getMin() << endl;
cout << "Pre ceiling " << min_max.getMax() << endl;
// cout << "Pre flooring " << min_max.getMin() << endl;
// cout << "Pre ceiling " << min_max.getMax() << endl;
min = floor( ((int)min_max.getMin()) / 100 )*100;
cout << "Min: " << min << endl;
// cout << "Min: " << min << endl;
max = ceil( (int)min_max.getMax()/100)*100 + 100;
cout << "Max: " << max << endl;
//cout << "Max: " << max << endl;
interval = (max - min)/40;

min_max.getAttributesForDistr()->setMin(min);
min_max.getAttributesForDistr()->setMax(max);
min_max.getAttributesForDistr()->setInterval(interval);

cout << "Interval: " << interval << endl;
//cout << "Interval: " << interval << endl;

// processing for stock price vs. perfomance relation
// NOTE : min, max, and interval for the price vs performance is denoted
// with the word "Price" appended to the variable name
cout << "------ Stock price vs Performance " << endl;
cout << "Min: " << minPrice << endl << "Max: " << maxPrice << endl;
minPrice = floor(minPrice/10)*10;
maxPrice = ceil(maxPrice/10)*10+10;
minPrice = floor(minPrice/10)*10 - 10;
maxPrice = ceil(maxPrice/10)*10+50;
cout << "Min: " << minPrice << endl << "Max: " << maxPrice << endl;
intervalPrice = (maxPrice-minPrice)/40;
intervalPrice = (20+maxPrice-minPrice)/40;
cout << "Interval: " << intervalPrice << endl << endl;
min_max.getAttributesForPerfVsPrice()->setMin(minPrice);
min_max.getAttributesForPerfVsPrice()->setMax(maxPrice);
min_max.getAttributesForPerfVsPrice()->setInterval(intervalPrice);

for (int j=0; j < getExp()->getNumTests(); j++ )
{

//cout << " " << data[j].getTotalValue() << endl;
// TODO
// Add logic to add in the performance


// TODO
// Uncomment the increment distribution function !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
positionToIncrement = getCorrespondingPosition(data[j].getTotalValue(), interval, min);
//cout << "Postion to increment: " << positionToIncrement << endl;
min_max.incrementDistributionAt(positionToIncrement);

positionToIncrement = getCorrespondingPosition(data[j].getFinalPrice(), intervalPrice, minPrice);
//cout << "Position to increment: " << positionToIncrement << endl;
if (positionToIncrement >= 40) { cout << "Error in getFitness() -- position to increment too high" << endl; }
else
{
min_max.addToPerfVsPrice(positionToIncrement, data[j].getTotalValue());
}
// TODO
// Write all of the stock lifecycles to a file by outputting each queue

Expand All @@ -349,21 +364,122 @@ Results Exchange::getFitness(int savingFact, double startMon)
//TODO
// update to subtract by the amount of starting money, getExp().getStartingMonies()
double fit = (sum/(double)getExp()->getNumTests() - startMon);


//TODO
// solve for a more intricate way of calculating the fitness score for perfomance
cout << "F: " << fit << endl;
min_max.setTotalVal(fit); // include fitness value into min_max
//cout << "check if set " << min_max.getTotalValue() << endl;
data[getExp()->getNumTests()] = min_max;

//Results* datatemp[getExp()->getNumTests()];
//*datatemp = data;

//CSVExport(data, getExp()->getNumTests());


return min_max;
}


void Exchange::CSVExport(Results data[], int size)
{
// Gives us the number of elements of type Results in the array "data"

Node* temp = nullptr;
bool done = false;
cout << "Number of trials: " << size << endl;
ofstream Data_File;
Data_File.open("StockPriceLifeTimes.csv");
if (Data_File.is_open())
{
cout << "Reading file " << endl;
}
else
{
cout << "Error: could not read file " << endl;
}
Data_File << "Stock Final Price,Stock Total Value,Money,Assets,Savings,Number of Stocks, Minimum Performance, Maximum Performance";
for (int i = 1; i <= size; i++)
{
Data_File << ",Trial " << (i);
}

Data_File << "\n";

for (int i = 0; i < size; i++)
{
Data_File << data[i].getFinalPrice() << "," << data[i].getTotalValue() << "," << data[i].getMoney()
<< "," << data[i].getAssets() << "," << data[i].getSavings() << "," << data[i].getNumStocks() << ","
<< data[i].getMin() << "," << data[i].getMax();

if (!data[0].getLifeCycle()->isEmpty())
{
for (int j = 0; j < size; j++)
{
temp = data[j].getLifeCycle()->dequeue();
Data_File << "," << temp->getPrice();
}
}
Data_File << "\n";
}

if (!data[0].getLifeCycle()->isEmpty())
{
while (!done)
{
Data_File << " " << "," << " " << "," << " "
<< "," << " "<< "," << " "<< "," << " " << ","
<< " " << "," << " ";
for (int j = 0; j < size; j++)
{
Data_File << "," << data[j].getLifeCycle()->dequeue()->getPrice();
}
Data_File << "\n";
if (data[0].getLifeCycle()->isEmpty() == true)
{
done = true;
}
}
}
Data_File.close();




/*
* Now writing the Performance vs. Stock price data
*

Data_File.open("PerformanceVsPriceData.csv");
if (Data_File.is_open())
{
cout << "Reading file " << endl;
}
else
{
cout << "Error: could not read file " << endl;
}
cout << "Graph Perf-vs-Price attr: min - " << data[size].getAttributesForPerfVsPrice()->getMin() << " max - " << data[size].getAttributesForPerfVsPrice()->getMax() << " ";
cout << "interval - " << data[size].getAttributesForPerfVsPrice()->getInterval() << endl;
int range1 =data[size].getAttributesForPerfVsPrice()->getMin();

for (int k=0; k < 40; k++)
{
Data_File << range1 << "-";
range1+=data[size].getAttributesForPerfVsPrice()->getInterval();
Data_File << range1 << "";

for (int p=0; p < data[size].getSizeOfListAt(k); p++ )
{
Data_File << "," << data[size].getListAt_PositionAt(k,p);
}
Data_File << "\n";
}

*/
}



Expand Down
2 changes: 2 additions & 0 deletions Exchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ExperimentType.h"
#include "Results.h"
#include "Bot.h"
#include <fstream>
#include <time.h>
#include <pthread.h>
using namespace std;
Expand All @@ -27,6 +28,7 @@ class Exchange {
Results getFitness(int savingFact, double startMon); // change to return a "Result" object
void printByDay();
void tick(bool pass);
void CSVExport(Results data[], int size);
ExperimentType* getExp();

int getCorrespondingPosition(double portfolioVal, int interval, int min); // O(1) !!!!!!!!!!!!!!!!!!!!!!!!!!
Expand Down
Loading