Skip to content

Commit

Permalink
Adding ChromosomeProcessor class, that can perform operations on Chro…
Browse files Browse the repository at this point in the history
…mosomes. Fixing definition of deltaNclusters.
  • Loading branch information
jniedzie committed Feb 13, 2019
1 parent 11ac099 commit 9b5c948
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 298 deletions.
3 changes: 2 additions & 1 deletion cppVersion/libChromosome/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_library(libChromosome src/Chromosome.cpp)
add_library(libChromosome src/Chromosome.cpp
src/ChromosomeProcessor.cpp)

set_property(TARGET libChromosome PROPERTY CXX_STANDARD 17)
105 changes: 43 additions & 62 deletions cppVersion/libChromosome/include/Chromosome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,68 @@

#include "GeneticHelpers.hpp"

/// Representation of Imaging Algo parameters that can be used for optimization with use
/// of a genetic algorithm. Allows to save and read double parameters in a vector of 64bit
/// unsigned integers.
class Chromosome
{
public:
enum ECrossover{
kUniform, ///< each bit has a chance to be exchaned between parents
kSinglePoint, ///< one chromosome gets crossed, the rest stays the same or is exchaned intact
kFixedSinglePoint,///< one chromosome gets crossed at point that doesn't modify any of the parameters, the rest stays the same or is exchaned intact
kMultiPoint, ///< each chromosome is crossed at a random point
kNcrossover
};

inline static std::string crossoverName[kNcrossover] = {
"Uniform",
"Single point",
"Fixed single point",
"Multi point"
};

public:
/// Default constructor
Chromosome();

/// Default destructor
~Chromosome();

static std::shared_ptr<Chromosome> GetRandom();
/// Prints basic information about this creature
void Print();

inline void SetParam(EParam par, double val){
params[par] = static_cast<uint16_t>(std::numeric_limits<uint16_t>::max()/(paramMax[par]-paramMin[par])*(val-paramMin[par]));
}
/// Sets value of parameter. It will NOT be automatically added to bitChromosome - user has to
/// call SaveToBitChromosome() manually.
/// \par Type of parameter
/// \val Value of the parameter
void SetParam(EParam par, double val);

/// Fixes value of parameter. It will NOT be automatically added to bitChromosome - user has to
/// call SaveToBitChromosome() manually.
/// \par Type of parameter
/// \val Value of the parameter
void FixParam(EParam par, double val);

inline void SetBitChromosome(int i, uint64_t bits){bitChromosome[i] = bits;}
/// Returns value of the parameter. This should be the only way of accessing parameters!
/// Don't try to access it directly, even within this class.
double GetParam(EParam par);

/// Saves all parameters in a vector of 64bit integers that will be used for crossover and mutation.
void SaveToBitChromosome();

/// Restores all parameters from a vector of 64bit integers used for crossover and mutation.
void ReadFromBitChromosome();

inline void SetScore(double val){score = val;}
// Trivial setters
inline void SetNormalizedScore(double val){normalizedScore = val;}
inline void SetExecutionTime(double val){executionTime = val;}
inline void SetMutationChance(double val){mutationChance = val;}
inline void SetSeverityFactor(double val){severityFactor = val;}
inline void SetCrossover(ECrossover val){crossover = val;}
inline void SetInputDataPath(std::string val){inputDataPath = val;}
inline void SetMinLayer(int val){minLayer = val;}
inline void SetMaxLayer(int val){maxLayer = val;}

// Getters
inline double GetParam(EParam par){
return paramMin[par] + (double)params[par]*(paramMax[par]-paramMin[par])/std::numeric_limits<uint16_t>::max();
}
inline uint64_t GetBitChromosome(int i){return bitChromosome[i];}

inline double GetScore(){return score;}
inline double GetNormalizedScore(){return normalizedScore;}
inline uint64_t GetUniqueID(){return uniqueID;}
inline std::string GetConfigPath(){return configPath;}
inline std::string GetClusteringOutputPath(){return clusteringOutputPath;}

void SaveToBitChromosome();
void ReadFromBitChromosome();

void StoreInConfig(std::string path="");
inline void SetClusteringOutput(ClusteringOutput val){clusteringOutput = val;}

// Trivial getters
inline double GetScore(){return score;}
inline double GetNormalizedScore(){return normalizedScore;}
inline uint64_t GetUniqueID(){return uniqueID;}
inline std::string GetConfigPath(){return configPath;}
inline std::string GetClusteringOutputPath(){return clusteringOutputPath;}

void Print();
void CalculateScore();

std::vector<std::shared_ptr<Chromosome>> ProduceChildWith(const std::shared_ptr<Chromosome> partner);
private:
std::vector<uint16_t> params;
std::vector<bool> isParamFixed;
std::vector<uint16_t> params; ///< Vector of chromosome parameters (see EParam)
std::vector<bool> isParamFixed; ///< Tells if a parameter should be fixed

std::vector<uint64_t> bitChromosome;
std::vector<uint64_t> bitChromosome;///< Bits representing all parameters (used for crossover
/// and mutation

// other variables not stored in bit chromosome
uint64_t uniqueID;
std::string configPath;
std::string clusteringOutputPath;
std::string inputDataPath;
int minLayer;
int maxLayer;
double mutationChance;
double severityFactor; // larger the value, more easily population members will die
ECrossover crossover;
uint64_t uniqueID; ///< Unique identifier of this creature
std::string configPath; ///< Path to file in which this creature will be stored
std::string clusteringOutputPath; ///< Path to file to which results of clustering with params
/// of this creature should be stored

ClusteringOutput clusteringOutput;
double executionTime;
Expand All @@ -102,9 +83,9 @@ class Chromosome
template<class T>
void SetValueFromChromosome(T &value, int &shift, int chromoIndex);

std::vector<uint64_t> SinglePointCrossover(uint64_t a, uint64_t b, bool fixed = false);

int BackToLimits();

friend class ChromosomeProcessor;
};

