Skip to content

Commit

Permalink
lab1 done
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtnikolaev committed Oct 26, 2021
1 parent 539d139 commit 6218a09
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ai_lab1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake-build-debug/
cmake-build-release/
.idea/
6 changes: 6 additions & 0 deletions ai_lab1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(ai_lab1)

set(CMAKE_CXX_STANDARD 20)

add_executable(ai_lab1 src/main.cpp src/tag.cpp src/tag.h src/tree.cpp src/tree.h src/deep.cpp src/deep.h src/conditions.h src/iterative.cpp src/iterative.h src/host.cpp src/host.h src/fabric.cpp src/fabric.h)
22 changes: 22 additions & 0 deletions ai_lab1/src/conditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef AI_LAB1_CONDITIONS_H
#define AI_LAB1_CONDITIONS_H

#include "tag.h"

struct Conditions {
constexpr static field_t init_field {{
{0, 4, 3},
{6, 2, 1},
{7, 5, 8}
}};
constexpr static field_t target_field {{
{1, 2, 3},
{4, 0, 5},
{6, 7, 8}
}};

const TagState init_state = TagState(init_field, {0, 0});
const TagState target_state = TagState(target_field, {1, 1});
};

#endif //AI_LAB1_CONDITIONS_H
66 changes: 66 additions & 0 deletions ai_lab1/src/deep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "deep.h"

DeepTree::DeepTree(const TagState& initial_state) : Tree(initial_state) {}

bool DeepTree::BuildSolvingTree(const TagState &target) {
stack_.push(root_);
unique_states_.push_back(root_->state_);

while (!stack_.empty()) {
auto curr_node = stack_.top();
stack_.pop();

host_->PrintCurrentNode(curr_node->state_, curr_node->depth_);
host_->LogIteration();

if (curr_node->state_ == target) {
host_->PrintSolutionFound(true);
solution_ = curr_node;
return true;
}
host_->PrintSolutionFound(false);

bool opened = OpenNode(curr_node);

if (!stack_.empty()) {
auto next_node = stack_.top();
host_->PrintStackState(next_node->state_,
next_node->depth_,
(int)stack_.size());
host_->Interrupt();
if (!opened) {
CleanBranchUp(curr_node, next_node->parent_);
}
}
else {
host_->PrintStackState(curr_node->state_,
curr_node->depth_,
(int)stack_.size());
host_->Interrupt();
}
}

unique_states_.clear();

return false;
}

bool DeepTree::OpenNode(Tree::Node *node) {
auto result = false;
auto next_states = node->state_.NextStates();
for (auto state : next_states) {
if (!UniqueCheck(state)) {
host_->PrintNewNode(state);
auto new_node = new Node(state, node);
host_->LogNew();
stack_.push(new_node);
unique_states_.push_back(state);
result = true;
}
else {
host_->PrintRepeatedNode(state);
}
}

return result;
}
15 changes: 15 additions & 0 deletions ai_lab1/src/deep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef AI_LAB1_DEEP_H
#define AI_LAB1_DEEP_H

#include "tree.h"

class DeepTree : public Tree {
public:
explicit DeepTree(const TagState& initial_state);
bool BuildSolvingTree(const TagState& target) override;
protected:
bool OpenNode(Node* node) override;
};


#endif //AI_LAB1_DEEP_H
27 changes: 27 additions & 0 deletions ai_lab1/src/fabric.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "fabric.h"
#include "deep.h"
#include "iterative.h"

TreeFabric::TreeFabric(const TreeType& tree_t, const HostType& host_t) : tree_t_(tree_t), host_t_(host_t) {}

std::unique_ptr<Tree> TreeFabric::BuildTreeObj(const TagState& initial_state) {
Tree* tree;

if (tree_t_ == TreeType::Deep) {
tree = new DeepTree(initial_state);
}
if (tree_t_ == TreeType::Iterative) {
tree = new IterativeTree(initial_state);
}

if (host_t_ == HostType::Debug) {
tree->SetHost(new DebugHost());

}
if (host_t_ == HostType::Solution) {
tree->SetHost(new SolutionHost());
}

return std::unique_ptr<Tree>(tree);
}

