Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
178 changes: 178 additions & 0 deletions CChromosome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* CChromosome.cpp
*
* Created on: Apr 24, 2015
* Author: Mark Wittekind and Drew Murray
*/

#include "CChromosome.h"
CChromosome::CChromosome(){
cout << "Default constructor of CChrom used\n";
m_par = NULL;
m_coreNum = 0;
}


CChromosome::CChromosome(Par* par, unsigned short coreNum) {
for(unsigned short i = 0; i<3;i++){
m_dvx.push_back(0);
m_dvy.push_back(0);
m_dvz.push_back(0);
m_t.push_back(0);
}
m_coreNum = coreNum;


unsigned seed = chrono::high_resolution_clock::now().time_since_epoch().count()+m_coreNum;
default_random_engine gen(seed);
uniform_real_distribution<float> distThrust(-1.0*(par->maxThrusterVel),par->maxThrusterVel);
uniform_int_distribution<int> distTime(0,par->maxTimeSteps);

m_par = par;
float x;
m_dvx[1] = -1.0*(par->maxThrusterVel);
m_dvx[2] = (par->maxThrusterVel);
m_dvx[0] = x = distThrust(gen);

m_dvy[1] = -1.0*(par->maxThrusterVel);
m_dvy[2] = (par->maxThrusterVel);
m_dvy[0] = distThrust(gen);

m_dvz[1] = -1.0*(par->maxThrusterVel);
m_dvz[2] = (par->maxThrusterVel);
m_dvz[0] = distThrust(gen);

m_t[1] = 0;
m_t[2] = float((par->maxTimeSteps));
m_t[0] = float(distTime(gen));
}

CChromosome::CChromosome(const CChromosome &rhs) {

m_par = rhs.m_par;
m_coreNum = rhs.m_coreNum;

for(unsigned short i = 0; i<3;i++){
m_dvx.push_back(0);
m_dvy.push_back(0);
m_dvz.push_back(0);
m_t.push_back(0);
}

m_dvx[1] = rhs.m_dvx[1];
m_dvx[2] = rhs.m_dvx[2];
m_dvx[0] = rhs.m_dvx[0];

m_dvy[1] = rhs.m_dvy[1];
m_dvy[2] = rhs.m_dvy[2];
m_dvy[0] = rhs.m_dvy[0];

m_dvz[1] = rhs.m_dvz[1];
m_dvz[2] = rhs.m_dvz[2];
m_dvz[0] = rhs.m_dvz[0];

m_t[1] = rhs.m_t[1];
m_t[2] = rhs.m_t[2];
m_t[0] = rhs.m_t[0];
}


CChromosome::~CChromosome() {
}


CChromosome CChromosome::operator=(const CChromosome& rhs){

m_par->G = rhs.m_par->G;
m_par = rhs.m_par;

m_dvx[1] = rhs.m_dvx[1];
m_dvx[2] = rhs.m_dvx[2];
m_dvx[0] = rhs.m_dvx[0];

m_dvy[1] = rhs.m_dvy[1];
m_dvy[2] = rhs.m_dvy[2];
m_dvy[0] = rhs.m_dvy[0];

m_dvz[1] = rhs.m_dvz[1];
m_dvz[2] = rhs.m_dvz[2];
m_dvz[0] = rhs.m_dvz[0];

m_t[1] = rhs.m_t[1];
m_t[2] = rhs.m_t[2];
m_t[0] = rhs.m_t[0];


return rhs;
}

void CChromosome::printChrom(ostream &output){
output << m_dvx[0] << " " << m_dvy[0] << " " << m_dvz[0] << " " << m_t[0] << "\n";
}

CChromosome CChromosome::operator*(CChromosome& rhs)
{
CChromosome newChrom(*this);
uniform_real_distribution<float> randOne(0,1);

for(unsigned short i = 0;i<newChrom.chromSize();i++){
unsigned seed = chrono::high_resolution_clock::now().time_since_epoch().count()+i+m_coreNum;
default_random_engine gen(seed);
uniform_real_distribution<float> dist((*newChrom[i])[1],(*newChrom[i])[2]);
float r = randOne(gen);
if (r < m_par->mutateChance){
//cout << "Mutating\n";
(*newChrom[i])[0] = dist(gen);
}
else if (r < (m_par->mutateChance+1.0)/2.0){
(*newChrom[i])[0] = (*(*this)[i])[0];
//cout << "Using lhs\n";
}
else {
(*newChrom[i])[0] = (*rhs[i])[0];
//cout << "Using rhs\n";
}
}
return newChrom;
}

vector<float> *CChromosome::operator[](int nIndex)
{
if (nIndex==0)return &m_dvx;
else if (nIndex==1)return &m_dvy;
else if (nIndex==2)return &m_dvz;
else if (nIndex==3)return &m_t;
else{
cout << "ERROR CChromosome::operator[] has invalid index " << nIndex << "\n";\
return NULL;
}

}


