Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

C++: Initial C++ implementation of unit 2

  • Loading branch information...
commit 7186c09e93b3f0f0da99d934c03c49ae88435b0f 1 parent c1c821a
@miguelbernadi miguelbernadi authored
View
8 .gitignore
@@ -7,6 +7,7 @@
*.o
*.so
*.pyc
+a.out
# Packages #
############
@@ -32,4 +33,9 @@
.DS_Store*
ehthumbs.db
Icon?
-Thumbs.db
+Thumbs.db
+
+# Editor generated files #
+##########################
+*.swp
+*~
View
11 unit02/c++/README.markdown
@@ -0,0 +1,11 @@
+C++ Implementation - Miguel Bernabeu Diaz < miguel.bernadi[AT]gmail[DOT]com >
+
+This project is based on the 2011 edition of "Stanford Online Introduction to Artificial Intelligence" (www.ai-class.com).
+The code of the example.cpp file is Public Domain. All other code is licensed under MIT License (http://www.opensource.org/licenses/mit-license.php).
+
+Algorithms:
+ * Breadth First Search (BFS) => Implemented
+ * Uniform Cost Search (UCS)
+ * Cheapest First Search (CFS)
+ * Depth First Search (DFS)
+ * A\* Search (ASS)
View
76 unit02/c++/Tree.h
@@ -0,0 +1,76 @@
+/*
+ * The MIT License (MIT)
+ * Copyright (c) 2011 Miguel Bernabeu Diaz <miguel.bernadi[AT]gmail[DOT]com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+ * associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+ * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+ * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef Tree_H_
+#define Tree_H_
+
+#include <list>
+
+/**
+ * Class representing a Binary Tree.
+ */
+template<typename TreeNode>
+class Tree
+{
+ public:
+ typedef TreeNode value_type; ///< The type of the nodes.
+ typedef std::list< value_type* > TreeNodeList;
+
+ Tree() : _rootNode(0) { }
+ ~Tree() { delete _NodeList; }
+ value_type* getRootPtr() { return _rootNode; }
+ void setRoot(value_type* rootNode) { _rootNode = rootNode; _NodeList.push_back( _rootNode ); }
+ void addNode(value_type* Node) { _NodeList.push_back(Node); }
+
+ private:
+ TreeNodeList _NodeList;
+ value_type* _rootNode;
+
+};
+
+/**
+ * Class representing a Node of the Tree.
+ * @param _Data is the information structure stored in the node.
+ */
+template<typename _Data, int nChild = 2>
+class TreeNode
+{
+ private:
+
+ public:
+ typedef _Data value_type;
+ int child_number;
+
+ /// Node contained information.
+ value_type Data;
+ /// Pointer to parent node
+ TreeNode<_Data>* parent;
+ /// Children of the Node
+ TreeNode<_Data>* child[nChild];
+
+ TreeNode(value_type Data) : child_number(nChild), Data(Data), parent(0) { }
+ ~TreeNode() { for(int i = 0; i < child_number; ++i) delete child[i]; }
+
+ int has_children() { int childs = 0; for(int i = 0; i < child_number; ++i) { if(child[i] != 0) ++childs; } return childs; }
+
+};// end of BinaryTreeNode definition
+
+#endif
+
View
97 unit02/c++/TreeSearch.h
@@ -0,0 +1,97 @@
+/*
+ * The MIT License (MIT)
+ * Copyright (c) 2011 Miguel Bernabeu Diaz <miguel.bernadi[AT]gmail[DOT]com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+ * associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+ * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+ * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef TreeSearch_H_
+#define TreeSearch_H_
+
+#include <list>
+#include <set>
+#include <exception>
+
+#include "Tree.h"
+#include "algorithms/Algorithms.h"
+
+template <typename TreeType, typename SearchAlgorithm/* = BreadthFirstSearch<TreeType>*/ >
+class TreeSearch
+{
+ public:
+ typedef std::list< Path<TreeType>* > PathList;
+ typedef typename TreeType::value_type::value_type GoalType;
+
+
+ PathList _frontier;
+ std::set<typename TreeType::value_type*> _explored;
+ typename TreeType::value_type* TreeRoot;
+ PathList LPath;
+
+ TreeSearch(typename TreeType::value_type* mNode) : TreeRoot( mNode ) { _frontier.push_back( new Path<TreeType>(TreeRoot) ); }
+ Path<TreeType>* find(GoalType Goal)
+ {
+ /*
+ Notes: A compare function must be specified to the set container.
+
+ Pseudocode:
+ Function TreeSearch(problem):
+ frontier = {[initial]}
+ loop:
+ if frontier is empty: FAIL
+ path = remove_choice(frontier)
+ s = path.end
+ if s is a goal: return path
+ for a in actions:
+ add [path + a -> Result(s, a)] to frontier
+ */
+
+ Path<TreeType>* path;
+ typename TreeType::value_type* Node;
+// Tries using SearchAlgorithm as holder of the actual algorithm implementation.
+// SearchAlgorithm sa();
+// return sa.find(Goal);
+ while(true)
+ {
+ if(_frontier.empty()) throw( std::exception() );
+ path = _frontier.front();
+ _frontier.remove(path);
+ Node = path->_path.back();
+ _explored.insert(Node);
+ if (Node->Data == Goal) return path;
+
+ // Add the checked Nodes children to a copy of the present path
+ // and add this path to _frontier.
+ if( Node->has_children() != 0 )
+ {
+ for(int i = 0; i < Node->child_number; ++i)
+ {
+ // Check if the child to be attached has been explored already
+ if( _explored.find(Node->child[i]) == _explored.end() )
+ {
+ Path<TreeType>* newpath = new Path<TreeType>(*path);
+ newpath->_path.push_back(Node->child[i]);
+ _frontier.push_back(newpath);
+ }
+ }
+ _frontier.sort();
+ }
+ }
+ }
+};
+
+#endif
+
View
31 unit02/c++/algorithms/Algorithms.h
@@ -0,0 +1,31 @@
+/*
+ * The MIT License (MIT)
+ * Copyright (c) 2011 Miguel Bernabeu Diaz <miguel.bernadi[AT]gmail[DOT]com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+ * associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+ * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+ * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef Algorithms_H_
+#define Algorithms_H_
+
+#include "BreadthFirstSearch.h"
+
+class UniformCostSearch; // UCS
+class CheapestFirstSearch; // CFS
+class DepthFirstSearch; // DFS
+class AStarSearch; // ASS
+
+#endif
View
87 unit02/c++/algorithms/BreadthFirstSearch.h
@@ -0,0 +1,87 @@
+/*
+ * The MIT License (MIT)
+ * Copyright (c) 2011 Miguel Bernabeu Diaz <miguel.bernadi[AT]gmail[DOT]com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+ * associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+ * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+ * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef BreadthFirstSearch_H_
+#define BreadthFirstSearch_H_
+
+#include "Path.h"
+
+template <typename TreeType>
+class BreadthFirstSearch // BFS
+{
+ public:
+ typedef std::list< Path<TreeType>* > PathList;
+ PathList _frontier;
+ typename std::set<typename TreeType::value_type*> _explored;
+ typename TreeType::value_type* TreeRoot;
+ PathList LPath;
+
+
+ BreadthFirstSearch() { }
+ Path<TreeType>* find( typename TreeType::value_type::value_type Goal)
+ {
+ Path<TreeType>* path;
+ typename TreeType::value_type* Node;
+ /*
+ * Notes: A compare function must be specified to the set container.
+ *
+ * Pseudocode:
+ * Function TreeSearch(problem):
+ * frontier = {[initial]}
+ * loop:
+ * if frontier is empty: FAIL
+ * path = remove_choice(frontier)
+ * s = path.end
+ * if s is a goal: return path
+ * for a in actions:
+ * add [path + a -> Result(s, a)] to frontier
+ */
+ while(true)
+ {
+ if(_frontier.empty()) throw( std::exception() );
+ path = _frontier.front();
+ _frontier.remove(path);
+ Node = path->_path.back();
+ _explored.insert(Node);
+ if (Node->Data == Goal) return path;
+
+ // Add the checked Nodes children to a copy of the present path
+ // and add this path to _frontier.
+ if( Node->has_children() != 0 )
+ {
+ for(int i = 0; i < Node->child_number; ++i)
+ {
+ // Check if the child to be attached has been explored already
+ if( _explored.find(Node->child[i]) == _explored.end() )
+ {
+ Path<TreeType>* newpath = new Path<TreeType>(*path);
+ newpath->_path.push_back(Node->child[i]);
+ _frontier.push_back(newpath);
+ }
+ }
+ _frontier.sort();
+ }
+ }
+
+ }
+};
+
+#endif
+
View
41 unit02/c++/algorithms/Path.h
@@ -0,0 +1,41 @@
+/*
+ * The MIT License (MIT)
+ * Copyright (c) 2011 Miguel Bernabeu Diaz <miguel.bernadi[AT]gmail[DOT]com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+ * associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+ * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+ * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef Path_H_
+#define Path_H_
+
+template <typename TreeType>
+class Path
+{
+ public:
+ typename TreeType::TreeNodeList _path;
+
+ Path() { }
+ Path(typename TreeType::value_type* Node) { addToPath(Node); }
+
+// Path(const Path<TreeType>& orig) { this->_path = orig._path; }
+
+ void addToPath(typename TreeType::value_type* Node) { _path.push_back(Node); }
+
+ //add comparison operator operator< for sorting by length.
+};
+
+#endif
+
View
59 unit02/c++/example.cpp
@@ -0,0 +1,59 @@
+/*
+ * The code contained in this file is licensed as Public Domain.
+ */
+
+/*
+ * Example of usage of headers Tree.h and TreeSearch.h
+ */
+
+#include <string>
+
+#include "Tree.h"
+#include "TreeSearch.h"
+
+/*
+ * This class represents the information stored in a node of the tree.
+ * It may contain lots of information but must implement an equality
+ * operator (operator==) to detect when the goal is achieved.
+ */
+class Info
+{
+ public:
+ std::string Id;
+ int Count;
+
+ Info() { }
+ Info(std::string Id, int Count) : Id(Id), Count(Count) { }
+
+ // operator== is a must (used for goal)
+ bool operator==(const Info &other) const
+ {
+ if(this->Id == other.Id) return true;
+ return false;
+ }
+};
+
+// Helpers for less mistakes and easier reading.
+typedef TreeNode<Info, 2> myNodeType;
+typedef Tree<myNodeType> myTreeType;
+
+int main()
+{
+ // Build the tree
+ myTreeType* mTree = new myTreeType();
+ mTree->setRoot( new myNodeType( Info("Root", 1) ) );
+ myNodeType* mNode = mTree->getRootPtr();
+ mNode->child[0] = new myNodeType( Info("ChildOne", 1) );
+ mTree->addNode(mNode->child[0]);
+
+ // Set the starting point for a search.
+ TreeSearch<myTreeType, BreadthFirstSearch<myTreeType> >* mSearch = new TreeSearch<myTreeType, BreadthFirstSearch<myTreeType> >( mTree->getRootPtr() );
+
+ /*
+ * mPath is guaranteed to be the shortest path between the starting point and
+ * the goal point.
+ */
+ Path<myTreeType>* mPath = mSearch->find( Info("ChildOne", 1) );
+
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.