Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ include_directories(
)

# === Source files ===
set(SOURCE_FILES
src/main.cpp

set(LIB_SOURCES
include/agents/QLearningAgent.cpp
include/core/ExecutionSimulator.cpp
include/env/MarketEnvironment.cpp
include/utils/Logger.cpp
include/utils/MathUtils.cpp
)

add_library(executionrl_lib ${LIB_SOURCES})
target_compile_definitions(executionrl_lib PRIVATE DATA_PATH="${CMAKE_SOURCE_DIR}/data/price_series.csv")

# === Executable ===
add_executable(ExecutionRL ${SOURCE_FILES})
add_executable(ExecutionRL src/main.cpp)
target_link_libraries(ExecutionRL PRIVATE executionrl_lib)

# === Tests ===
enable_testing()
Expand All @@ -34,10 +36,17 @@ add_executable(test_agent tests/test_agent.cpp)
add_executable(test_env tests/test_env.cpp)
add_executable(test_simulation tests/test_simulation.cpp)

target_link_libraries(test_agent PRIVATE executionrl_lib)
target_link_libraries(test_env PRIVATE executionrl_lib)
target_link_libraries(test_simulation PRIVATE executionrl_lib)

target_include_directories(test_agent PRIVATE include)
target_include_directories(test_env PRIVATE include)
target_include_directories(test_simulation PRIVATE include)

add_test(NAME AgentTest COMMAND test_agent)
add_test(NAME EnvTest COMMAND test_env)
add_test(NAME SimulationTest COMMAND test_simulation)

set_tests_properties(AgentTest EnvTest SimulationTest
PROPERTIES WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
18 changes: 10 additions & 8 deletions include/core/ExecutionSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
ExecutionSimulator::ExecutionSimulator(MarketEnvironment& env, QLearningAgent& agent)
: env(env), agent(agent) {}

void ExecutionSimulator::run() {
env.reset();
while (!env.isDone()) {
auto state = env.getState();
int action = agent.chooseAction(state);
double reward = env.step(action);
auto nextState = env.getState();
agent.update(state, action, reward, nextState);
void ExecutionSimulator::run(int episodes) {
for (int i = 0; i < episodes; ++i) {
env.reset();
while (!env.isDone()) {
auto state = env.getState();
int action = agent.chooseAction(state);
double reward = env.step(action);
auto nextState = env.getState();
agent.update(state, action, reward, nextState);
}
}
}
2 changes: 1 addition & 1 deletion include/core/ExecutionSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ExecutionSimulator {
public:
ExecutionSimulator(MarketEnvironment& env, QLearningAgent& agent);
void run();
void run(int episodes = 1);

private:
MarketEnvironment& env;
Expand Down
14 changes: 12 additions & 2 deletions include/env/MarketEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,25 @@ bool MarketEnvironment::isDone() const {
}

void MarketEnvironment::loadMarketData() {
std::ifstream file("prices.csv");
std::ifstream file(DATA_PATH);
std::string line;

prices.clear();
bool firstLine = true;
while (std::getline(file, line)) {
if (firstLine) { // skip header if present
firstLine = false;
if (line.find_first_not_of("0123456789-.") != std::string::npos)
continue;
}
std::stringstream ss(line);
std::string cell;
if (std::getline(ss, cell, ',')) {
prices.push_back(std::stod(cell));
try {
prices.push_back(std::stod(cell));
} catch (const std::invalid_argument&) {
// ignore malformed lines
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "include/core/ExecutionSimulator.hpp"
#include "include/env/MarketEnvironment.hpp"
#include "include/agents/QLearningAgent.hpp"
#include "core/ExecutionSimulator.hpp"
#include "env/MarketEnvironment.hpp"
#include "agents/QLearningAgent.hpp"

int main() {
MarketEnvironment env;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_agent.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/agents/QLearningAgent.hpp"
#include "agents/QLearningAgent.hpp"
#include <cassert>
#include <iostream>

Expand Down
2 changes: 1 addition & 1 deletion tests/test_env.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/env/MarketEnvironment.hpp"
#include "env/MarketEnvironment.hpp"
#include <cassert>
#include <iostream>

Expand Down
8 changes: 4 additions & 4 deletions tests/test_simulation.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../include/core/ExecutionSimulator.hpp"
#include "../include/env/MarketEnvironment.hpp"
#include "../include/agents/QLearningAgent.hpp"
#include "../include/config.hpp"
#include "core/ExecutionSimulator.hpp"
#include "env/MarketEnvironment.hpp"
#include "agents/QLearningAgent.hpp"
#include "config.hpp"
#include <iostream>

int main() {
Expand Down
Loading