Use a genetic algorithm to evolve populations of bit strings.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
1.3 KiB

  1. //
  2. #pragma once
  3. //
  4. #include "BitEvolver/Includes.h"
  5. //
  6. #include <memory>
  7. #include <string>
  8. #include <sstream>
  9. #include <vector>
  10. #include <mutex>
  11. //
  12. namespace BitEvolver
  13. {
  14. //
  15. using std::string;
  16. //
  17. class Chromosome
  18. {
  19. //
  20. public:
  21. //
  22. Chromosome(std::shared_ptr<class Random> _random, int _bits);
  23. //
  24. void Reset();
  25. void Randomize();
  26. //
  27. void SetGenerationNumber(int g);
  28. void IncrementGenerationNumber();
  29. int GetGenerationNumber();
  30. //
  31. void SetBitCount(int count);
  32. int GetBitCount();
  33. //
  34. void FlipBit(int index);
  35. //
  36. bool GetBit(int index);
  37. void SetBit(int index, bool b);
  38. void SetBits(string s);
  39. //
  40. void ResetFitness();
  41. void SetFitness(double d);
  42. void AdjustFitness(double d);
  43. double GetFitness();
  44. /**
  45. Error is just inverted fitness
  46. */
  47. void ResetError();
  48. void SetError(double e);
  49. void AdjustError(double e);
  50. double GetError();
  51. //
  52. std::string ToString();
  53. //
  54. const Chromosome& operator=(const Chromosome& other);
  55. //
  56. private:
  57. // Random number generator
  58. std::shared_ptr<class Random> random;
  59. //
  60. int generation_number;
  61. //
  62. std::vector<bool> bits;
  63. int bits_count_desired;
  64. // Fitness
  65. double fitness;
  66. // Mutexes
  67. std::recursive_mutex modification_mutex;
  68. };
  69. };