/
Tree.cs
240 lines (205 loc) · 8.02 KB
/
Tree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace CPE415_Project
{
[Serializable]
public class Node
{
public List<Node> Nodes = new List<Node>();
public string Name { get; private set; }
public void AddNode(Node p) {
Nodes.Add(p);
p.SetParent(this);
}
public void SetParent(Node p) { Parent = p; }
public Node Parent { get; private set; }
/* Between parent and this */
public int PathCost { get; set; }
public int HeruisticValue { get; private set; }
public Node(string name,int heruisticValue)
{
Name = name;
HeruisticValue = heruisticValue;
}
public bool Visited = false;
public bool VisitedAllNodes = false;
}
[Serializable]
public class Tree
{
public String Path { get; set; }
public String VisitedNodes { get; set; }
public int TotalPathCost { get; set; }
public Tree Clone()
{
object clonedObject = null;
BinaryFormatter formatter = new BinaryFormatter();
using (Stream stream = new MemoryStream())
{
formatter.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
clonedObject = formatter.Deserialize(stream);
}
return (Tree)clonedObject;
}
public Node Root { get; set; }
public List<Node> ReadTree()
{
List<Node> nodlar = new List<Node>();
using (FileStream fs = new FileStream("heuristic.txt", FileMode.Open))
{
if (fs.CanRead)
{
using (StreamReader sr = new StreamReader(fs))
{
string okunan;
while ((okunan = sr.ReadLine()) != null)
{
if (okunan == string.Empty)
continue;
string[] ayrilan = okunan.Split(' ');
int heuristic = Convert.ToInt32(ayrilan[1]);
Node yeniNod = new Node(ayrilan[0], heuristic);
nodlar.Add(yeniNod);
}
}
}
}
using (FileStream fs = new FileStream("tree.txt", FileMode.Open))
{
if (fs.CanRead)
{
using (StreamReader sr = new StreamReader(fs))
{
string okunan;
while ((okunan = sr.ReadLine()) != null)
{
if (okunan == string.Empty)
continue;
string[] ayrilan = okunan.Split(' ');
string parent, child,cost;
parent = ayrilan[0];
child = ayrilan[1];
int icost = Convert.ToInt32(ayrilan[2]);
/*Name = A , Heuristic = 60
Name = B 25
Q 20
C 30
D 50
E 35
F 20
H 15
K 10
G 0*/
foreach (Node n in nodlar)
{
if(n.Name == parent)
{
foreach(Node c in nodlar)
{
if(c.Name == child)
{
n.AddNode(c);
c.PathCost = icost;
}
}
}
}
}
}
}
}
Root = nodlar[0];
foreach(Node n in nodlar)
{
Trace.WriteLine(string.Format("Node {0}, Heruistic value is {1}", n.Name, n.HeruisticValue));
Trace.WriteLine(string.Format("Has {0} child(ren)", n.Nodes.Count));
foreach(Node c in n.Nodes)
{
Trace.WriteLine("\t Child node name {0}", c.Name);
}
}
return nodlar;
}
public void GreedySearch(string destinationName)
{
/// Şuan bulunduğumuz nodu ağacın kök noduna eşitle (başlangıç noktası)
Node currentNode = Root;
/// Hedef noktasına giden yolu tutacak stack
var destinationPath = new Stack<Node>();
/// Bütün gezilen yolları tutacak stack
var fullPath = new Stack<Node>();
for (;;)
{
/// Şuanki bulunduğumuz nodu yola ekle
destinationPath.Push(currentNode);
parent_return:;
/// Şuanki bulunduğumuz nodu gezilen nodlar stackına ekle
fullPath.Push(currentNode);
currentNode.Visited = true;
Trace.WriteLine("node : {0}", currentNode.Name);
/// Hedef noda varıldı mı?
if(currentNode.Name.CompareTo(destinationName) == 0)
{
Trace.WriteLine("Destination found");
break;
}
/// Şuan bulunduğumuz nodun alt nodları arasından seçilen nod
Node selectedNode = null;
/// Bütün alt nodlar için
foreach(Node n in currentNode.Nodes)
{
/// Eğer alt nodun altında kalan nodlar gezildiyse
if (n.VisitedAllNodes)
continue;
/// Seçilen nod boşsa, şuankine eşitle
if (selectedNode == null)
selectedNode = n;
else
{
/// Değilse, heuristic değerlerini karşılaştır ve küçük olanı seç
if (selectedNode.HeruisticValue > n.HeruisticValue)
selectedNode = n;
}
}
/// Eğer herhangi bir nod seçilmediyse
if (selectedNode == null)
{
/// Şuanki bulunduğumuz nodun bütün nodlarını gezildi olarak işaretle
currentNode.VisitedAllNodes = true;
/// Bir üst noda dön
currentNode = currentNode.Parent;
destinationPath.Pop();
goto parent_return;
}
else
{
currentNode = selectedNode;
continue;
}
} /// (break) döngünün bitişi
/// Sonucu değişkenlere aktar
///
while (fullPath.Count > 0)
VisitedNodes += "," + fullPath.Pop().Name;
char[] chars = VisitedNodes.ToCharArray();
Array.Reverse(chars);
VisitedNodes = new string(chars);
int totalpathcost = 0;
while (destinationPath.Count > 0)
{
var point = destinationPath.Pop();
Path += "," + point.Name;
totalpathcost += point.PathCost;
}
TotalPathCost = totalpathcost;
Trace.WriteLine("{0}", totalpathcost.ToString());
char[] charss = Path.ToCharArray();
Array.Reverse(charss);
Path = new string(charss);
}
}
}