Compare commits
7 Commits
060b9f9182
...
test_sqlit
| Author | SHA1 | Date | |
|---|---|---|---|
| 6794dada92 | |||
| 6ff4a66551 | |||
| 45f73c721e | |||
| e99bd33985 | |||
| 40b2f963a6 | |||
| cc00586bc9 | |||
| d7ea0b99ac |
@@ -8,7 +8,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
# set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||
add_compile_options(-ggdb)
|
||||
|
||||
include_directories(external/include/)
|
||||
include_directories(
|
||||
external/include
|
||||
shared/inc
|
||||
)
|
||||
|
||||
link_libraries(
|
||||
${CMAKE_SOURCE_DIR}/external/libimgui.a
|
||||
${CMAKE_SOURCE_DIR}/external/libraylib.a
|
||||
@@ -18,50 +22,44 @@ link_libraries(
|
||||
m
|
||||
)
|
||||
|
||||
add_library(shared STATIC
|
||||
shared/src/canvas/BackGround.cpp
|
||||
shared/src/canvas/BackGroundColors.cpp
|
||||
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
|
||||
shared/src/TcpSocket.cpp
|
||||
shared/src/sql.cpp
|
||||
shared/src/values/DnaDB.cpp
|
||||
)
|
||||
|
||||
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/BackGroundColors.cpp
|
||||
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
|
||||
shared/src/TcpSocket.cpp
|
||||
)
|
||||
# Add include directories
|
||||
target_include_directories(app PRIVATE app/inc shared/inc )
|
||||
target_include_directories(app PRIVATE app/inc)
|
||||
target_link_libraries(app PRIVATE shared)
|
||||
|
||||
add_executable(server
|
||||
server/src/server.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
|
||||
)
|
||||
# Add include directories
|
||||
target_include_directories(server PRIVATE server/inc shared/inc)
|
||||
target_include_directories(server PRIVATE server/inc)
|
||||
target_link_libraries(server PRIVATE shared)
|
||||
|
||||
add_executable(view
|
||||
view/src/main.cpp
|
||||
view/src/Vapp.cpp
|
||||
shared/src/canvas/BackGround.cpp
|
||||
shared/src/canvas/BackGroundColors.cpp
|
||||
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
|
||||
|
||||
shared/src/sql.cpp
|
||||
)
|
||||
# Add include directories
|
||||
target_include_directories(view PRIVATE view/inc shared/inc)
|
||||
target_include_directories(view PRIVATE view/inc)
|
||||
target_link_libraries(view PRIVATE shared)
|
||||
|
||||
add_executable(slike
|
||||
random/slike.cpp
|
||||
)
|
||||
target_link_libraries(slike PRIVATE shared)
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <array>
|
||||
#include "canvas/Canvas.hpp"
|
||||
#include "DnaStore.hpp"
|
||||
|
||||
class App
|
||||
{
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace DnaStore
|
||||
{
|
||||
void init();
|
||||
void deinit();
|
||||
void load(DnaManagerData *data);
|
||||
void saveData(DnaManagerData *data);
|
||||
void saveVec(DnaManagerData *data);
|
||||
|
||||
@@ -64,7 +64,7 @@ void App::init(int screenWidth, int screenHeight)
|
||||
this->screenWidth = screenWidth;
|
||||
this->screenHeight = screenHeight;
|
||||
this->canvas.init(screenWidth);
|
||||
|
||||
DnaStore::init();
|
||||
// int s = MeasureText("GEN 9999: 999/999", 20);
|
||||
// TraceLog(LOG_INFO, "%d", s);
|
||||
|
||||
@@ -239,4 +239,5 @@ void App::deinit()
|
||||
UnloadRenderTexture(canvasTexure[i]);
|
||||
}
|
||||
canvas.deinit();
|
||||
DnaStore::deinit();
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "sys.hpp"
|
||||
#include "DnaStore.hpp"
|
||||
#include "TcpSocket.hpp"
|
||||
#include "values/DnaDB.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
@@ -17,19 +18,30 @@
|
||||
#define VECTOR_FILE_NAME "VECTOR.bin"
|
||||
#define GEN_FILE_PATTRN "gen/%04d.bin"
|
||||
#define HOST_NAME "localhost"
|
||||
#define DB_NAME "data.db"
|
||||
|
||||
DnaDB dnaDB;
|
||||
|
||||
void DnaStore::init(){
|
||||
const char* filename = sys::transformFilePath(DB_NAME);
|
||||
dnaDB.init(filename);
|
||||
}
|
||||
|
||||
void DnaStore::deinit(){
|
||||
dnaDB.deinit();
|
||||
}
|
||||
|
||||
void DnaStore::load(DnaManagerData *data)
|
||||
{
|
||||
const char *dir = sys::transformFilePath("gen");
|
||||
std::filesystem::create_directory(dir);
|
||||
if (sys::fileExists(ID_FILE_NAME))
|
||||
{
|
||||
sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(int64_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
std::vector<int64_t> ids = dnaDB.getUserIds();
|
||||
if(!ids.empty()){
|
||||
data->id = ids[0];
|
||||
}else{
|
||||
data->id = time(nullptr);
|
||||
sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(int64_t));
|
||||
dnaDB.insertUser(data->id, 0);
|
||||
}
|
||||
|
||||
if (sys::fileExists(DATA_FILE_NAME))
|
||||
|
||||
147
random/slike.cpp
Normal file
147
random/slike.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#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,8 +32,9 @@ private:
|
||||
Vector2 start = {0};
|
||||
std::list<DrawArgs> drawCalls;
|
||||
|
||||
void drawBranch();
|
||||
void calculateBranch();
|
||||
|
||||
void drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness);
|
||||
inline size_t getNumOfBranches(int dep);
|
||||
inline Color getStartColor(DrawArgs &arg);
|
||||
inline Color getEndColor(int dep, Color &start);
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace sql
|
||||
void init();
|
||||
void shutdown();
|
||||
int open(const char *filename, sqlite3 **ppDb);
|
||||
int exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *first_arg, char **errmsg);
|
||||
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail);
|
||||
int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value);
|
||||
int step(sqlite3_stmt *pStmt);
|
||||
|
||||
20
shared/inc/values/DnaDB.hpp
Normal file
20
shared/inc/values/DnaDB.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <cinttypes>
|
||||
#include <vector>
|
||||
|
||||
struct sqlite3;
|
||||
|
||||
class DnaDB
|
||||
{
|
||||
sqlite3 *db;
|
||||
|
||||
public:
|
||||
void init(const char *db_name = "data.db");
|
||||
void deinit();
|
||||
std::vector<int64_t> getUserIds();
|
||||
int insertUser(int64_t id, int64_t max_gen);
|
||||
int updateUser(int64_t id, int64_t max_gen);
|
||||
|
||||
void getGenerations();
|
||||
void getLikesForGeneration();
|
||||
void getVectorForGeneration();
|
||||
};
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
enum Liked
|
||||
{
|
||||
tbd,
|
||||
yes,
|
||||
no
|
||||
tbd = 0,
|
||||
yes = 1,
|
||||
no = 2,
|
||||
};
|
||||
|
||||
struct UiUnit
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
namespace Similarity
|
||||
{
|
||||
float euclidean_distance(Dna *d1, Dna *d2);// direct distance betwen vector. wont give 0 and 1
|
||||
// float dot_product(Dna *d1, Dna *d2); // doent return betwen 0 to 1
|
||||
float dot_minmax(Dna *d1, Dna *d2); // doent return betwen 0 to 1
|
||||
float cosine_similarity(Dna *d1, Dna *d2);
|
||||
float cosine_similarity_int(Dna *d1, Dna *d2);
|
||||
float hamming_distance(Dna *d1, Dna *d2);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
#include "canvas/BackGround.hpp"
|
||||
#include "canvas/BackGroundColors.hpp"
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#include <rlgl.h>
|
||||
|
||||
#define ITER_PER_FRAME 5000
|
||||
|
||||
@@ -56,7 +57,7 @@ bool Tree::tick()
|
||||
size_t i = 0;
|
||||
while (!drawCalls.empty())
|
||||
{
|
||||
drawBranch();
|
||||
calculateBranch();
|
||||
drawCalls.pop_front();
|
||||
i++;
|
||||
if (i >= ITER_PER_FRAME)
|
||||
@@ -68,7 +69,7 @@ bool Tree::tick()
|
||||
|
||||
// Private
|
||||
|
||||
void Tree::drawBranch()
|
||||
void Tree::calculateBranch()
|
||||
{
|
||||
DrawArgs arg = drawCalls.front();
|
||||
if (arg.dep == MAX_DEPTH)
|
||||
@@ -86,7 +87,7 @@ void Tree::drawBranch()
|
||||
|
||||
Color colorStart = getStartColor(arg);
|
||||
Color colorEnd = getEndColor(arg.dep, colorStart);
|
||||
|
||||
// drawBranch(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd);
|
||||
for (float i = 0; i < 1; i += fstep)
|
||||
{
|
||||
Vector2 point = Vector2Lerp(arg.start, end, i);
|
||||
@@ -94,9 +95,6 @@ void Tree::drawBranch()
|
||||
int size = Lerp(sizeStart, sizeEnd, i);
|
||||
DrawCircleV(point, size, color);
|
||||
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
|
||||
|
||||
// use
|
||||
// DrawRectangleGradientEx
|
||||
}
|
||||
|
||||
// add more branches to draw
|
||||
@@ -200,3 +198,51 @@ inline float Tree::getAngleVar(DrawArgs &arg)
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -78,6 +78,10 @@ namespace sql
|
||||
{
|
||||
return sqlite3_open(filename, ppDb);
|
||||
}
|
||||
int exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *first, char **errmsg)
|
||||
{
|
||||
return sqlite3_exec(db, sql, callback, first, errmsg);
|
||||
}
|
||||
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail)
|
||||
{
|
||||
return sqlite3_prepare_v2(db, zSql, nByte, pStmt, pzTail);
|
||||
|
||||
69
shared/src/values/DnaDB.cpp
Normal file
69
shared/src/values/DnaDB.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "values/DnaDB.hpp"
|
||||
|
||||
#include <sql.hpp>
|
||||
|
||||
|
||||
|
||||
void DnaDB::init(const char * db_name)
|
||||
{
|
||||
sql::init();
|
||||
int res = sql::open(db_name, &db);
|
||||
|
||||
constexpr char create_user_table[] = "CREATE TABLE IF NOT EXISTS user_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, MAX_GENERATION INTEGER);";
|
||||
res = sql::exec(db, create_user_table, nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void DnaDB::deinit()
|
||||
{
|
||||
sql::close(db);
|
||||
sql::shutdown();
|
||||
}
|
||||
|
||||
std::vector<int64_t> DnaDB::getUserIds()
|
||||
{
|
||||
constexpr char sql[] = "SELECT USER_ID FROM user_table;";
|
||||
sqlite3_stmt *stmt;
|
||||
sql::prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
std::vector<int64_t> ids;
|
||||
while (sql::step(stmt) != SQL_DONE)
|
||||
{
|
||||
int64_t id = sql::column_int64(stmt, 0);
|
||||
ids.push_back(id);
|
||||
}
|
||||
sql::finalize(stmt);
|
||||
return ids;
|
||||
}
|
||||
|
||||
int DnaDB::insertUser(int64_t id, int64_t max_gen){
|
||||
constexpr char sql[] = "INSERT INTO user_table (USER_ID, MAX_GENERATION) VALUES (?,?);";
|
||||
sqlite3_stmt *stmt;
|
||||
sql::prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
sql::bind_int64(stmt, 1, id);
|
||||
sql::bind_int64(stmt, 2, max_gen);
|
||||
int ret = sql::step(stmt);
|
||||
sql::finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DnaDB::updateUser(int64_t id, int64_t max_gen){
|
||||
constexpr char sql[] = "UPDATE user_table SET MAX_GENERATION = ? WHERE USER_ID = ?;";
|
||||
sqlite3_stmt *stmt;
|
||||
sql::prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
sql::bind_int64(stmt, 1, max_gen);
|
||||
sql::bind_int64(stmt, 2, id);
|
||||
int ret = sql::step(stmt);
|
||||
sql::finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DnaDB::getGenerations()
|
||||
{
|
||||
}
|
||||
|
||||
void DnaDB::getLikesForGeneration()
|
||||
{
|
||||
}
|
||||
|
||||
void DnaDB::getVectorForGeneration()
|
||||
{
|
||||
}
|
||||
@@ -2,10 +2,26 @@
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <raylib.h>
|
||||
#include <chrono>
|
||||
|
||||
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)
|
||||
{
|
||||
uint8_t *a = (uint8_t *)d1;
|
||||
@@ -100,8 +116,45 @@ namespace Similarity
|
||||
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)
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
size_t num_pairs = (vec.size() * (vec.size() - 1)) / 2;
|
||||
|
||||
float total_similarity = 0.0;
|
||||
@@ -113,6 +166,11 @@ namespace Similarity
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -123,7 +181,7 @@ namespace Similarity
|
||||
uint8_t *b = (uint8_t *)d2;
|
||||
|
||||
// Create a distance matrix
|
||||
std::vector<std::vector<uint32_t>> dp(len + 1, std::vector<uint32_t>(len + 1, 0));
|
||||
static std::vector<std::vector<uint32_t>> dp(len + 1, std::vector<uint32_t>(len + 1, 0));
|
||||
|
||||
// Initialize the first row and column
|
||||
for (size_t i = 0; i <= len; ++i)
|
||||
|
||||
14
tmp.txt
Normal file
14
tmp.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
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
|
||||
@@ -28,6 +28,7 @@ public:
|
||||
private:
|
||||
bool showSelection;
|
||||
bool showStats;
|
||||
bool saveToFile;
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *get_gen_num;
|
||||
std::vector<int64_t> ids;
|
||||
|
||||
@@ -105,12 +105,12 @@ void Vapp::update()
|
||||
simil[1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
|
||||
simil[2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
|
||||
simil[3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
|
||||
simil[4] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance_without_seeds);
|
||||
simil[5] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
||||
simil[4] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
||||
simil[5] = Similarity::calc_similarity(manager.vector, Similarity::dot_minmax);
|
||||
stageOfDrawing = DrawingStage::save;
|
||||
break;
|
||||
case DrawingStage::save:
|
||||
drawToFile();
|
||||
if(saveToFile) drawToFile();
|
||||
stageOfDrawing = DrawingStage::done;
|
||||
break;
|
||||
case DrawingStage::done:
|
||||
@@ -140,6 +140,10 @@ void Vapp::draw()
|
||||
{
|
||||
setUpTable();
|
||||
}
|
||||
if(ImGui::MenuItem("Save to File", nullptr, saveToFile, true))
|
||||
{
|
||||
saveToFile = !saveToFile;
|
||||
}
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
@@ -185,8 +189,8 @@ void Vapp::draw()
|
||||
ImGui::LabelText("##sim2", "cosine_similarity: %f", simil[1]);
|
||||
ImGui::LabelText("##sim3", "cosine_similarity_int: %f", simil[2]);
|
||||
ImGui::LabelText("##sim4", "hamming_distance: %f", simil[3]);
|
||||
ImGui::LabelText("##sim5", "hamming_distance_without_seeds: %f", simil[4]);
|
||||
ImGui::LabelText("##sim6", "levenshtein_distance: %f", simil[5]);
|
||||
ImGui::LabelText("##sim5", "levenshtein_distance: %f", simil[4]);
|
||||
ImGui::LabelText("##sim6", "dot_minmax: %f", simil[5]);
|
||||
|
||||
const ImGuiTableFlags flags = ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
|
||||
const int columns = numberOfFunc + 1;
|
||||
@@ -206,9 +210,9 @@ void Vapp::draw()
|
||||
ImGui::TableSetColumnIndex(4);
|
||||
ImGui::Text("hamming_distance");
|
||||
ImGui::TableSetColumnIndex(5);
|
||||
ImGui::Text("hamming_distance_without_seeds");
|
||||
ImGui::TableSetColumnIndex(6);
|
||||
ImGui::Text("levenshtein_distance");
|
||||
ImGui::TableSetColumnIndex(6);
|
||||
ImGui::Text("dot_minmax");
|
||||
|
||||
for (int row = 0; row < similTable.size(); row++)
|
||||
{
|
||||
@@ -325,8 +329,8 @@ void Vapp::setUpTable()
|
||||
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][3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
|
||||
similTable[s][4] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance_without_seeds);
|
||||
similTable[s][5] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
||||
similTable[s][4] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
|
||||
similTable[s][5] = Similarity::calc_similarity(manager.vector, Similarity::dot_minmax);
|
||||
DnaManager::newGen(&manager);
|
||||
}
|
||||
else
|
||||
@@ -335,13 +339,13 @@ void Vapp::setUpTable()
|
||||
}
|
||||
|
||||
sql::reset(get_gen_stmt);
|
||||
|
||||
if(saveToFile)
|
||||
{
|
||||
int64_t id = ids[selected_id_index];
|
||||
char buff[50];
|
||||
sprintf(buff, "%ld.txt", id);
|
||||
const char* buff = TextFormat("%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 << "| index | euclidean_distance | cosine_similarity | cosine_similarity_int | hamming_distance | levenshtein_distance | dot_minmax |\n";
|
||||
file << "| --- | --- | --- | --- | --- | --- | --- |\n";
|
||||
|
||||
|
||||
@@ -356,6 +360,7 @@ void Vapp::setUpTable()
|
||||
file << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sql::finalize(get_gen_stmt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user