Compare commits
5 Commits
main
...
060b9f9182
| Author | SHA1 | Date | |
|---|---|---|---|
| 060b9f9182 | |||
| 59fd47e684 | |||
| 593b813988 | |||
| 5f6305a2f2 | |||
| dddf8ca632 |
@@ -8,11 +8,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
|
|||||||
# set(CMAKE_VERBOSE_MAKEFILE ON)
|
# set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||||
add_compile_options(-ggdb)
|
add_compile_options(-ggdb)
|
||||||
|
|
||||||
include_directories(
|
include_directories(external/include/)
|
||||||
external/include
|
|
||||||
shared/inc
|
|
||||||
)
|
|
||||||
|
|
||||||
link_libraries(
|
link_libraries(
|
||||||
${CMAKE_SOURCE_DIR}/external/libimgui.a
|
${CMAKE_SOURCE_DIR}/external/libimgui.a
|
||||||
${CMAKE_SOURCE_DIR}/external/libraylib.a
|
${CMAKE_SOURCE_DIR}/external/libraylib.a
|
||||||
@@ -22,7 +18,11 @@ link_libraries(
|
|||||||
m
|
m
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(shared STATIC
|
add_executable(app
|
||||||
|
app/src/main.cpp
|
||||||
|
app/src/App.cpp
|
||||||
|
app/src/DnaStore.cpp
|
||||||
|
app/src/sys.cpp
|
||||||
shared/src/canvas/BackGround.cpp
|
shared/src/canvas/BackGround.cpp
|
||||||
shared/src/canvas/BackGroundColors.cpp
|
shared/src/canvas/BackGroundColors.cpp
|
||||||
shared/src/canvas/Canvas.cpp
|
shared/src/canvas/Canvas.cpp
|
||||||
@@ -32,33 +32,36 @@ add_library(shared STATIC
|
|||||||
shared/src/values/mrand.cpp
|
shared/src/values/mrand.cpp
|
||||||
shared/src/values/Similarity.cpp
|
shared/src/values/Similarity.cpp
|
||||||
shared/src/TcpSocket.cpp
|
shared/src/TcpSocket.cpp
|
||||||
shared/src/sql.cpp
|
|
||||||
)
|
)
|
||||||
|
# Add include directories
|
||||||
add_executable(app
|
target_include_directories(app PRIVATE app/inc shared/inc )
|
||||||
app/src/main.cpp
|
|
||||||
app/src/App.cpp
|
|
||||||
app/src/DnaStore.cpp
|
|
||||||
app/src/sys.cpp
|
|
||||||
)
|
|
||||||
target_include_directories(app PRIVATE app/inc)
|
|
||||||
target_link_libraries(app PRIVATE shared)
|
|
||||||
|
|
||||||
add_executable(server
|
add_executable(server
|
||||||
server/src/server.cpp
|
server/src/server.cpp
|
||||||
server/src/checker.cpp
|
server/src/checker.cpp
|
||||||
|
|
||||||
|
shared/src/sql.cpp
|
||||||
|
shared/src/TcpSocket.cpp
|
||||||
|
shared/src/values/Dna.cpp
|
||||||
|
shared/src/values/DnaManager.cpp
|
||||||
|
shared/src/values/mrand.cpp
|
||||||
)
|
)
|
||||||
target_include_directories(server PRIVATE server/inc)
|
# Add include directories
|
||||||
target_link_libraries(server PRIVATE shared)
|
target_include_directories(server PRIVATE server/inc shared/inc)
|
||||||
|
|
||||||
add_executable(view
|
add_executable(view
|
||||||
view/src/main.cpp
|
view/src/main.cpp
|
||||||
view/src/Vapp.cpp
|
view/src/Vapp.cpp
|
||||||
)
|
shared/src/canvas/BackGround.cpp
|
||||||
target_include_directories(view PRIVATE view/inc)
|
shared/src/canvas/BackGroundColors.cpp
|
||||||
target_link_libraries(view PRIVATE shared)
|
shared/src/canvas/Canvas.cpp
|
||||||
|
shared/src/canvas/Tree.cpp
|
||||||
|
shared/src/values/Dna.cpp
|
||||||
|
shared/src/values/DnaManager.cpp
|
||||||
|
shared/src/values/mrand.cpp
|
||||||
|
shared/src/values/Similarity.cpp
|
||||||
|
|
||||||
add_executable(slike
|
shared/src/sql.cpp
|
||||||
random/slike.cpp
|
|
||||||
)
|
)
|
||||||
target_link_libraries(slike PRIVATE shared)
|
# Add include directories
|
||||||
|
target_include_directories(view PRIVATE view/inc shared/inc)
|
||||||
104
Readme.md
104
Readme.md
@@ -1,90 +1,26 @@
|
|||||||
# Treender
|
|
||||||
|
|
||||||
A genetic algorithm visualization tool with interactive evolution simulation.
|
# Code style
|
||||||
|
|
||||||
## Overview
|
## Naming Conventions:
|
||||||
|
- PascalCase:
|
||||||
|
- Functions
|
||||||
|
- Class names
|
||||||
|
- Struct names
|
||||||
|
|
||||||
Treender is a C++ application that simulates genetic algorithms through an interactive visual interface. Users can observe and influence the evolution of digital organisms represented as visual patterns.
|
- camelCase:
|
||||||
|
- Class attributes
|
||||||
|
- Class methods
|
||||||
|
- Struct attributes
|
||||||
|
- Variables
|
||||||
|
|
||||||
## Features
|
## Class Structure:
|
||||||
|
|
||||||
- **Interactive Evolution**: Like/dislike organisms to guide evolution
|
- Header (.h) Files:
|
||||||
- **Genetic Algorithm**: DNA-based reproduction with mutation
|
- Declare public methods and attributes first, followed by private members.
|
||||||
- **Visual Representation**: Organisms displayed as colorful patterns
|
- Implementation (.cpp) Files:
|
||||||
- **Client-Server Architecture**: Separate app and server components
|
- Implement methods in the same order as declared in the corresponding header file.
|
||||||
- **Cross-Platform**: Supports Web, Android, and Desktop platforms
|
|
||||||
|
|
||||||
## Architecture
|
## Include order
|
||||||
|
- Std
|
||||||
```
|
- local ( form inc dir )
|
||||||
Treender
|
- external raylib
|
||||||
├── app/ # Main application (Raylib + Dear ImGui)
|
|
||||||
├── server/ # TCP server for data management
|
|
||||||
├── shared/ # Shared code (DNA logic, networking)
|
|
||||||
├── external/ # Third-party libraries
|
|
||||||
└── data.db # SQLite database
|
|
||||||
```
|
|
||||||
|
|
||||||
## Components
|
|
||||||
|
|
||||||
### Application
|
|
||||||
- **Main Entry**: `app/src/main.cpp`
|
|
||||||
- **Core Class**: `App` class handles rendering, input, and evolution logic
|
|
||||||
- **Visualization**: Uses Raylib for graphics and Dear ImGui for UI
|
|
||||||
|
|
||||||
### Server
|
|
||||||
- **TCP Server**: Handles client connections and data persistence
|
|
||||||
- **Database**: SQLite for storing organism data and user preferences
|
|
||||||
- **Block Management**: Tracks warnings and blocked organisms
|
|
||||||
|
|
||||||
### Shared Library
|
|
||||||
- **DNA System**: Genetic algorithm implementation (`Dna.cpp`)
|
|
||||||
- **Networking**: TCP socket communication
|
|
||||||
- **Data Structures**: Shared between app and server
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
- CMake 3.10+
|
|
||||||
- C++17 compiler
|
|
||||||
- Raylib
|
|
||||||
- Dear ImGui
|
|
||||||
- SQLite3
|
|
||||||
|
|
||||||
### Build Instructions
|
|
||||||
|
|
||||||
```bash
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake ..
|
|
||||||
make
|
|
||||||
```
|
|
||||||
## Running
|
|
||||||
|
|
||||||
1. Start the server:
|
|
||||||
```bash
|
|
||||||
./build/server
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Run the application:
|
|
||||||
```bash
|
|
||||||
./build/app
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
- **Like/Disike**: Click on organisms to influence evolution
|
|
||||||
- **Rotation**: Drag to rotate the view
|
|
||||||
- **Statistics**: View similarity scores and generation information
|
|
||||||
|
|
||||||
## Database
|
|
||||||
|
|
||||||
The application uses SQLite database (`data.db`) to store:
|
|
||||||
- Organism DNA data
|
|
||||||
- User preferences
|
|
||||||
- Blocked/warned organisms
|
|
||||||
|
|
||||||
## Screenshots
|
|
||||||
|
|
||||||

|
|
||||||

|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 168 KiB |
147
random/slike.cpp
147
random/slike.cpp
@@ -1,147 +0,0 @@
|
|||||||
#include <raylib.h>
|
|
||||||
#include <raymath.h>
|
|
||||||
#include <cmath>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
Vector2 newPoint(Vector2 start, float len, float angleD){
|
|
||||||
float angleR = (angleD * PI) / 180; // radian
|
|
||||||
return {start.x + len * cos(angleR), start.y - len * sin(angleR)};
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawBranch(Vector2 startV, Vector2 endV, Color startC, Color endC, int startR, int endR){
|
|
||||||
float fstep = 0.05;
|
|
||||||
for (float i = 0; i < 1.05; i += fstep)
|
|
||||||
{
|
|
||||||
Vector2 point = Vector2Lerp(startV, endV, i);
|
|
||||||
Color color = ColorLerp(startC, endC, i);
|
|
||||||
int size = Lerp(startR, endR, i);
|
|
||||||
DrawCircleV(point, size, color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr float screenWidth = 800;
|
|
||||||
constexpr float screenHeight = 800;
|
|
||||||
|
|
||||||
constexpr float len = 300;
|
|
||||||
constexpr float angleD = 100;
|
|
||||||
constexpr Vector2 start = {screenWidth / 2, screenHeight - 100};
|
|
||||||
constexpr float radius = 20;
|
|
||||||
constexpr float radiusS = 80;
|
|
||||||
constexpr float radiusE = 60;
|
|
||||||
constexpr Color colorS = RED;
|
|
||||||
constexpr Color colorE = GREEN;
|
|
||||||
|
|
||||||
typedef void (*slika)();
|
|
||||||
|
|
||||||
int idx = 0;
|
|
||||||
|
|
||||||
std::vector<slika> v_slik = {
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
DrawCircleV(start, radius, BLACK);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 end = {start.x, start.y - len};
|
|
||||||
DrawCircleV(start, radius, BLACK);
|
|
||||||
DrawLineEx(start, end, 10, BLACK);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 end = newPoint(start, len, angleD);
|
|
||||||
DrawCircleV(start, radius, BLACK);
|
|
||||||
DrawLineEx(start, end, 10, BLACK);
|
|
||||||
DrawCircleV(end, radius, BLACK);
|
|
||||||
DrawCircleSectorLines(start, len / 3, 360 - angleD, 360, 30, BLACK);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 end = newPoint(start, len, angleD);
|
|
||||||
DrawCircleV(start, radius, BLACK);
|
|
||||||
DrawLineEx(start, end, 10, BLACK);
|
|
||||||
DrawCircleV(end, radius, BLACK);
|
|
||||||
DrawCircleLinesV(start, radiusS, BLACK);
|
|
||||||
DrawCircleLinesV(end, radiusE, BLACK);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 end = newPoint(start, len, angleD);
|
|
||||||
DrawLineEx(start, end, 10, BLACK);
|
|
||||||
DrawCircleV(start, radiusS, colorS);
|
|
||||||
DrawCircleV(end, radiusE, colorE);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 end = newPoint(start, len, angleD);
|
|
||||||
drawBranch(start, end, colorS, colorE, radiusS, radiusE);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 p = newPoint(start, len, angleD);
|
|
||||||
drawBranch(start, p, colorS, colorE, radiusS, radiusE);
|
|
||||||
Vector2 p1 = newPoint(p, len, 135);
|
|
||||||
Vector2 p2 = newPoint(p, len, 90);
|
|
||||||
Vector2 p3 = newPoint(p, len, 45);
|
|
||||||
DrawLineEx(p, p1, 10, BLACK);
|
|
||||||
DrawLineEx(p, p2, 10, BLACK);
|
|
||||||
DrawLineEx(p, p3, 10, BLACK);
|
|
||||||
DrawCircleV(p, radius, BLACK);
|
|
||||||
DrawCircleV(p1, radius, BLACK);
|
|
||||||
DrawCircleV(p2, radius, BLACK);
|
|
||||||
DrawCircleV(p3, radius, BLACK);
|
|
||||||
},
|
|
||||||
|
|
||||||
[]()
|
|
||||||
{
|
|
||||||
Vector2 p = newPoint(start, len, angleD);
|
|
||||||
drawBranch(start, p, colorS, colorE, radiusS, radiusE);
|
|
||||||
Vector2 p1 = newPoint(p, len, 135);
|
|
||||||
Vector2 p2 = newPoint(p, len, 90);
|
|
||||||
Vector2 p3 = newPoint(p, len, 45);
|
|
||||||
drawBranch(p, p1, colorE, BLUE, radiusE, 50);
|
|
||||||
drawBranch(p, p2, colorE, ORANGE, radiusE, 50);
|
|
||||||
drawBranch(p, p3, colorE, PURPLE, radiusE, 50);
|
|
||||||
},
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
InitWindow(screenWidth, screenHeight, "Slike");
|
|
||||||
SetTargetFPS(60);
|
|
||||||
|
|
||||||
while (!WindowShouldClose())
|
|
||||||
{
|
|
||||||
BeginDrawing();
|
|
||||||
if (IsKeyPressed(KEY_S))
|
|
||||||
{
|
|
||||||
TakeScreenshot(TextFormat("slika%.2d.png", idx));
|
|
||||||
}
|
|
||||||
if (IsKeyPressed(KEY_N))
|
|
||||||
{
|
|
||||||
idx++;
|
|
||||||
if (idx >= v_slik.size())
|
|
||||||
idx = v_slik.size() - 1;
|
|
||||||
}
|
|
||||||
if (IsKeyPressed(KEY_P))
|
|
||||||
{
|
|
||||||
idx--;
|
|
||||||
if (idx < 0)
|
|
||||||
idx = 0;
|
|
||||||
}
|
|
||||||
ClearBackground(WHITE);
|
|
||||||
v_slik[idx]();
|
|
||||||
|
|
||||||
EndDrawing();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
CloseWindow();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -32,9 +32,8 @@ private:
|
|||||||
Vector2 start = {0};
|
Vector2 start = {0};
|
||||||
std::list<DrawArgs> drawCalls;
|
std::list<DrawArgs> drawCalls;
|
||||||
|
|
||||||
void calculateBranch();
|
void drawBranch();
|
||||||
|
|
||||||
void drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness);
|
|
||||||
inline size_t getNumOfBranches(int dep);
|
inline size_t getNumOfBranches(int dep);
|
||||||
inline Color getStartColor(DrawArgs &arg);
|
inline Color getStartColor(DrawArgs &arg);
|
||||||
inline Color getEndColor(int dep, Color &start);
|
inline Color getEndColor(int dep, Color &start);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
namespace Similarity
|
namespace Similarity
|
||||||
{
|
{
|
||||||
float euclidean_distance(Dna *d1, Dna *d2);// direct distance betwen vector. wont give 0 and 1
|
float euclidean_distance(Dna *d1, Dna *d2);// direct distance betwen vector. wont give 0 and 1
|
||||||
float dot_minmax(Dna *d1, Dna *d2); // doent return betwen 0 to 1
|
// float dot_product(Dna *d1, Dna *d2); // doent return betwen 0 to 1
|
||||||
float cosine_similarity(Dna *d1, Dna *d2);
|
float cosine_similarity(Dna *d1, Dna *d2);
|
||||||
float cosine_similarity_int(Dna *d1, Dna *d2);
|
float cosine_similarity_int(Dna *d1, Dna *d2);
|
||||||
float hamming_distance(Dna *d1, Dna *d2);
|
float hamming_distance(Dna *d1, Dna *d2);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "canvas/BackGround.hpp"
|
#include "canvas/BackGround.hpp"
|
||||||
#include "canvas/BackGroundColors.hpp"
|
#include "canvas/BackGroundColors.hpp"
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include <rlgl.h>
|
|
||||||
|
|
||||||
#define ITER_PER_FRAME 5000
|
#define ITER_PER_FRAME 5000
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ void Tree::init(int size)
|
|||||||
start.x = size / 2;
|
start.x = size / 2;
|
||||||
start.y = size;
|
start.y = size;
|
||||||
calculateLevels(size);
|
calculateLevels(size);
|
||||||
// texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
|
//texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tree::draw(Dna *dna)
|
void Tree::draw(Dna *dna)
|
||||||
@@ -57,7 +56,7 @@ bool Tree::tick()
|
|||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (!drawCalls.empty())
|
while (!drawCalls.empty())
|
||||||
{
|
{
|
||||||
calculateBranch();
|
drawBranch();
|
||||||
drawCalls.pop_front();
|
drawCalls.pop_front();
|
||||||
i++;
|
i++;
|
||||||
if (i >= ITER_PER_FRAME)
|
if (i >= ITER_PER_FRAME)
|
||||||
@@ -69,7 +68,7 @@ bool Tree::tick()
|
|||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
void Tree::calculateBranch()
|
void Tree::drawBranch()
|
||||||
{
|
{
|
||||||
DrawArgs arg = drawCalls.front();
|
DrawArgs arg = drawCalls.front();
|
||||||
if (arg.dep == MAX_DEPTH)
|
if (arg.dep == MAX_DEPTH)
|
||||||
@@ -87,14 +86,17 @@ void Tree::calculateBranch()
|
|||||||
|
|
||||||
Color colorStart = getStartColor(arg);
|
Color colorStart = getStartColor(arg);
|
||||||
Color colorEnd = getEndColor(arg.dep, colorStart);
|
Color colorEnd = getEndColor(arg.dep, colorStart);
|
||||||
// drawBranch(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd);
|
|
||||||
for (float i = 0; i < 1; i += fstep)
|
for (float i = 0; i < 1; i += fstep)
|
||||||
{
|
{
|
||||||
Vector2 point = Vector2Lerp(arg.start, end, i);
|
Vector2 point = Vector2Lerp(arg.start, end, i);
|
||||||
Color color = ColorLerp(colorStart, colorEnd, i);
|
Color color = ColorLerp(colorStart, colorEnd, i);
|
||||||
int size = Lerp(sizeStart, sizeEnd, i);
|
int size = Lerp(sizeStart, sizeEnd, i);
|
||||||
DrawCircleV(point, size, color);
|
DrawCircleV(point, size, color);
|
||||||
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
|
//DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
|
||||||
|
|
||||||
|
// use
|
||||||
|
// DrawRectangleGradientEx
|
||||||
}
|
}
|
||||||
|
|
||||||
// add more branches to draw
|
// add more branches to draw
|
||||||
@@ -198,51 +200,3 @@ inline float Tree::getAngleVar(DrawArgs &arg)
|
|||||||
|
|
||||||
return angleVar;
|
return angleVar;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tree::drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness)
|
|
||||||
{
|
|
||||||
// Calculate the direction vector from startPoint to endPoint
|
|
||||||
Vector2 direction = {endPoint.x - startPoint.x, endPoint.y - startPoint.y};
|
|
||||||
|
|
||||||
// Normalize the direction vector
|
|
||||||
float length = sqrtf(direction.x * direction.x + direction.y * direction.y);
|
|
||||||
if (length == 0)
|
|
||||||
length = 1; // Avoid division by zero
|
|
||||||
Vector2 normalizedDir = {direction.x / length, direction.y / length};
|
|
||||||
|
|
||||||
// Calculate the perpendicular vector (rotate 90 degrees)
|
|
||||||
Vector2 perpendicular = {-normalizedDir.y, normalizedDir.x};
|
|
||||||
|
|
||||||
// Calculate the four vertices of the quadrilateral
|
|
||||||
Vector2 topLeft = {
|
|
||||||
startPoint.x + perpendicular.x * startThickness,
|
|
||||||
startPoint.y + perpendicular.y * startThickness};
|
|
||||||
Vector2 topRight = {
|
|
||||||
endPoint.x + perpendicular.x * endThickness,
|
|
||||||
endPoint.y + perpendicular.y * endThickness};
|
|
||||||
Vector2 bottomLeft = {
|
|
||||||
startPoint.x - perpendicular.x * startThickness,
|
|
||||||
startPoint.y - perpendicular.y * startThickness};
|
|
||||||
Vector2 bottomRight = {
|
|
||||||
endPoint.x - perpendicular.x * endThickness,
|
|
||||||
endPoint.y - perpendicular.y * endThickness};
|
|
||||||
|
|
||||||
// Draw the two triangles to form the quadrilateral
|
|
||||||
rlBegin(RL_TRIANGLES);
|
|
||||||
// First triangle
|
|
||||||
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
|
|
||||||
rlVertex2f(topLeft.x, topLeft.y);
|
|
||||||
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
|
|
||||||
rlVertex2f(topRight.x, topRight.y);
|
|
||||||
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
|
|
||||||
rlVertex2f(bottomLeft.x, bottomLeft.y);
|
|
||||||
|
|
||||||
// Second triangle
|
|
||||||
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
|
|
||||||
rlVertex2f(bottomLeft.x, bottomLeft.y);
|
|
||||||
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
|
|
||||||
rlVertex2f(topRight.x, topRight.y);
|
|
||||||
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
|
|
||||||
rlVertex2f(bottomRight.x, bottomRight.y);
|
|
||||||
rlEnd();
|
|
||||||
}
|
|
||||||
@@ -2,26 +2,10 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <raylib.h>
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
namespace Similarity
|
namespace Similarity
|
||||||
{
|
{
|
||||||
|
|
||||||
float dot_minmax(Dna *d1, Dna *d2)
|
|
||||||
{
|
|
||||||
uint64_t max = sizeof(Dna) * 255 * 255;
|
|
||||||
uint8_t *a = (uint8_t *)d1;
|
|
||||||
uint8_t *b = (uint8_t *)d2;
|
|
||||||
uint32_t result = 0;
|
|
||||||
for (size_t i = 0; i < sizeof(Dna); ++i)
|
|
||||||
{
|
|
||||||
result += static_cast<uint32_t>(a[i]) * static_cast<uint32_t>(b[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result / (double)max;
|
|
||||||
}
|
|
||||||
|
|
||||||
float euclidean_distance(Dna *d1, Dna *d2)
|
float euclidean_distance(Dna *d1, Dna *d2)
|
||||||
{
|
{
|
||||||
uint8_t *a = (uint8_t *)d1;
|
uint8_t *a = (uint8_t *)d1;
|
||||||
@@ -116,45 +100,8 @@ namespace Similarity
|
|||||||
return 1 - (distance / (end - start));
|
return 1 - (distance / (end - start));
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *nameofFunc(simil_func f)
|
|
||||||
{
|
|
||||||
if (f == &Similarity::euclidean_distance)
|
|
||||||
{
|
|
||||||
return "eucl";
|
|
||||||
}
|
|
||||||
else if (f == &Similarity::dot_minmax)
|
|
||||||
{
|
|
||||||
return "dot";
|
|
||||||
}
|
|
||||||
else if (f == &Similarity::cosine_similarity)
|
|
||||||
{
|
|
||||||
return "cos";
|
|
||||||
}
|
|
||||||
else if (f == &Similarity::cosine_similarity_int)
|
|
||||||
{
|
|
||||||
return "cos_i";
|
|
||||||
}
|
|
||||||
else if (f == &Similarity::hamming_distance)
|
|
||||||
{
|
|
||||||
return "hamming_distance";
|
|
||||||
}
|
|
||||||
else if (f == &Similarity::hamming_distance_without_seeds)
|
|
||||||
{
|
|
||||||
return "hamming_distance_without_seeds";
|
|
||||||
}
|
|
||||||
else if (f == &Similarity::levenshtein_distance)
|
|
||||||
{
|
|
||||||
return "leven";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return "unknown nameofFunc";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float calc_similarity(std::vector<Dna> &vec, simil_func f)
|
float calc_similarity(std::vector<Dna> &vec, simil_func f)
|
||||||
{
|
{
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
size_t num_pairs = (vec.size() * (vec.size() - 1)) / 2;
|
size_t num_pairs = (vec.size() * (vec.size() - 1)) / 2;
|
||||||
|
|
||||||
float total_similarity = 0.0;
|
float total_similarity = 0.0;
|
||||||
@@ -166,11 +113,6 @@ namespace Similarity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
float average_similarity = total_similarity / num_pairs;
|
float average_similarity = total_similarity / num_pairs;
|
||||||
|
|
||||||
auto stop = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
const auto int_ms = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
|
|
||||||
|
|
||||||
return average_similarity * 100.0f;
|
return average_similarity * 100.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +123,7 @@ namespace Similarity
|
|||||||
uint8_t *b = (uint8_t *)d2;
|
uint8_t *b = (uint8_t *)d2;
|
||||||
|
|
||||||
// Create a distance matrix
|
// Create a distance matrix
|
||||||
static std::vector<std::vector<uint32_t>> dp(len + 1, std::vector<uint32_t>(len + 1, 0));
|
std::vector<std::vector<uint32_t>> dp(len + 1, std::vector<uint32_t>(len + 1, 0));
|
||||||
|
|
||||||
// Initialize the first row and column
|
// Initialize the first row and column
|
||||||
for (size_t i = 0; i <= len; ++i)
|
for (size_t i = 0; i <= len; ++i)
|
||||||
|
|||||||
14
tmp.txt
14
tmp.txt
@@ -1,14 +0,0 @@
|
|||||||
eucl cos cos_i hamming dot leven
|
|
||||||
91 117 181 87 41 105799
|
|
||||||
60 78 305 250 40 100331
|
|
||||||
61 78 121 105 40 97438
|
|
||||||
66 81 124 106 40 97529
|
|
||||||
60 78 127 108 40 96296
|
|
||||||
62 85 131 104 39 96456
|
|
||||||
61 81 125 106 40 96510
|
|
||||||
61 81 125 103 40 97253
|
|
||||||
61 81 125 78 40 97409
|
|
||||||
60 82 125 103 40 99816
|
|
||||||
62 81 128 81 40 98978
|
|
||||||
68 81 126 58 40 98289
|
|
||||||
61 88 130 60 39 99663
|
|
||||||
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
@@ -28,7 +28,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
bool showSelection;
|
bool showSelection;
|
||||||
bool showStats;
|
bool showStats;
|
||||||
bool saveToFile;
|
|
||||||
sqlite3 *db;
|
sqlite3 *db;
|
||||||
sqlite3_stmt *get_gen_num;
|
sqlite3_stmt *get_gen_num;
|
||||||
std::vector<int64_t> ids;
|
std::vector<int64_t> ids;
|
||||||
|
|||||||
@@ -105,12 +105,12 @@ void Vapp::update()
|
|||||||
simil[1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
|
simil[1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
|
||||||
simil[2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
|
simil[2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
|
||||||
simil[3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
|
simil[3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
|
||||||
simil[4] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
simil[4] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance_without_seeds);
|
||||||
simil[5] = Similarity::calc_similarity(manager.vector, Similarity::dot_minmax);
|
simil[5] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
||||||
stageOfDrawing = DrawingStage::save;
|
stageOfDrawing = DrawingStage::save;
|
||||||
break;
|
break;
|
||||||
case DrawingStage::save:
|
case DrawingStage::save:
|
||||||
if(saveToFile) drawToFile();
|
drawToFile();
|
||||||
stageOfDrawing = DrawingStage::done;
|
stageOfDrawing = DrawingStage::done;
|
||||||
break;
|
break;
|
||||||
case DrawingStage::done:
|
case DrawingStage::done:
|
||||||
@@ -140,10 +140,6 @@ void Vapp::draw()
|
|||||||
{
|
{
|
||||||
setUpTable();
|
setUpTable();
|
||||||
}
|
}
|
||||||
if(ImGui::MenuItem("Save to File", nullptr, saveToFile, true))
|
|
||||||
{
|
|
||||||
saveToFile = !saveToFile;
|
|
||||||
}
|
|
||||||
ImGui::EndMainMenuBar();
|
ImGui::EndMainMenuBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,8 +185,8 @@ void Vapp::draw()
|
|||||||
ImGui::LabelText("##sim2", "cosine_similarity: %f", simil[1]);
|
ImGui::LabelText("##sim2", "cosine_similarity: %f", simil[1]);
|
||||||
ImGui::LabelText("##sim3", "cosine_similarity_int: %f", simil[2]);
|
ImGui::LabelText("##sim3", "cosine_similarity_int: %f", simil[2]);
|
||||||
ImGui::LabelText("##sim4", "hamming_distance: %f", simil[3]);
|
ImGui::LabelText("##sim4", "hamming_distance: %f", simil[3]);
|
||||||
ImGui::LabelText("##sim5", "levenshtein_distance: %f", simil[4]);
|
ImGui::LabelText("##sim5", "hamming_distance_without_seeds: %f", simil[4]);
|
||||||
ImGui::LabelText("##sim6", "dot_minmax: %f", simil[5]);
|
ImGui::LabelText("##sim6", "levenshtein_distance: %f", simil[5]);
|
||||||
|
|
||||||
const ImGuiTableFlags flags = ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
|
const ImGuiTableFlags flags = ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
|
||||||
const int columns = numberOfFunc + 1;
|
const int columns = numberOfFunc + 1;
|
||||||
@@ -210,9 +206,9 @@ void Vapp::draw()
|
|||||||
ImGui::TableSetColumnIndex(4);
|
ImGui::TableSetColumnIndex(4);
|
||||||
ImGui::Text("hamming_distance");
|
ImGui::Text("hamming_distance");
|
||||||
ImGui::TableSetColumnIndex(5);
|
ImGui::TableSetColumnIndex(5);
|
||||||
ImGui::Text("levenshtein_distance");
|
ImGui::Text("hamming_distance_without_seeds");
|
||||||
ImGui::TableSetColumnIndex(6);
|
ImGui::TableSetColumnIndex(6);
|
||||||
ImGui::Text("dot_minmax");
|
ImGui::Text("levenshtein_distance");
|
||||||
|
|
||||||
for (int row = 0; row < similTable.size(); row++)
|
for (int row = 0; row < similTable.size(); row++)
|
||||||
{
|
{
|
||||||
@@ -329,8 +325,8 @@ void Vapp::setUpTable()
|
|||||||
similTable[s][1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
|
similTable[s][1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
|
||||||
similTable[s][2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
|
similTable[s][2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
|
||||||
similTable[s][3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
|
similTable[s][3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
|
||||||
similTable[s][4] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
similTable[s][4] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance_without_seeds);
|
||||||
similTable[s][5] = Similarity::calc_similarity(manager.vector, Similarity::dot_minmax);
|
similTable[s][5] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
||||||
DnaManager::newGen(&manager);
|
DnaManager::newGen(&manager);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -339,28 +335,27 @@ void Vapp::setUpTable()
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql::reset(get_gen_stmt);
|
sql::reset(get_gen_stmt);
|
||||||
if(saveToFile)
|
|
||||||
|
int64_t id = ids[selected_id_index];
|
||||||
|
char buff[50];
|
||||||
|
sprintf(buff, "%ld.txt", id);
|
||||||
|
std::ofstream file(buff);
|
||||||
|
|
||||||
|
file << "| index | euclidean_distance | cosine_similarity | cosine_similarity_int | hamming_distance | hamming_distance_without_seeds | levenshtein_distance |\n";
|
||||||
|
file << "| --- | --- | --- | --- | --- | --- | --- |\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (size_t i = 0; i < similTable.size(); i++)
|
||||||
{
|
{
|
||||||
int64_t id = ids[selected_id_index];
|
file << "|" << i << "|";
|
||||||
const char* buff = TextFormat("%ld.txt", id);
|
for (size_t j = 0; j < similTable[i].size(); j++)
|
||||||
std::ofstream file(buff);
|
|
||||||
|
|
||||||
file << "| index | euclidean_distance | cosine_similarity | cosine_similarity_int | hamming_distance | levenshtein_distance | dot_minmax |\n";
|
|
||||||
file << "| --- | --- | --- | --- | --- | --- | --- |\n";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (size_t i = 0; i < similTable.size(); i++)
|
|
||||||
{
|
{
|
||||||
file << "|" << i << "|";
|
file << similTable[i][j] << "|";
|
||||||
for (size_t j = 0; j < similTable[i].size(); j++)
|
|
||||||
{
|
|
||||||
file << similTable[i][j] << "|";
|
|
||||||
}
|
|
||||||
file << "\n";
|
|
||||||
}
|
}
|
||||||
|
file << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sql::finalize(get_gen_stmt);
|
sql::finalize(get_gen_stmt);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user