27 changes: 27 additions & 0 deletions ai_lab1/src/fabric.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef AI_LAB1_FABRIC_H
#define AI_LAB1_FABRIC_H

#include <memory>
#include "tree.h"

enum class TreeType {
Deep,
Iterative,
};

enum class HostType {
Debug,
Solution,
};

class TreeFabric {
public:
explicit TreeFabric(const TreeType& tree_t, const HostType& host_t);
std::unique_ptr<Tree> BuildTreeObj(const TagState& initial_state);
private:
TreeType tree_t_;
HostType host_t_;
};


#endif //AI_LAB1_FABRIC_H
77 changes: 77 additions & 0 deletions ai_lab1/src/host.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "host.h"

#include <iostream>

void DebugHost::PrintCurrentNode(const TagState& state, int depth) const {
std::cout << "*** Current Node: ***\n";
std::cout << "(depth: " << depth << ")\n";
std::cout << state.ToString() << std::endl;
}

void DebugHost::Interrupt() const {
getchar();
}

void DebugHost::PrintNewNode(const TagState &state) const {
std::cout << "Child Node: \n";
std::cout << state.ToString() << std::endl;
}

void DebugHost::PrintSolutionFound(bool found) const {
if (found) {
std::cout << "[it is the target state!]\n" << std::endl;
}
else {
std::cout << "[isn't target]\n" << std::endl;
}
}

void DebugHost::PrintRepeatedNode(const TagState &state) const {
std::cout << "Repeated Child Node: \n";
std::cout << "(will not going to stack) \n";
std::cout << state.ToString() << std::endl;
}

void DebugHost::PrintStackState(const TagState &top_state, int depth, int stack_size) const {
if (stack_size == 0) {
std::cout << "** Now Opening Stack is empty! **\n";
std::cout << "-------------------------------------------------" << std::endl;
return;
}
std::cout << "Now in Opening Stack: " << stack_size << " nodes.\n";
std::cout << "Next Node to open: \n";
std::cout << "(depth: " << depth << ")\n";
std::cout << top_state.ToString();
std::cout << "-------------------------------------------------" << std::endl;
}

void SolutionHost::LogIteration() {
time_compl_++;
}

void SolutionHost::LogNew() {
curr_mem_++;
if (curr_mem_ > memory_compl_) {
memory_compl_ = curr_mem_;
}
}

void SolutionHost::LogDelete() {
curr_mem_--;
}

void SolutionHost::PrintSolutionNode(const TagState &state, int depth) const {
std::cout << "(depth: " << depth << ")\n";
std::cout << state.ToString() << std::endl;
}

void SolutionHost::PrintComplexity(int depth) const {
std::cout << "*** Solution was found! ***\n";
std::cout << "Depth: " << depth << "\n";
std::cout << "Time complexity: " << time_compl_ << " steps\n";
std::cout << "Memory complexity: " << memory_compl_ << " memory units\n" << std::endl;

if (depth > kMaxSolutionPath) {
std::cout << "[too deep to print solution path]" << std::endl;
}
}
50 changes: 50 additions & 0 deletions ai_lab1/src/host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef AI_LAB1_HOST_H
#define AI_LAB1_HOST_H

#include "tag.h"

constexpr static int kMaxSolutionPath = 100;

class Host {
public:
virtual ~Host() = default;
virtual void Interrupt() const {};
virtual void PrintNewNode(const TagState& state) const {};
virtual void PrintRepeatedNode(const TagState& state) const {};
virtual void PrintStackState(const TagState& top_state, int depth, int stack_size) const {};
virtual void PrintCurrentNode(const TagState& state, int depth) const {};
virtual void PrintSolutionFound(bool found) const {};
virtual void LogIteration() {};
virtual void LogNew() {};
virtual void LogDelete() {};
virtual void PrintSolutionNode(const TagState& state, int depth) const {};
virtual void PrintComplexity(int depth) const {};
protected:
Host() = default;
};

