Symphony 1.0
Loading...
Searching...
No Matches
study_path.h
Go to the documentation of this file.
1//
2// Created by velocitatem on 12/16/24.
3//
4
5#ifndef STUDY_PATH_H
6#define STUDY_PATH_H
7
8#include <fstream>
9#include <iostream>
10#include <map>
11#include <sstream>
12#include <vector>
13#include <nlohmann/json.hpp>
14#include "symphony.h"
15
16
17
18// Study-specific classes
19class StudyState : public State {
20public:
21 std::map<std::string, double> mastery_levels; // Topic -> Mastery %
23
24 StudyState(std::map<std::string, double> mastery_levels, double remaining_time)
26
27 void print() override {
28 for (const auto& [topic, mastery] : mastery_levels) {
29 std::cout << topic << ": " << mastery << "%\n";
30 }
31 std::cout << "Time left: " << remaining_time << " hours\n";
32 }
33};
34
35class StudyProblem : public Problem {
36 StudyState* initial_state_;
37 std::map<std::string, std::vector<std::string>> dependencies; // Topic -> Prerequisites
38 std::map<std::string, double> synergies; // Topic synergy weights
39
40public:
42 std::map<std::string, std::vector<std::string>> dependencies,
43 std::map<std::string, double> synergies)
44 : initial_state_(initial_state), dependencies(std::move(dependencies)), synergies(std::move(synergies)) {}
45
46 State* initial_state() override {
47 return initial_state_;
48 }
49
50 bool goal_test(State* state) override {
51 auto* study_state = dynamic_cast<StudyState*>(state);
52 for (const auto& [_, mastery] : study_state->mastery_levels) {
53 if (mastery < 100.0) return false;
54 }
55 return true;
56 }
57
58 std::vector<std::shared_ptr<Action>> actions(std::shared_ptr<State> state) override {
59 auto* study_state = std::dynamic_pointer_cast<StudyState>(state).get();
60 std::vector<std::shared_ptr<Action>> available_actions;
61
62 for (const auto& [topic, mastery] : study_state->mastery_levels) {
63 if (mastery < 100.0 && study_state->remaining_time > 0) {
64 double cost = 1.0; // 1 hour per study session
65 auto new_mastery = study_state->mastery_levels;
66 new_mastery[topic] += std::min(10.0, 100.0 - mastery); // Increment by 10%, cap at 100%
67 double synergy_bonus = synergies.count(topic) ? synergies.at(topic) : 0.0;
68 new_mastery[topic] += synergy_bonus;
69
70 double time_left = study_state->remaining_time - cost;
71 auto new_state = new StudyState(new_mastery, time_left);
72 available_actions.emplace_back( std::make_shared<Action>(topic, cost, state, std::shared_ptr<State>(new_state)) );
73 }
74 }
75
76 return available_actions;
77 }
78
79 double heuristic(State* state) override {
80 auto* study_state = dynamic_cast<StudyState*>(state);
81 double total_gap = 0;
82 for (const auto& [_, mastery] : study_state->mastery_levels) {
83 total_gap += (100.0 - mastery);
84 }
85 return total_gap / study_state->remaining_time;
86 }
87};
88
89// Main Function
90void run(std::string &input) {
91 std::ifstream file(input);
92 std::string json_string;
93 std::string line;
94 while (std::getline(file, line)) {
95 json_string += line;
96 }
97
98 nlohmann::json json = nlohmann::json::parse(json_string);
99 std::map<std::string, double> mastery_levels = json["mastery_levels"];
100 std::map<std::string, std::vector<std::string>> dependencies = json["dependencies"];
101 std::map<std::string, double> synergies = json["synergies"];
102 double time = json["time"];
103
104
105 auto initial_state = new StudyState(mastery_levels, time); // 7 days, 3 hours/day
106 auto problem = new StudyProblem(initial_state, dependencies, synergies);
107 auto solution = create_search(BEAM_SEARCH, problem)->search();
108
109 if (solution) {
110 std::cout << "Optimal study plan:\n";
111 auto current = solution;
112 while (current) {
113 if (current->action) {
114 std::cout << "Study " << current->action->name << " for " << current->action->cost << " hours\n";
115 }
116 current = current->parent;
117 }
118 } else {
119 std::cout << "No solution found within the given time.\n";
120 }
121 delete problem;
122
123}
124
125
126#endif //STUDY_PATH_H
Represents an abstract problem that needs to be solved.
Definition definitions.h:63
virtual std::shared_ptr< Node > search()=0
Represents an abstract state in a problem.
Definition definitions.h:18
State * initial_state() override
Retrieves the initial state of the problem.
Definition study_path.h:46
std::vector< std::shared_ptr< Action > > actions(std::shared_ptr< State > state) override
Retrieves the set of actions applicable to a given state.
Definition study_path.h:58
bool goal_test(State *state) override
Tests if a given state satisfies the goal condition.
Definition study_path.h:50
double heuristic(State *state) override
Computes a heuristic estimate for a given state.
Definition study_path.h:79
StudyProblem(StudyState *initial_state, std::map< std::string, std::vector< std::string > > dependencies, std::map< std::string, double > synergies)
Definition study_path.h:41
std::map< std::string, double > mastery_levels
Definition study_path.h:21
StudyState(std::map< std::string, double > mastery_levels, double remaining_time)
Definition study_path.h:24
double remaining_time
Definition study_path.h:22
void print() override
Prints the state details.
Definition study_path.h:27
Search * create_search(SearchAlgorithmIndex search_algorithm_index, Problem *problem)
Factory method to create a search object based on the specified search algorithm.
Definition search.cpp:13
@ BEAM_SEARCH
Definition search.h:106
void run(std::string &input)
Definition study_path.h:90