Navigation Menu

Skip to content

Commit

Permalink
C++: Initial C++ implementation of unit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbernadi committed Oct 17, 2011
1 parent c1c821a commit 7186c09
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .gitignore
Expand Up @@ -7,6 +7,7 @@
*.o
*.so
*.pyc
a.out

# Packages #
############
Expand All @@ -32,4 +33,9 @@
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
Thumbs.db

# Editor generated files #
##########################
*.swp
*~
11 changes: 11 additions & 0 deletions 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)
76 changes: 76 additions & 0 deletions 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

97 changes: 97 additions & 0 deletions 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

31 changes: 31 additions & 0 deletions 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
87 changes: 87 additions & 0 deletions 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

41 changes: 41 additions & 0 deletions 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

0 comments on commit 7186c09

Please sign in to comment.