class DebugHost : public Host {
public:
void Interrupt() const override;
void PrintCurrentNode(const TagState& state, int depth) const override;
void PrintNewNode(const TagState& state) const override;
void PrintSolutionFound(bool found) const override;
void PrintRepeatedNode(const TagState& state) const override;
void PrintStackState(const TagState& top_state, int depth, int stack_size) const override;
};

class SolutionHost : public Host {
public:
void PrintSolutionNode(const TagState& state, int depth) const override;
void PrintComplexity(int depth) const override;
void LogIteration() override;
void LogNew() override;
void LogDelete() override;
private:
int time_compl_ = 0;
int memory_compl_ = 1;
int curr_mem_ = 1;
};


#endif //AI_LAB1_HOST_H
24 changes: 24 additions & 0 deletions ai_lab1/src/iterative.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "iterative.h"

IterativeTree::IterativeTree(const TagState &initial_state) : DeepTree(initial_state) {}

bool IterativeTree::BuildSolvingTree(const TagState &target) {
constexpr int k_max_depth = 362880;
for (auto i = 0; i < k_max_depth; ++i) {
depth_limit_ = i;
bool built = DeepTree::BuildSolvingTree(target);
if (built) {
return true;
}
}
return false;
}

bool IterativeTree::OpenNode(Tree::Node *node) {
if (node->depth_ >= depth_limit_) {
return false;
}

return DeepTree::OpenNode(node);
}

17 changes: 17 additions & 0 deletions ai_lab1/src/iterative.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef AI_LAB1_ITERATIVE_H
#define AI_LAB1_ITERATIVE_H

#include "deep.h"

class IterativeTree : public DeepTree {
public:
explicit IterativeTree(const TagState& initial_state);
bool BuildSolvingTree(const TagState& target) override;

protected:
int depth_limit_ = 0;
bool OpenNode(Node* node) override;
};


#endif //AI_LAB1_ITERATIVE_H
50 changes: 50 additions & 0 deletions ai_lab1/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "fabric.h"
#include "conditions.h"
#include <iostream>

int main() {
Conditions c;
TreeType tree_t;
HostType host_t;
int choice;

std::cout << "Initial state: " << std::endl;
std::cout << c.init_state.ToString() << std::endl;
std::cout << "Target state: " << std::endl;
std::cout << c.target_state.ToString() << std::endl;

std::cout << "-------------------------------------" << std::endl;
std::cout << " 1 - Depth-First Search " << std::endl;
std::cout << " 2 - Iterative Depth Search " << std::endl;
std::cout << std::endl;
std::cout << " Enter selection, 0 to quit: ";
std::cin >> choice;
getchar();
std::cout << std::endl;

if (choice == 0) return 0;
if (choice == 1) tree_t = TreeType::Deep;
if (choice == 2) tree_t = TreeType::Iterative;

std::cout << "-------------------------------------" << std::endl;
std::cout << " 1 - Debug Mode " << std::endl;
std::cout << " 2 - Solution Mode " << std::endl;
std::cout << std::endl;
std::cout << " Enter selection, 0 to quit: ";
std::cin >> choice;
getchar();
std::cout << std::endl;

if (choice == 0) return 0;
if (choice == 1) host_t = HostType::Debug;
if (choice == 2) host_t = HostType::Solution;

auto fabric = TreeFabric(tree_t, host_t);
auto tree = fabric.BuildTreeObj(c.init_state);
tree->BuildSolvingTree(c.target_state);
tree->PrintSolution();

std::cout << "Press ENTER to exit ..." << std::endl;
getchar();
return 0;
}
Loading

0 comments on commit 6218a09

Please sign in to comment.