bool CChromosome::operator==(const CChromosome& rhs)
{
if(m_dvx[0]==rhs.m_dvx[0] && m_dvy[0]==rhs.m_dvy[0] && m_dvz[0]==rhs.m_dvz[0] && m_t[0]==rhs.m_t[0])
return true;
return false;
}

bool CChromosome::operator!=(const CChromosome& rhs)
{
if(m_dvx[0]!=rhs.m_dvx[0] || m_dvy[0]!=rhs.m_dvy[0] || m_dvz[0]!=rhs.m_dvz[0] || m_t[0]!=rhs.m_t[0])
return true;
return false;
}

bool CChromosome::operator<(const CChromosome& rhs){
return(this->m_t[0]<rhs.m_t[0]);
}
bool CChromosome::operator>(const CChromosome& rhs){
return(this->m_t[0]>rhs.m_t[0]);
}
bool CChromosome::operator>=(const CChromosome& rhs){
return(this->m_t[0]>=rhs.m_t[0]);
}
bool CChromosome::operator<=(const CChromosome& rhs){
return(this->m_t[0]<=rhs.m_t[0]);
}
56 changes: 56 additions & 0 deletions CChromosome.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* CChromosome.h
*
* Created on: Apr 24, 2015
* Author: Mark Wittekind and Drew Murray
*/

#ifndef CTRAIT_H_
#define CTRAIT_H_

#include "Parameters.cpp"
#include <random>
#include <chrono>

class CChromosome {
public:
CChromosome(Par* par, unsigned short coreNum);
CChromosome(const CChromosome &rhs);
CChromosome();
virtual ~CChromosome();
Par* m_par;

vector<float> m_dvx;
vector<float> m_dvy;
vector<float> m_dvz;
vector<float> m_t;
unsigned short m_coreNum;
int chromSize() const {
return 4;
}
void printChrom(ostream &output);

bool operator==(const CChromosome& rhs);
bool operator!=(const CChromosome& rhs);
CChromosome operator*(CChromosome& rhs);
bool operator<(const CChromosome& rhs);
bool operator>(const CChromosome& rhs);
bool operator>=(const CChromosome& rhs);
bool operator<=(const CChromosome& rhs);
CChromosome operator=(const CChromosome& rhs);
vector<float> *operator[](int nIndex);


// friend ostream &operator<<(ostream &output, CChromosome &chrom) {
// const int size = chrom.chromSize();
// cout << "ERROR: "
// for (unsigned short i = 0; i < size; i++) {
// //cout << chrom[i][0] << "\n";
// //output << chrom[i][0] << "\n";
// }
// output << "\n";
// return output;
// }
};

#endif /* CTRAIT_H_ */
43 changes: 43 additions & 0 deletions CCoordSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* CCoordSet.cpp
*
* Created on: Apr 8, 2015
* Author: Mark Wittekind and Drew Murray
*/

#include "CCoordSet.h"

CCoordSet::CCoordSet() {
CSVector zero;
m_position =zero;
m_velocity =zero;
m_acceleration =zero;

}

CCoordSet::CCoordSet(CSVector pos, CSVector vel, CSVector acc) {
m_position = pos;
m_velocity = vel;
m_acceleration = acc;
}

CCoordSet CCoordSet::operator=(const CCoordSet& rhs)
{
if(this == &rhs)
return *this;
m_position = rhs.m_position;
m_velocity = rhs.m_velocity;
m_acceleration = rhs.m_acceleration;
return *this;
}

CCoordSet::CCoordSet(const CCoordSet& other) {
m_position = other.m_position;
m_velocity = other.m_velocity;
m_acceleration = other.m_acceleration;

}

CCoordSet::~CCoordSet() {
}

31 changes: 31 additions & 0 deletions CCoordSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* CCoordSet.h
*
* Created on: Apr 8, 2015
* Author: Mark Wittekind and Drew Murray
*/

#ifndef CCOORDSET_H_
#define CCOORDSET_H_

#include "CSVector.h"

class CCoordSet {
public:
CCoordSet();
CCoordSet(const CCoordSet& other);
CCoordSet(CSVector pos, CSVector vel, CSVector acc);
CCoordSet operator=(const CCoordSet& rhs);
virtual ~CCoordSet();
CSVector m_position;
CSVector m_velocity;
CSVector m_acceleration;

friend ostream &operator<<( ostream &output, const CCoordSet &D ){
output << "Pos:" << D.m_position << "\nVel:" << D.m_velocity << "\nAcc:"
<< D.m_acceleration << "\n";
return output;
}
};

#endif /* CCOORDSET_H_ */
Loading