#endif /* Chromosome_hpp */
43 changes: 43 additions & 0 deletions cppVersion/libChromosome/include/ChromosomeProcessor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// ChromosomeProcessor.hpp
//
// Created by Jeremi Niedziela on 13/02/2019.
//

#ifndef ChromosomeProcessor_hpp
#define ChromosomeProcessor_hpp

#include "GeneticHelpers.hpp"
#include "Chromosome.hpp"

class ChromosomeProcessor {
public:
/// Default constructor
ChromosomeProcessor(double _mutationChance,
double _severityFactor,
ECrossover _crossover);

/// Default destructor
~ChromosomeProcessor();

std::shared_ptr<Chromosome> GetRandomChromosome();

void CalculateScore(std::shared_ptr<Chromosome> chromo);

std::pair<std::shared_ptr<Chromosome>, std::shared_ptr<Chromosome>>
CrossChromosomes(const std::shared_ptr<Chromosome> mom,
const std::shared_ptr<Chromosome> dad);

void StoreChromosomeInConfig(const std::shared_ptr<Chromosome> chromo,
std::string path="");

private:

std::pair<uint64_t,uint64_t> SinglePointCrossover(uint64_t a, uint64_t b, bool fixed=false);

double mutationChance;
double severityFactor; // larger the value, more easily population members will die
ECrossover crossover;
};

#endif /* ChromosomeProcessor_hpp */
35 changes: 25 additions & 10 deletions cppVersion/libChromosome/include/GeneticHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ enum EDet {
kBH ///< back hadronic endcap (plastic)
};

enum ECrossover{
kUniform, ///< each bit has a chance to be exchaned between parents
kSinglePoint, ///< one chromosome gets crossed, the rest stays the same or is exchaned intact
kFixedSinglePoint, ///< one chromosome gets crossed at point that doesn't modify any of the parameters, the rest stays the same or is exchaned intact
kMultiPoint, ///< each chromosome is crossed at a random point
kNcrossover
};

inline static std::string crossoverName[kNcrossover] = {
"Uniform",
"Single point",
"Fixed single point",
"Multi point"
};

inline double RandDouble(double min, double max)
{
return min + static_cast<double>(rand()) /( static_cast<double>(RAND_MAX/(max-min)));
Expand Down Expand Up @@ -186,17 +201,17 @@ struct ClusteringOutput {

/// Default constructor
ClusteringOutput(){
resolutionMean = 99999;
resolutionSigma = 99999;
separationMean = 99999;
separationSigma = 99999;
containmentMean = 99999;
containmentSigma = 99999;
deltaNclustersMean = 99999;
resolutionMean = 99999;
resolutionSigma = 99999;
separationMean = 99999;
separationSigma = 99999;
containmentMean = 99999;
containmentSigma = 99999;
deltaNclustersMean = 99999;
deltaNclustersSigma = 99999;
nRecoFailed = 99999;
nCantMatchRecSim = 99999;
nFakeRec = 99999;
nRecoFailed = 99999;
nCantMatchRecSim = 99999;
nFakeRec = 99999;
}

/// Prints clustering results
Expand Down
Loading

0 comments on commit 9b5c948

Please sign in to comment.