Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++: Initial C++ implementation of unit 2
- Loading branch information
1 parent
c1c821a
commit 7186c09
Showing
8 changed files
with
409 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
Oops, something went wrong.