Symphony 1.0
Loading...
Searching...
No Matches
vacuum.h
Go to the documentation of this file.
1//
2// Created by velocitatem on 12/16/24.
3//
4
5
6#ifndef VACUUM_H
7#define VACUUM_H
8
9#include <iostream>
10#include "../symphony.h"
11
12
13
14
18class VacuumState : public State {
19public:
20 VacuumState() : x(0), dirty0(true), dirty1(true) {}
21 VacuumState(int x, bool dirty0, bool dirty1) : x(x), dirty0(dirty0), dirty1(dirty1) {}
22 int x;
23 bool dirty0;
24 bool dirty1;
25 void print() override {
26 std::cout << "VacuumState(" << x << ", " << dirty0 << ", " << dirty1 << ")" << std::endl;
27 }
28};
29
30
36class VacuumCleaner : public Problem {
37public:
43 bool goal_test(State *state) override {
44 auto *vacuum_state = dynamic_cast<VacuumState *>(state);
46 if (vacuum_state == nullptr) {
47 return false;
48 }
49 return !vacuum_state->dirty0 && !vacuum_state->dirty1;
50 }
56 std::vector<std::shared_ptr<Action>> actions(std::shared_ptr<State> state) override {
57 auto vacuum_state = std::dynamic_pointer_cast<VacuumState>(state);
58 std::vector<std::shared_ptr<Action>> actions;
59
60 auto create_state = [&](int x, bool dirty0, bool dirty1) {
61 return std::make_shared<VacuumState>(x, dirty0, dirty1);
62 };
63
64 // Add possible actions with smart pointers
65 if (vacuum_state->x == 0) {
66 if (vacuum_state->dirty0) {
67 actions.push_back(std::make_shared<Action>("Suck", 1, state, create_state(0, false, vacuum_state->dirty1)));
68 } else {
69 actions.push_back(std::make_shared<Action>("Right", 1, state, create_state(1, vacuum_state->dirty0, vacuum_state->dirty1)));
70 }
71 } else {
72 if (vacuum_state->dirty1) {
73 actions.push_back(std::make_shared<Action>("Suck", 1, state, create_state(1, vacuum_state->dirty0, false)));
74 } else {
75 actions.push_back(std::make_shared<Action>("Left", 1, state, create_state(0, vacuum_state->dirty0, vacuum_state->dirty1)));
76 }
77 }
78
79 return actions;
80 }
81
87 double heuristic(State *state) override {
88 auto *vacuum_state = dynamic_cast<VacuumState *>(state);
89 return vacuum_state->dirty0 + vacuum_state->dirty1;
90 }
91};
92
93
94#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 vacuum cleaner problem. This class defines the initial state, goal test,...
Definition vacuum.h:36
~VacuumCleaner()
Definition vacuum.h:41
bool goal_test(State *state) override
Tests if a given state satisfies the goal condition.
Definition vacuum.h:43
double heuristic(State *state) override
Returns the heuristic value for the given state.
Definition vacuum.h:87
VacuumCleaner()
Definition vacuum.h:38
std::vector< std::shared_ptr< Action > > actions(std::shared_ptr< State > state) override
Returns the possible actions for the given state.
Definition vacuum.h:56
Represents the state of the vacuum cleaner problem.
Definition vacuum.h:18
bool dirty0
Definition vacuum.h:23
VacuumState(int x, bool dirty0, bool dirty1)
Definition vacuum.h:21
bool dirty1
Definition vacuum.h:24
void print() override
Prints the state details.
Definition vacuum.h:25
VacuumState()
Definition vacuum.h:20