Compare commits
No commits in common. "9f6771d90a61ac65b3f0f1a539f537ad70f896ad" and "e50acd9cc5b19d27d73d710f6780b20433f5fa86" have entirely different histories.
9f6771d90a
...
e50acd9cc5
@ -1,6 +1,6 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include "canvas/Canvas.hpp"
|
#include "canvas/Canvas.hpp"
|
||||||
#include "DnaStore.hpp"
|
#include "values/DnaManager.hpp"
|
||||||
class App
|
class App
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ private:
|
|||||||
|
|
||||||
std::array<UiUnit, 2> unit = {0};
|
std::array<UiUnit, 2> unit = {0};
|
||||||
|
|
||||||
DnaManagerData manager;
|
DnaManager manager;
|
||||||
|
|
||||||
Rectangle likedTextBox;
|
Rectangle likedTextBox;
|
||||||
Rectangle genTextBox;
|
Rectangle genTextBox;
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
#include "values/DnaManager.hpp"
|
|
||||||
|
|
||||||
namespace DnaStore
|
|
||||||
{
|
|
||||||
void load(DnaManagerData *data);
|
|
||||||
void saveData(DnaManagerData *data);
|
|
||||||
void saveVec(DnaManagerData *data);
|
|
||||||
void saveGen(DnaManagerData *data);
|
|
||||||
} // namespace DnaStore
|
|
5
inc/Math.hpp
Normal file
5
inc/Math.hpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include <raylib.h>
|
||||||
|
|
||||||
|
Color ColorAdd(Color c1, Color c2);
|
||||||
|
Color ColorAddValue(Color c, int add);
|
||||||
|
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom);
|
@ -28,9 +28,19 @@ struct NetUnit
|
|||||||
|
|
||||||
static_assert(24 == sizeof(NetUnit));
|
static_assert(24 == sizeof(NetUnit));
|
||||||
|
|
||||||
struct DnaManagerData
|
class DnaManager
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
void init();
|
||||||
|
void deinit();
|
||||||
|
UiUnit next();
|
||||||
|
void like(UiUnit unit);
|
||||||
int generation;
|
int generation;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void saveData();
|
||||||
|
void saveVec();
|
||||||
|
void saveGen();
|
||||||
uint128 randSeed;
|
uint128 randSeed;
|
||||||
uint128 id;
|
uint128 id;
|
||||||
int queued;
|
int queued;
|
||||||
@ -38,11 +48,6 @@ struct DnaManagerData
|
|||||||
std::vector<Dna> vector;
|
std::vector<Dna> vector;
|
||||||
std::vector<int> liked;
|
std::vector<int> liked;
|
||||||
std::vector<int> disliked;
|
std::vector<int> disliked;
|
||||||
};
|
|
||||||
|
|
||||||
namespace DnaManager
|
void newGen();
|
||||||
{
|
|
||||||
UiUnit next(DnaManagerData *data);
|
|
||||||
bool like(UiUnit unit, DnaManagerData *data);
|
|
||||||
void newGen(DnaManagerData *data);
|
|
||||||
};
|
};
|
||||||
|
54
src/App.cpp
54
src/App.cpp
@ -2,6 +2,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include "App.hpp"
|
#include "App.hpp"
|
||||||
|
#include "Math.hpp"
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
@ -9,44 +10,6 @@
|
|||||||
#define TOP (1 - pos)
|
#define TOP (1 - pos)
|
||||||
#define BOTTOM pos
|
#define BOTTOM pos
|
||||||
|
|
||||||
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom)
|
|
||||||
{
|
|
||||||
float br = box.width / box.height;
|
|
||||||
float tr = textW / textH;
|
|
||||||
|
|
||||||
Rectangle ret = {0, 0, 0, 0};
|
|
||||||
|
|
||||||
float hm = box.height * margin;
|
|
||||||
float wm = box.width * margin;
|
|
||||||
|
|
||||||
ret.height = box.height - hm;
|
|
||||||
ret.width = box.width - wm;
|
|
||||||
|
|
||||||
if (br < tr)
|
|
||||||
{
|
|
||||||
// bolj kvadrat izracunaj visino iz sirine
|
|
||||||
ret.height = ret.width / tr;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// bolj podolgovat izracunaj sirino iz visine
|
|
||||||
ret.width = ret.height * tr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (topOrBottom)
|
|
||||||
{
|
|
||||||
ret.y = box.y + hm;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ret.y = box.y + box.height - hm - ret.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret.x = box.x + wm;
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dimentions for font size 20
|
// Dimentions for font size 20
|
||||||
// DISLIKE 83
|
// DISLIKE 83
|
||||||
// LIKE 46
|
// LIKE 46
|
||||||
@ -67,7 +30,7 @@ void App::init(int screenWidth, int screenHeight)
|
|||||||
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
|
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
DnaStore::load(&manager);
|
manager.init();
|
||||||
upTex(Liked::tbd);
|
upTex(Liked::tbd);
|
||||||
while (!canvas.tick(canvasTexure[TOP]))
|
while (!canvas.tick(canvasTexure[TOP]))
|
||||||
{
|
{
|
||||||
@ -108,17 +71,9 @@ void App::upTex(Liked liked)
|
|||||||
if (liked != Liked::tbd)
|
if (liked != Liked::tbd)
|
||||||
{
|
{
|
||||||
unit[TOP].liked = liked;
|
unit[TOP].liked = liked;
|
||||||
bool ng = DnaManager::like(unit[TOP], &manager);
|
manager.like(unit[TOP]);
|
||||||
|
|
||||||
if (ng)
|
|
||||||
{
|
|
||||||
DnaStore::saveGen(&manager);
|
|
||||||
DnaManager::newGen(&manager);
|
|
||||||
DnaStore::saveVec(&manager);
|
|
||||||
}
|
|
||||||
DnaStore::saveData(&manager);
|
|
||||||
}
|
}
|
||||||
unit[TOP] = DnaManager::next(&manager);
|
unit[TOP] = manager.next();
|
||||||
if (unit[TOP].dna != nullptr)
|
if (unit[TOP].dna != nullptr)
|
||||||
{
|
{
|
||||||
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
|
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
|
||||||
@ -220,4 +175,5 @@ void App::deinit()
|
|||||||
UnloadRenderTexture(canvasTexure[i]);
|
UnloadRenderTexture(canvasTexure[i]);
|
||||||
}
|
}
|
||||||
canvas.deinit();
|
canvas.deinit();
|
||||||
|
manager.deinit();
|
||||||
}
|
}
|
140
src/DnaStore.cpp
140
src/DnaStore.cpp
@ -1,140 +0,0 @@
|
|||||||
#include <cstdio>
|
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
#include "sys.hpp"
|
|
||||||
#include "DnaStore.hpp"
|
|
||||||
|
|
||||||
#include <raylib.h>
|
|
||||||
|
|
||||||
#define ID_FILE_NAME "ID.bin"
|
|
||||||
#define DATA_FILE_NAME "DATA.bin"
|
|
||||||
#define VECTOR_FILE_NAME "VECTOR.bin"
|
|
||||||
|
|
||||||
void DnaStore::load(DnaManagerData *data)
|
|
||||||
{
|
|
||||||
if (sys::fileExists(ID_FILE_NAME))
|
|
||||||
{
|
|
||||||
sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(uint128));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data->id = mrand::getState(time(nullptr));
|
|
||||||
sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(uint128));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sys::fileExists(DATA_FILE_NAME))
|
|
||||||
{
|
|
||||||
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
|
|
||||||
|
|
||||||
FILE *file = fopen(filename, "rb");
|
|
||||||
if (file == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
fread(&data->randSeed, 1, sizeof(uint128), file);
|
|
||||||
fread(&data->generation, 1, sizeof(int), file);
|
|
||||||
fread(&data->showed, 1, sizeof(int), file);
|
|
||||||
data->queued = data->showed;
|
|
||||||
int size, tmp;
|
|
||||||
|
|
||||||
fread(&size, sizeof(size), 1, file);
|
|
||||||
for (int i = 0; i < size; i++)
|
|
||||||
{
|
|
||||||
fread(&tmp, sizeof(tmp), 1, file);
|
|
||||||
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
|
|
||||||
continue;
|
|
||||||
data->liked.push_back(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
fread(&size, sizeof(size), 1, file);
|
|
||||||
for (int i = 0; i < size; i++)
|
|
||||||
{
|
|
||||||
fread(&tmp, sizeof(tmp), 1, file);
|
|
||||||
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
|
|
||||||
continue;
|
|
||||||
data->disliked.push_back(tmp);
|
|
||||||
}
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data->randSeed = data->id;
|
|
||||||
data->queued = 0;
|
|
||||||
data->showed = 0;
|
|
||||||
data->generation = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
data->vector.resize(NUM_PER_GEN);
|
|
||||||
if (sys::fileExists(VECTOR_FILE_NAME))
|
|
||||||
{
|
|
||||||
sys::loadDataFromFile(VECTOR_FILE_NAME, data->vector.data(), sizeof(Dna) * NUM_PER_GEN);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
|
||||||
{
|
|
||||||
DNA::newDna(&data->vector[i], &data->randSeed);
|
|
||||||
}
|
|
||||||
saveVec(data); // v primeru da prvic ne gres cez celo generacijo se vec ne shrani in bo drugacen ker se bo regeneriral z drugim rand seed
|
|
||||||
saveData(data); // save ce rand seed se je spremenil in v premeru user ugasne brez da like se rand seed ne shrani in se resetira v ID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DnaStore::saveData(DnaManagerData *data)
|
|
||||||
{
|
|
||||||
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
|
|
||||||
|
|
||||||
FILE *file = fopen(filename, "wb");
|
|
||||||
if (file == NULL)
|
|
||||||
return;
|
|
||||||
fwrite(&data->randSeed, 1, sizeof(uint128), file);
|
|
||||||
fwrite(&data->generation, 1, sizeof(int), file);
|
|
||||||
fwrite(&data->showed, 1, sizeof(int), file);
|
|
||||||
|
|
||||||
int tmp;
|
|
||||||
tmp = data->liked.size();
|
|
||||||
fwrite(&tmp, sizeof(tmp), 1, file);
|
|
||||||
fwrite(data->liked.data(), sizeof(int) * tmp, 1, file);
|
|
||||||
|
|
||||||
tmp = data->disliked.size();
|
|
||||||
|
|
||||||
fwrite(&tmp, sizeof(tmp), 1, file);
|
|
||||||
fwrite(data->disliked.data(), sizeof(tmp) * tmp, 1, file);
|
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DnaStore::saveVec(DnaManagerData *data)
|
|
||||||
{
|
|
||||||
const char *filename = sys::transformFilePath(VECTOR_FILE_NAME);
|
|
||||||
FILE *file = fopen(filename, "wb");
|
|
||||||
if (file == NULL)
|
|
||||||
return;
|
|
||||||
fwrite(data->vector.data(), sizeof(Dna) * NUM_PER_GEN, 1, file);
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DnaStore::saveGen(DnaManagerData *data)
|
|
||||||
{
|
|
||||||
std::vector<NetUnit> gen;
|
|
||||||
gen.resize(NUM_PER_GEN);
|
|
||||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
|
||||||
{
|
|
||||||
gen[i].hash = ComputeCRC32((unsigned char *)&data->vector[i], sizeof(Dna));
|
|
||||||
gen[i].liked = Liked::tbd;
|
|
||||||
gen[i].index = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &&i : data->liked)
|
|
||||||
{
|
|
||||||
gen[i].liked = Liked::yes;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &&i : data->disliked)
|
|
||||||
{
|
|
||||||
gen[i].liked = Liked::no;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *fileName = TextFormat("gen%04d.bin", data->generation);
|
|
||||||
|
|
||||||
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
|
|
||||||
}
|
|
64
src/Math.cpp
Normal file
64
src/Math.cpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <cmath>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "Math.hpp"
|
||||||
|
#include "values/mrand.hpp"
|
||||||
|
|
||||||
|
#include <raymath.h>
|
||||||
|
|
||||||
|
Color ColorAddValue(Color c, int add)
|
||||||
|
{
|
||||||
|
int r = std::clamp(c.r + add, 0, 255);
|
||||||
|
int g = std::clamp(c.g + add, 0, 255);
|
||||||
|
int b = std::clamp(c.b + add, 0, 255);
|
||||||
|
|
||||||
|
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, c.a};
|
||||||
|
}
|
||||||
|
|
||||||
|
Color ColorAdd(Color c1, Color c2)
|
||||||
|
{
|
||||||
|
int r = std::clamp(c1.r + c2.r, 0, 255);
|
||||||
|
int g = std::clamp(c1.g + c2.g, 0, 255);
|
||||||
|
int b = std::clamp(c1.b + c2.b, 0, 255);
|
||||||
|
int a = std::clamp(c1.a + c2.a, 0, 255);
|
||||||
|
|
||||||
|
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom)
|
||||||
|
{
|
||||||
|
float br = box.width / box.height;
|
||||||
|
float tr = textW / textH;
|
||||||
|
|
||||||
|
Rectangle ret = {0, 0, 0, 0};
|
||||||
|
|
||||||
|
float hm = box.height * margin;
|
||||||
|
float wm = box.width * margin;
|
||||||
|
|
||||||
|
ret.height = box.height - hm;
|
||||||
|
ret.width = box.width - wm;
|
||||||
|
|
||||||
|
if (br < tr)
|
||||||
|
{
|
||||||
|
// bolj kvadrat izracunaj visino iz sirine
|
||||||
|
ret.height = ret.width / tr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// bolj podolgovat izracunaj sirino iz visine
|
||||||
|
ret.width = ret.height * tr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topOrBottom)
|
||||||
|
{
|
||||||
|
ret.y = box.y + hm;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret.y = box.y + box.height - hm - ret.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.x = box.x + wm;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
@ -3,7 +3,8 @@
|
|||||||
#include "canvas/BackGround.hpp"
|
#include "canvas/BackGround.hpp"
|
||||||
#include "canvas/BackGroundColors.hpp"
|
#include "canvas/BackGroundColors.hpp"
|
||||||
#include "canvas/Circle.hpp"
|
#include "canvas/Circle.hpp"
|
||||||
#include "canvas/stb_perlin.h"
|
#include "Math.hpp"
|
||||||
|
#include "stb_perlin.h"
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <rlgl.h>
|
#include <rlgl.h>
|
||||||
@ -27,25 +28,6 @@ constexpr static float mounten2max = 0.90;
|
|||||||
constexpr static float mounten3min = 0.90f;
|
constexpr static float mounten3min = 0.90f;
|
||||||
constexpr static float mounten3max = 0.95f;
|
constexpr static float mounten3max = 0.95f;
|
||||||
|
|
||||||
Color ColorAddValue(Color c, int add)
|
|
||||||
{
|
|
||||||
int r = std::clamp(c.r + add, 0, 255);
|
|
||||||
int g = std::clamp(c.g + add, 0, 255);
|
|
||||||
int b = std::clamp(c.b + add, 0, 255);
|
|
||||||
|
|
||||||
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, c.a};
|
|
||||||
}
|
|
||||||
|
|
||||||
Color ColorAdd(Color c1, Color c2)
|
|
||||||
{
|
|
||||||
int r = std::clamp(c1.r + c2.r, 0, 255);
|
|
||||||
int g = std::clamp(c1.g + c2.g, 0, 255);
|
|
||||||
int b = std::clamp(c1.b + c2.b, 0, 255);
|
|
||||||
int a = std::clamp(c1.a + c2.a, 0, 255);
|
|
||||||
|
|
||||||
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Public
|
// Public
|
||||||
void BackGround::init(int canvasSize)
|
void BackGround::init(int canvasSize)
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#include "canvas/Circle.hpp"
|
#include "canvas/Circle.hpp"
|
||||||
#include "sunShader.hpp"
|
#include "sunShader.hpp"
|
||||||
|
|
||||||
|
#include "Math.hpp"
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <rlgl.h>
|
#include <rlgl.h>
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "canvas/Tree.hpp"
|
#include "canvas/Tree.hpp"
|
||||||
#include "canvas/Circle.hpp"
|
#include "canvas/Circle.hpp"
|
||||||
|
#include "Math.hpp"
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "values/Dna.hpp"
|
#include "values/Dna.hpp"
|
||||||
|
|
||||||
|
#include <raymath.h>
|
||||||
|
|
||||||
namespace DNA
|
namespace DNA
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,112 +1,257 @@
|
|||||||
|
#include <ctime>
|
||||||
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "values/DnaManager.hpp"
|
#include "values/DnaManager.hpp"
|
||||||
|
#include "sys.hpp"
|
||||||
|
|
||||||
UiUnit DnaManager::next(DnaManagerData *data)
|
#include <raylib.h>
|
||||||
|
|
||||||
|
#define ID_FILE_NAME "ID.bin"
|
||||||
|
#define DATA_FILE_NAME "DATA.bin"
|
||||||
|
#define VECTOR_FILE_NAME "VECTOR.bin"
|
||||||
|
|
||||||
|
void DnaManager::init()
|
||||||
{
|
{
|
||||||
if (data->queued >= NUM_PER_GEN)
|
if (sys::fileExists(ID_FILE_NAME))
|
||||||
|
{
|
||||||
|
sys::loadDataFromFile(ID_FILE_NAME, &id, sizeof(uint128));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
id = mrand::getState(time(nullptr));
|
||||||
|
sys::saveDataToFile(ID_FILE_NAME, &id, sizeof(uint128));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sys::fileExists(DATA_FILE_NAME))
|
||||||
|
{
|
||||||
|
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
|
||||||
|
|
||||||
|
FILE *file = fopen(filename, "rb");
|
||||||
|
if (file == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
fread(&randSeed, 1, sizeof(uint128), file);
|
||||||
|
fread(&generation, 1, sizeof(int), file);
|
||||||
|
fread(&showed, 1, sizeof(showed), file);
|
||||||
|
queued = showed;
|
||||||
|
int size, tmp;
|
||||||
|
|
||||||
|
fread(&size, sizeof(size), 1, file);
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
fread(&tmp, sizeof(tmp), 1, file);
|
||||||
|
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
|
||||||
|
continue;
|
||||||
|
liked.push_back(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
fread(&size, sizeof(size), 1, file);
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
fread(&tmp, sizeof(tmp), 1, file);
|
||||||
|
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
|
||||||
|
continue;
|
||||||
|
disliked.push_back(tmp);
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
randSeed = id;
|
||||||
|
queued = 0;
|
||||||
|
showed = 0;
|
||||||
|
generation = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector.resize(NUM_PER_GEN);
|
||||||
|
if (sys::fileExists(VECTOR_FILE_NAME))
|
||||||
|
{
|
||||||
|
sys::loadDataFromFile(VECTOR_FILE_NAME, vector.data(), sizeof(Dna) * NUM_PER_GEN);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||||
|
{
|
||||||
|
DNA::newDna(&vector[i], &randSeed);
|
||||||
|
}
|
||||||
|
saveVec();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DnaManager::deinit()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void DnaManager::saveData()
|
||||||
|
{
|
||||||
|
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
|
||||||
|
|
||||||
|
FILE *file = fopen(filename, "wb");
|
||||||
|
if (file == NULL)
|
||||||
|
return;
|
||||||
|
fwrite(&randSeed, 1, sizeof(uint128), file);
|
||||||
|
fwrite(&generation, 1, sizeof(int), file);
|
||||||
|
fwrite(&showed, 1, sizeof(showed), file);
|
||||||
|
|
||||||
|
int tmp;
|
||||||
|
tmp = liked.size();
|
||||||
|
fwrite(&tmp, sizeof(tmp), 1, file);
|
||||||
|
fwrite(liked.data(), sizeof(int) * tmp, 1, file);
|
||||||
|
|
||||||
|
tmp = disliked.size();
|
||||||
|
|
||||||
|
fwrite(&tmp, sizeof(tmp), 1, file);
|
||||||
|
fwrite(disliked.data(), sizeof(tmp) * tmp, 1, file);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DnaManager::saveVec()
|
||||||
|
{
|
||||||
|
const char *filename = sys::transformFilePath(VECTOR_FILE_NAME);
|
||||||
|
FILE *file = fopen(filename, "wb");
|
||||||
|
if (file == NULL)
|
||||||
|
return;
|
||||||
|
fwrite(vector.data(), sizeof(Dna) * NUM_PER_GEN, 1, file);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
UiUnit DnaManager::next()
|
||||||
|
{
|
||||||
|
if (queued >= NUM_PER_GEN)
|
||||||
{
|
{
|
||||||
return {nullptr, Liked::tbd, -1};
|
return {nullptr, Liked::tbd, -1};
|
||||||
}
|
}
|
||||||
|
|
||||||
Dna *ret = &data->vector[data->queued];
|
Dna *ret = &vector[queued];
|
||||||
int index = data->queued++;
|
int index = queued++;
|
||||||
return {ret, Liked::tbd, index};
|
return {ret, Liked::tbd, index};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DnaManager::like(UiUnit unit, DnaManagerData *data)
|
void DnaManager::like(UiUnit unit)
|
||||||
{
|
{
|
||||||
int found = -1;
|
int found = -1;
|
||||||
if (unit.index == data->showed)
|
if (unit.index == showed)
|
||||||
{
|
{
|
||||||
found = data->showed;
|
found = showed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found == -1)
|
if (found == -1)
|
||||||
{
|
{
|
||||||
// RUN OUT OF GEN WAITING FOR NEW GEN
|
// RUN OUT OF GEN WAITING FOR NEW GEN
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
if (unit.liked == Liked::yes)
|
if (unit.liked == Liked::yes)
|
||||||
{
|
{
|
||||||
data->liked.push_back(found);
|
liked.push_back(found);
|
||||||
}
|
}
|
||||||
else if (unit.liked == Liked::no)
|
else if (unit.liked == Liked::no)
|
||||||
{
|
{
|
||||||
data->disliked.push_back(found);
|
disliked.push_back(found);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// could be infinite loop if something went wrong and user lost UiUnit and next is returning null thinking that it queued everiting
|
TraceLog(LOG_ERROR, "LIKED WITH NO VAULE");
|
||||||
// maybe return true to move on and maybe tread Liked::tbd as Liked::no
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
data->showed++;
|
|
||||||
|
|
||||||
if (data->showed >= NUM_PER_GEN && data->queued >= NUM_PER_GEN) // if buffer was biger in the past showed could be more then NUM_PER_GEN so its changed to >= insted of ==
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DnaManager::newGen(DnaManagerData *data)
|
|
||||||
{
|
|
||||||
data->queued = 0;
|
|
||||||
data->showed = 0;
|
|
||||||
data->generation += 1;
|
|
||||||
if (data->liked.size() == 0)
|
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
|
||||||
{
|
|
||||||
DNA::newDna(&data->vector[i], &data->randSeed);
|
|
||||||
}
|
|
||||||
data->disliked.clear();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->liked.size() == 1)
|
showed++;
|
||||||
|
|
||||||
|
if (showed >= NUM_PER_GEN && queued >= NUM_PER_GEN) // if buffer was biger in the past showed could be more then NUM_PER_GEN so its changed to >= insted of ==
|
||||||
{
|
{
|
||||||
int front = data->liked.front();
|
saveGen();
|
||||||
|
newGen();
|
||||||
|
queued = 0;
|
||||||
|
showed = 0;
|
||||||
|
generation += 1;
|
||||||
|
saveVec();
|
||||||
|
}
|
||||||
|
saveData();
|
||||||
|
}
|
||||||
|
|
||||||
for (auto &&i : data->disliked)
|
void DnaManager::saveGen()
|
||||||
{
|
{
|
||||||
DNA::clone(&data->vector[front], &data->vector[i], &data->randSeed);
|
std::vector<NetUnit> gen;
|
||||||
}
|
gen.resize(NUM_PER_GEN);
|
||||||
|
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||||
|
{
|
||||||
|
unsigned int *hash = ComputeMD5((unsigned char *)&vector[i], sizeof(Dna));
|
||||||
|
gen[i].hash.a = hash[0];
|
||||||
|
gen[i].hash.b = hash[1];
|
||||||
|
gen[i].hash.c = hash[2];
|
||||||
|
gen[i].hash.d = hash[3];
|
||||||
|
|
||||||
data->disliked.clear();
|
gen[i].liked = Liked::tbd;
|
||||||
data->liked.clear();
|
gen[i].index = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->liked.size() >= 2)
|
for (auto &&i : liked)
|
||||||
{
|
{
|
||||||
for (auto &&i : data->disliked)
|
gen[i].liked = Liked::yes;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &&i : disliked)
|
||||||
|
{
|
||||||
|
gen[i].liked = Liked::no;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *fileName = TextFormat("gen%04d.bin", generation);
|
||||||
|
|
||||||
|
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DnaManager::newGen()
|
||||||
|
{
|
||||||
|
if (liked.size() == 0)
|
||||||
|
{
|
||||||
|
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||||
|
{
|
||||||
|
DNA::newDna(&vector[i], &randSeed);
|
||||||
|
}
|
||||||
|
disliked.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (liked.size() == 1)
|
||||||
|
{
|
||||||
|
int front = liked.front();
|
||||||
|
|
||||||
|
for (auto &&i : disliked)
|
||||||
|
{
|
||||||
|
DNA::clone(&vector[front], &vector[i], &randSeed);
|
||||||
|
}
|
||||||
|
|
||||||
|
disliked.clear();
|
||||||
|
liked.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (liked.size() >= 2)
|
||||||
|
{
|
||||||
|
for (auto &&i : disliked)
|
||||||
{
|
{
|
||||||
|
|
||||||
size_t p1 = mrand::getValue(0, data->liked.size() - 1, &data->randSeed);
|
size_t p1 = mrand::getValue(0, liked.size() - 1, &randSeed);
|
||||||
size_t p2 = mrand::getValue(0, data->liked.size() - 1, &data->randSeed);
|
size_t p2 = mrand::getValue(0, liked.size() - 1, &randSeed);
|
||||||
while (p1 == p2)
|
while (p1 == p2)
|
||||||
{
|
{
|
||||||
p2 = mrand::getValue(0, data->liked.size(), &data->randSeed);
|
p2 = mrand::getValue(0, liked.size(), &randSeed);
|
||||||
}
|
}
|
||||||
|
|
||||||
p1 = data->liked[p1];
|
p1 = liked[p1];
|
||||||
p2 = data->liked[p2];
|
p2 = liked[p2];
|
||||||
Dna *p1p = &data->vector[p1];
|
Dna *p1p = &vector[p1];
|
||||||
Dna *p2p = &data->vector[p2];
|
Dna *p2p = &vector[p2];
|
||||||
Dna *c = &data->vector[i];
|
Dna *c = &vector[i];
|
||||||
|
|
||||||
DNA::makeChild(p1p, p2p, c, &data->randSeed);
|
DNA::makeChild(p1p, p2p, c, &randSeed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < NUM_PER_GEN; i++)
|
for (size_t i = 0; i < NUM_PER_GEN; i++)
|
||||||
{
|
{
|
||||||
DNA::mutate(&data->vector[i], NUM_OF_MUT, &data->randSeed);
|
DNA::mutate(&vector[i], NUM_OF_MUT, &randSeed);
|
||||||
}
|
}
|
||||||
data->disliked.clear();
|
disliked.clear();
|
||||||
data->liked.clear();
|
liked.clear();
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
#include <cinttypes>
|
#include <inttypes.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "values/mrand.hpp"
|
#include "values/mrand.hpp"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user