Symphony 1.0
Loading...
Searching...
No Matches
task_scheduler.h
Go to the documentation of this file.
1#ifndef TASK_SCHEDULER_H
2#define TASK_SCHEDULER_H
3
4#include <iostream>
5#include <vector>
6#include <queue>
7#include "../symphony.h"
8
9
10
14class Task {
15public:
16 Task(std::string name, int priority, int deadline)
18
19 bool operator==(const Task &other) const {
20 return name == other.name && priority == other.priority && deadline == other.deadline;
21 }
22
23 std::string name;
26};
27
31class TaskSchedulerState : public State {
32public:
34 tasks = {
35 Task("Task 1", 3, 5),
36 Task("Task 2", 2, 3),
37 Task("Task 3", 5, 10)
38 };
39 }
41 tasks = other.tasks;
42 }
43 TaskSchedulerState(std::vector<Task> tasks) : tasks(tasks) {}
44 std::vector<Task> tasks;
45 void print() override {
46 for (const auto &task : tasks) {
47 std::cout << "Task: " << task.name << ", Priority: " << task.priority << ", Deadline: " << task.deadline << std::endl;
48 }
49 }
50};
51
52
53class TaskScheduler : public Problem {
59public:
65 bool goal_test(State *state) override {
66 auto *scheduler_state = dynamic_cast<TaskSchedulerState *>(state);
67 return scheduler_state && scheduler_state->tasks.empty();
68 }
69 std::vector<std::shared_ptr<Action>> actions(std::shared_ptr<State> state) override {
70 auto scheduler_state = std::dynamic_pointer_cast<TaskSchedulerState>(state);
71 std::vector<std::shared_ptr<Action>> actions;
72
73 for (const auto &task : scheduler_state->tasks) {
74 auto new_state = std::make_shared<TaskSchedulerState>(*scheduler_state);
75 // Remove the task from the new state
76 int index = 0;
77 for (const auto &t : new_state->tasks) {
78 if (t == task) {
79 new_state->tasks.erase(new_state->tasks.begin() + index);
80 break;
81 }
82 index++;
83 }
84 actions.push_back(std::make_shared<Action>("Complete " + task.name, 1, state, new_state));
85 }
86
87 return actions;
88 }
89 double heuristic(State *state) override {
90 auto *scheduler_state = dynamic_cast<TaskSchedulerState *>(state);
91 int total_priority = 0;
92 for (const auto &task : scheduler_state->tasks) {
93 total_priority += task.priority;
94 }
95 return total_priority;
96 }
97};
98
99#endif
Represents an abstract problem that needs to be solved.
Definition definitions.h:63
State * initial_state_
Pointer to the initial state of the problem.
Represents an abstract state in a problem.
Definition definitions.h:18
Represents the state of the task scheduler problem.
void print() override
Prints the state details.
std::vector< Task > tasks
TaskSchedulerState(const TaskSchedulerState &other)
TaskSchedulerState(std::vector< Task > tasks)
bool goal_test(State *state) override
Tests if a given state satisfies the goal condition.
TaskScheduler()
Represents the task scheduler problem. This class defines the initial state, goal test,...
std::vector< std::shared_ptr< Action > > actions(std::shared_ptr< State > state) override
Retrieves the set of actions applicable to a given state.
double heuristic(State *state) override
Computes a heuristic estimate for a given state.
Represents a task with a name, priority, and deadline.
Task(std::string name, int priority, int deadline)
std::string name
int priority
int deadline
bool operator==(const Task &other) const