理工大学信息工程与自动化学院学生实验报告( 2013 — 2014 学年第 1 学期)课程名称:人工智能开课实验室:信自楼445 2013 年12月 20日一、上机目的及容1.上机容用确定性推理算法求解教材65-66页介绍的八数码难题。
2.上机目的(1)复习程序设计和数据结构课程的相关知识,实现课程间的平滑过渡;(2)掌握并实现在小规模状态空间中进行图搜索的方法;(3)理解并掌握图搜索的技术要点。
二、实验原理及基本技术路线图(方框原理图或程序流程图)(1)设计并实现程序,求解出正确的解答路径;(2)对所设计的算法采用大O符号进行时间复杂性和空间复杂性分析;(3)对一般图搜索的技术要点和技术难点进行评述性分析。
三、所用仪器、材料(设备名称、型号、规格等或使用软件)1台PC及VISUAL C++6.0软件四、实验方法、步骤(或:程序代码或操作过程)建立工程后建立5个source Files文件分别为1.AttributeValue.cpp#include "AttributeValue.h"#include "base.h"AttributeValue::AttributeValue(std::string const& instring) : m_value(instring){}bool AttributeValue::GetType(){if (m_value == "P"){return true;}else if (m_value == "N"){return false;}else{throw DataErrException();}}2.basefun.cpp#include <math.h>float log2 (float x){return 1.0 / log10(2) * log10(x);}float calEntropy(float prob){float sum=0;if (prob == 0 || prob == 1){return 0;}sum -= prob * log2(prob);sum -= (1 - prob) * log2 ( 1 - prob );return sum;3.DataPoint.cpp#include <iostream>#include "DataPoint.h"DataPoint::DataPoint(std::vector<AttributeValue> const& attributes, bool type) : m_type(type){for (int i=0; i<attributes.size(); ++i){m_attributes.push_back( attributes[i] );}}void DataPoint::display(){for (int i=0; i<m_attributes.size(); ++i){std::cout << "\t" << m_attributes[i].getValue();}if (true == m_type){std::cout << "\tP";}else{std::cout << "\tN";}std::cout << std::endl;}4.DataSet.cpp5.main.cpp#include <fstream>#include <iostream>#include <list>#include <sstream>#include <string>#include <vector>#include "AttributeValue.h"#include "DataPoint.h"#include "DataSet.h"DataPoint processLine(std::string const& sLine){std::istringstream isLine(sLine, std::istringstream::in);std::vector<AttributeValue> attributes;// TODO: need to handle beginning and ending empty spaces.while( isLine.good() ){std::string rawfield;isLine >> rawfield;attributes.push_back( AttributeValue( rawfield ) );}AttributeValue v = attributes.back();attributes.pop_back();bool type = v.GetType();return DataPoint(attributes, type);}void main(){std::ifstream ifs("in.txt", std::ifstream::in);DataSet initDataset;while( ifs.good() ){// TODO: need to handle empty lines.std::string sLine;std::getline(ifs, sLine);initDataset.addDataPoint( processLine(sLine) );}std::list<DataSet> processQ;std::vector<DataSet> finishedDataSet;processQ.push_back(initDataset);while ( processQ.size() > 0 ){std::vector<DataSet> splittedDataSets;DataSet dataset = processQ.front();dataset.splitDataSet(splittedDataSets);processQ.pop_front();for (int i=0; i<splittedDataSets.size(); ++i){float prob = splittedDataSets[i].getPositiveProb();if (prob == 0.0 || prob == 1.0){finishedDataSet.push_back(splittedDataSets[i]);}else{processQ.push_back(splittedDataSets[i]);}}}std::cout << "The dicision tree is:" << std::endl;for (int i = 0; i < finishedDataSet.size(); ++i){finishedDataSet[i].display();}}建立4个Header Files文件1.AttributeValue.h#ifndef ATTRIBUTE_VALUE_H_#define ATTRIBUTE_VALUE_H_#include <string>class AttributeValue{public:AttributeValue(std::string const& instring);bool GetType();std::string const& getValue() const{return m_value;}private:std::string m_value;};struct AttributeValueCmp{bool operator() (AttributeValue const& lhs, AttributeValue const& rhs) const {return lhs.getValue() < rhs.getValue();}};#endif2.base.hclass DataErrException : public std::exception{};float calEntropy(float prob);3.DatePoint.h#ifndef DATA_POINT_H_#define DATA_POINT_H_#include <vector>#include "AttributeValue.h"class DataPoint{public:DataPoint(std::vector<AttributeValue> const& attributes, bool type);bool isPositive(){return m_type;}int getNAttributes(){return m_attributes.size();}AttributeValue const& getAttribute(int index){return m_attributes[index];}void display();private:std::vector<AttributeValue> m_attributes;bool m_type;};#endif4.DateSet.h#include <map>#include <utility>#include "DataPoint.h"class SplitAttributeValue{public:SplitAttributeValue(AttributeValue v, int id): m_v(v), m_attributeIndex(id){}int getAttributeIndex(){return m_attributeIndex;}void display();private:int m_attributeIndex;AttributeValue m_v;};class DataSet{public:void addDataPoint(DataPoint const& datapoint);float getPositiveProb();void splitDataSet(std::vector<DataSet>& splittedSets);void display();private:std::vector<SplitAttributeValue> m_splitAttributes;std::vector<DataPoint> m_data;};五、实验过程原始记录( 测试数据、图表、计算等)实验运行截图六、实验结果、分析和结论(误差分析与数据处理、成果总结等。