<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>ID3</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp; 
 
</b></span><b>&nbsp;&nbsp;&nbsp; <span lang="en-ca">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
ID3--simple test</span></b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is just a simple demo of ID3 algorithms in AI. It is actually a top-down decision tree construction program. However,</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>I just tried the very simple first step because I am really hungry after 9:00pm.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <span lang="en-ca"><b>If you are given a database, can you do some induction 
	from the data? One way is by this very old decision-tree algorithms. It is 
	like this:</b></span><p><img border="0" src="picture/ID3.ht1.gif" width="637" height="285"></p>
	<p>Alternate: another suitable restaurant nearby<br>
	Bar: comfortable bar for waiting<br>
	Fri/Sat: true on Fridays and Saturdays<br>
	Hungry: whether one is hungry<br>
	Patrons: how many people are present (none, some, full)<br>
	Price: price range ($, $$, $$$)<br>
	Raining: raining outside<br>
	Reservation: reservation made<br>
	Type: kind of restaurant (French, Italian, Thai, Burger)<br>
	WaitEstimate: estimated wait by host (0-10 mins, 10-30, 30-60,</p>
	<p><span lang="en-ca">You are asked a question &quot;under what circumstance 
	would you decide to wait&quot;?</span></p>
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>This is a rather straight 
forward, simple, trivial partition problem. However, it gives me a good chance 
of practicing STL.</b></font></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">1. ID3.</span></b></font><span lang="en-ca"><font size="3"><b>cpp</b></font></span></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: ID3.cpp</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;set&gt;
#include &lt;string&gt;

using namespace std;

const int DataCount=12;
const int CategoryCount=10;
const int GoalFeatureIndex=CategoryCount;

char* propertyName[CategoryCount]=
{
	&quot;alternative&quot;, &quot;bar&quot;, &quot;Friday&quot;, &quot;hungry&quot;, &quot;patron&quot;, &quot;price&quot;,
	&quot;rain&quot;, &quot;reservation&quot;, &quot;type&quot;, &quot;estimate&quot;
};

typedef  vector&lt;string&gt; PropertySet;
typedef set&lt;string&gt; Property;

typedef set&lt;PropertySet&gt; DataSet;

//typedef set&lt;PropertySet&gt; Component;

typedef set&lt;DataSet&gt; Partition;


char* rawData[DataCount][CategoryCount+1]=
{
	{&quot;yes&quot;, &quot;no&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;some&quot;, &quot;$$$&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;french&quot;, &quot;0-10&quot;, &quot;yes&quot;},
	{&quot;yes&quot;, &quot;no&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;full&quot;, &quot;$&quot;, &quot;no&quot;, &quot;no&quot;, &quot;thai&quot;, &quot;30-60&quot;, &quot;no&quot;},
	{&quot;no&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;no&quot;, &quot;some&quot;, &quot;$&quot;, &quot;no&quot;, &quot;no&quot;, &quot;burger&quot;, &quot;0-10&quot;, &quot;yes&quot;},
	{&quot;yes&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;full&quot;, &quot;$&quot;, &quot;no&quot;, &quot;no&quot;, &quot;thai&quot;, &quot;10-30&quot;, &quot;yes&quot;},
	
	{&quot;yes&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;full&quot;, &quot;$$$&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;french&quot;, &quot;&gt;60&quot;, &quot;no&quot;},
	{&quot;no&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;some&quot;, &quot;$$&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;italian&quot;, &quot;0-10&quot;, &quot;yes&quot;},
	{&quot;no&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;no&quot;, &quot;none&quot;, &quot;$&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;burger&quot;, &quot;0-10&quot;, &quot;no&quot;},
	{&quot;no&quot;, &quot;no&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;some&quot;, &quot;$$&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;thai&quot;, &quot;0-10&quot;, &quot;yes&quot;},
	
	{&quot;no&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;full&quot;, &quot;$&quot;, &quot;yes&quot;, &quot;no&quot;, &quot;burger&quot;, &quot;&gt;60&quot;, &quot;no&quot;},
	{&quot;yes&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;full&quot;, &quot;$$$&quot;, &quot;no&quot;, &quot;yes&quot;, &quot;italian&quot;, &quot;10-30&quot;, &quot;no&quot;},
	
	{&quot;no&quot;, &quot;no&quot;, &quot;no&quot;, &quot;no&quot;, &quot;none&quot;, &quot;$&quot;, &quot;no&quot;, &quot;no&quot;, &quot;thai&quot;, &quot;0-10&quot;, &quot;no&quot;},
	{&quot;yes&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;yes&quot;, &quot;full&quot;, &quot;$&quot;, &quot;no&quot;, &quot;no&quot;, &quot;burger&quot;, &quot;30-60&quot;, &quot;yes&quot;}
};

void initialize(DataSet&amp; dataset);
bool isUniform(const DataSet&amp; dataset);
Partition decomposite(const DataSet&amp; theData, int index);
//bool newPropertyValue(Property&amp; property, const string&amp; value);
void addPartition(Partition&amp; partition, const PropertySet&amp; propertySet, int index);
void displayProperty(const PropertySet&amp; property);
void displayDataSet(const DataSet&amp; dataset);
void displayPartition(const Partition&amp; partition);

void ID3(const DataSet&amp; dataSet, int* proceedList, int length);

int proceedList[8]={4, 9, 0, 3, 7, 2, 1, 6};

int main()
{
	DataSet dataset;
	initialize(dataset);
	displayDataSet(dataset);
	ID3(dataset, proceedList, 8);
	//displayPartition(decomposite(dataset, 4));
	

	return 0;
}

bool isUniform(const DataSet&amp; dataset)
{
	if (dataset.size()==1)
	{
		return true;
	}
	DataSet::const_iterator it=dataset.begin();
	string result=(*it)[GoalFeatureIndex];
	for (it++; it!=dataset.end(); it++)
	{
		if (result!=(*it)[GoalFeatureIndex])
		{
			return false;
		}
	}
	return true;
}


void ID3(const DataSet&amp; dataSet, int* proceedList, int length)
{
	bool result=false;
	Partition partition;
	if (length==0)
	{
		return ;
	}
	partition=decomposite(dataSet, proceedList[0]);


	
	cout&lt;&lt;&quot;the decomposite on property of &quot;&lt;&lt;propertyName[proceedList[0]]&lt;&lt;endl;
	displayPartition(partition);

	for (Partition::const_iterator it=partition.begin(); it!=partition.end(); it++)
	{
		if (!isUniform((*it)))
		{
			ID3((*it), proceedList+1, length-1);
		}

	}	
}




void initialize(DataSet&amp; dataset)
{
	PropertySet property;
	for (int r=0; r&lt;DataCount; r++)
	{		
		property.clear();
		for (int c=0; c&lt;CategoryCount+1; c++)
		{
			property.push_back(string(rawData[r][c]));
		}
		dataset.insert(property);
	}
}

void displayPartition(const Partition&amp; partition)
{
	cout&lt;&lt;&quot;partition is:\n&quot;;
	for (Partition::const_iterator it=partition.begin(); it!=partition.end(); it++)
	{
		displayDataSet(*it);
	}
}
		
/*			
bool newPropertyValue(Property&amp; property, const string&amp; value)
{
	for (Property::iterator it=property.begin(); it!=property.end(); it++)
	{
		if (*it==value)
		{
			return false;
		}
	}
	property.insert(value);
	return true;
}
*/

void addPartition(Partition&amp; partition, const PropertySet&amp; propertySet, int index)
{
	DataSet newDataSet;
	for (Partition::iterator it=partition.begin(); it!=partition.end(); it++)
	{
		DataSet::iterator ptr=it-&gt;begin();
		if ((*ptr)[index]==propertySet[index])
		{
			(*it).insert(propertySet);
			return;
		}
	}
	newDataSet.insert(propertySet);
	partition.insert(newDataSet);
}




Partition decomposite(const DataSet&amp; theData, int index)
{
	//Property property;
	Partition partition;
	for (DataSet::const_iterator it=theData.begin(); it!=theData.end(); it++)
	{
		//newPropertyValue(property, (*it)[index]);
		
		addPartition(partition, (*it), index);
		
	}
	return partition;
}



	

void displayProperty(const PropertySet&amp; property)
{
	cout&lt;&lt;&quot;(&quot;;
	for (PropertySet::const_iterator it=property.begin(); it!=property.end(); it++)
	{
		cout&lt;&lt;*it&lt;&lt;&quot;, &quot;;
	}
	cout&lt;&lt;&quot;)\n&quot;;
}

void displayDataSet(const DataSet&amp; dataset)
{
	for (int i=0; i&lt;CategoryCount; i++)
	{
		cout&lt;&lt;propertyName[i]&lt;&lt;&quot; &quot;;
	}
	cout&lt;&lt;&quot;goal\n&quot;;
	for (DataSet::const_iterator it=dataset.begin(); it!=dataset.end(); it++)
	{
		displayProperty(*it);
	}
}

</pre>
<pre>กก</pre>
<pre></pre>
<pre></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>How to run?</b></font></span></pre>

<pre>alternative bar Friday hungry patron price rain reservation type estimate goal
(no, no, no, no, none, $, no, no, thai, 0-10, no, )
(no, no, no, yes, some, $$, yes, yes, thai, 0-10, yes, )
(no, yes, no, no, none, $, yes, no, burger, 0-10, no, )
(no, yes, no, no, some, $, no, no, burger, 0-10, yes, )
(no, yes, no, yes, some, $$, yes, yes, italian, 0-10, yes, )
(no, yes, yes, no, full, $, yes, no, burger, &gt;60, no, )
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
(yes, no, no, yes, some, $$$, no, yes, french, 0-10, yes, )
(yes, no, yes, no, full, $$$, no, yes, french, &gt;60, no, )
(yes, no, yes, yes, full, $, no, no, thai, 10-30, yes, )
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
(yes, yes, yes, yes, full, $$$, no, yes, italian, 10-30, no, )
the decomposite on property of patron
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(no, no, no, no, none, $, no, no, thai, 0-10, no, )
(no, yes, no, no, none, $, yes, no, burger, 0-10, no, )
alternative bar Friday hungry patron price rain reservation type estimate goal
(no, no, no, yes, some, $$, yes, yes, thai, 0-10, yes, )
(no, yes, no, no, some, $, no, no, burger, 0-10, yes, )
(no, yes, no, yes, some, $$, yes, yes, italian, 0-10, yes, )
(yes, no, no, yes, some, $$$, no, yes, french, 0-10, yes, )
alternative bar Friday hungry patron price rain reservation type estimate goal
(no, yes, yes, no, full, $, yes, no, burger, &gt;60, no, )
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
(yes, no, yes, no, full, $$$, no, yes, french, &gt;60, no, )
(yes, no, yes, yes, full, $, no, no, thai, 10-30, yes, )
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
(yes, yes, yes, yes, full, $$$, no, yes, italian, 10-30, no, )
the decomposite on property of estimate
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(no, yes, yes, no, full, $, yes, no, burger, &gt;60, no, )
(yes, no, yes, no, full, $$$, no, yes, french, &gt;60, no, )
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, yes, yes, full, $, no, no, thai, 10-30, yes, )
(yes, yes, yes, yes, full, $$$, no, yes, italian, 10-30, no, )
the decomposite on property of alternative
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
the decomposite on property of hungry
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
the decomposite on property of reservation
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
the decomposite on property of Friday
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, no, yes, full, $, no, no, thai, 30-60, no, )
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, yes, yes, yes, full, $, no, no, burger, 30-60, yes, )
the decomposite on property of alternative
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, yes, yes, full, $, no, no, thai, 10-30, yes, )
(yes, yes, yes, yes, full, $$$, no, yes, italian, 10-30, no, )
the decomposite on property of hungry
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, yes, yes, full, $, no, no, thai, 10-30, yes, )
(yes, yes, yes, yes, full, $$$, no, yes, italian, 10-30, no, )
the decomposite on property of reservation
partition is:
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, no, yes, yes, full, $, no, no, thai, 10-30, yes, )
alternative bar Friday hungry patron price rain reservation type estimate goal
(yes, yes, yes, yes, full, $$$, no, yes, italian, 10-30, no, )
Press any key to continue</pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="PocketRuler.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>