aboutsummaryrefslogtreecommitdiff
path: root/plugins/CppnExplorer/include/CppnExplorer.hpp
blob: 5ca20c5d98e3252fef640c05b79fdddb57f125d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef __CPPN_EXPLORER_HPP__
#define __CPPN_EXPLORER_HPP__

#include <Hyperneat/Cppn.hpp>
#include <Hyperneat/Genome.hpp>
#include <Hyperneat/NeuralNet.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <HyperNeat/Utils/Atomic.hpp>
#include <HyperNeat/Utils/Thread.hpp>
#include <Hyperneat/NeuralNetPrms.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/RectangleShape.hpp>

namespace hyperneat
{
    class CppnExplorer
    {
    public:
        CppnExplorer();
        ~CppnExplorer();

        void run(const Genome& genome, const NeuralNetPrms* nnPrms = nullptr, sf::RenderTarget* target = nullptr);
        void shutdown();
        void draw();

        // void run(const Genome& genome, const NeuralNetPrms& nnPrms);
        // void shutdown();

    private:
        void windowHandler();
        void eventHandler();
        void neuralNetGenerator();
        void fieldGenerator();

        class Neuron
        {
        public:
            Neuron(const NeuralNet::Neuron& refNeuron);

            sf::VertexArray _synapses = sf::VertexArray(sf::Lines);
            sf::CircleShape _nucleus  = sf::CircleShape(4.0f * PIXEL_SIZE, 12);
        };

        Cppn           _cppn;
        Genome         _genome;
        NeuralNetPrms  _nnPrms;
        Vector<Neuron> _neurons;
        NeuralNet      _neuralNet;
        bool           _withNeuralNet  = false;
        Atomic<bool>   _neuralNetReady = {false};

        sf::RenderWindow  _window;
        Atomic<bool>      _windowOpen = {false};
        sf::RenderTarget* _target     = nullptr;
        const sf::Color   _uiColor    = {255, 255, 255, 8};

        sf::Image          _fieldImage;
        sf::RectangleShape _fieldSprite;
        sf::Texture        _fieldTexture;
        size_t             _currentField = 0;
        Atomic<bool>       _fieldReady   = {false};

        Thread _windowHandler;
        Thread _fieldGenerator;
        Thread _neuralNetGenerator;

        static constexpr unsigned RESOLUTION = 512;
        static constexpr float    PIXEL_SIZE = 1.0f / static_cast<float>(RESOLUTION);

        // class Neuron
        // {
        // public:
        //     enum class Type {
        //         INPUT,
        //         HIDDEN,
        //         OUTPUT
        //     };

        //     Type            _type;
        //     sf::CircleShape _body     = sf::CircleShape(0.0f, 30);
        //     sf::CircleShape _nucleus  = sf::CircleShape(0.0f, 30);
        //     sf::VertexArray _synapses = sf::VertexArray(sf::Lines);
        // };

        // void print(const sf::Color* field);
        // void placeCursorAt(const sf::Vector2f& newPos);
        // void createNeuron(Neuron& repNeuron, const NeuralNet::Neuron& srcNeuron);

        // Cppn          _cppn;
        // Genome        _genome;
        // NeuralNet     _neuralNet;
        // NeuralNetPrms _nnPrms;

        // const unsigned   RESOLUTION = 512;
        // sf::RenderWindow _window;
        // Atomic<bool>     _running;
        // Thread           _executor;

        // const sf::Color  WEIGHTS      = { 128,   0, 255 };
        // const sf::Color  INV_WEIGHTS  = { 255,   0, 128 };
        // const sf::Color  BIAS         = {   0, 255, 255 };
        // const sf::Color* _field       = &WEIGHTS;

        // sf::Image          _valueImage;
        // sf::Texture        _valueTexture;
        // sf::RectangleShape _valueSprite;
        // sf::Image          _biasImage;
        // sf::Texture        _biasTexture;
        // sf::RectangleShape _biasSprite;
        // bool               _showBias = false;

        // const sf::Color _uiColor = { 64, 64, 64 };
        // const float     PIXEL    = 1.0f / static_cast<float>(RESOLUTION);

        // sf::Vector2f       _cursorPos = { 0.0f, 0.0f };
        // sf::RectangleShape _cursorH   = sf::RectangleShape(sf::Vector2f(0.2f, PIXEL * 3.0f));
        // sf::RectangleShape _cursorV   = sf::RectangleShape(sf::Vector2f(PIXEL * 3.0f, 0.2f));
        // sf::VertexArray    _grid      = sf::VertexArray(sf::Lines);

        // bool           _showField   = true;
        // bool           _showNeurons = true;
        // bool           _showGrid    = true;
        // Vector<Neuron> _neurons;
    };
}

#endif