-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
143 lines (137 loc) · 6.05 KB
/
Program.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
using System;
using System.Linq;
using System.Collections.Generic;
using static Project_Genetic.Constants;
using System.Diagnostics;
namespace Project_Genetic
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
DateTime start = DateTime.Now;
Constants.CalculateResultsRealFunction();
List<Tree> trees = new List<Tree>(NO_FIRST_GENERATION);
TreeBuilder treeBuilder = new TreeBuilder();
Console.WriteLine("creating first generation({0} trees)",NO_FIRST_GENERATION);
// double min=double.MaxValue;
// bool foundAtFirst=false;
List<double> MSEs = new List<double>();
for (int i = 0; i < NO_FIRST_GENERATION; i++)
{
Tree tree = new Tree();
treeBuilder.build(tree);
tree.CalculateMSE();
trees.Add(tree);
// if(double.IsNormal(tree.MSE) && tree.MSE<min){
// min = tree.MSE;
// }
// if(min<GOOD_MSE){
// foundAtFirst=true;
// break;
// }
}
Console.WriteLine("first generation created successfully");
trees = trees.Where(t => double.IsNormal(t.MSE)).ToList();
trees.Sort();
trees = trees.Take(NO_EACH_GENERATION).ToList();
Random random = new Random(unchecked((int)DateTime.Now.Ticks));
int mseGood = NO_PARENTS * 3 / 4;
int mseBad = NO_PARENTS - mseGood;
// TreeBuilder treeBuilder = new TreeBuilder();
// if(foundAtFirst) goto jump;
System.Console.WriteLine("Genetic iterations started! no of iterations:{0}, no of parents:{1}, no of each generation:{2}",NO_ITERATIONS,NO_PARENTS,NO_EACH_GENERATION);
for (int i = 0; i < NO_ITERATIONS; i++)//Genetic
{
Console.Write("\r"+Math.Round(100.0 * i / NO_ITERATIONS) + "% done!");
for (int j = 0; j < NO_PARENTS; j++)
{
//TODO: choose parents better
int randomI1, randomI2;
if (j < mseGood)
{
randomI1 = random.Next(mseGood);
randomI2 = random.Next(mseGood);
}
else
{
randomI1 = random.Next(NO_EACH_GENERATION);
randomI2 = random.Next(NO_EACH_GENERATION);
}
Tree treeParent1 = trees[randomI1], treeParent2 = trees[randomI2];
Tree treeChild1 = treeParent1.Clone(), treeChild2 = treeParent2.Clone();
//TODO: make the children
treeBuilder.SwapChildren(ref treeChild1, ref treeChild2);
//TODO: random number choose Jahesh
//TODO2: Jahesh not working
if (treeBuilder.DecideJahesh())
{
if (treeBuilder.ChooseTree())
{
treeBuilder.doJahesh(ref treeChild1);
}
else
{
treeBuilder.doJahesh(ref treeChild2);
}
}
treeChild1.CalculateMSE();
treeChild2.CalculateMSE();
if(!trees.Any(tr=>tr.MSE==treeChild1.MSE))
trees.Add(treeChild1);
if(!trees.Any(tr=>tr.MSE==treeChild2.MSE))
trees.Add(treeChild2);
}
trees = trees.Where(t => double.IsNormal(t.MSE)).ToList();
trees.Sort();
trees = trees.Take(NO_EACH_GENERATION).ToList();
MSEs.Add(trees[0].MSE);
if(trees[0].MSE<GOOD_MSE){
break;
}
}
// jump:
// if(foundAtFirst){
// trees = trees.Where(t => double.IsNormal(t.MSE)).ToList();
// trees.Sort();
// trees = trees.Take(NO_EACH_GENERATION).ToList();
// }
System.Console.WriteLine("\n"+(DateTime.Now-start).ToHumanReadableString());
runPlot(Constants.RealFunctionString());
runPlotMSE(string.Format("[{0}]",string.Join(',',MSEs)));
for (int i = 0; i < Math.Min(NO_SAMPLES,trees.Count); i++)
{
Console.Write((i + 1) + "th:");
Console.Write(trees[i]);
Console.WriteLine(" " + trees[i].MSE);
if(i<1 || (i<3 && (trees[i].MSE<GOOD_MSE)))
runPlot(trees[i].ToString());
}
}
static void runPlot(string func)
{
string file="\"C:\\Users\\Sadegh\\Desktop\\code\\Term5\\AI Abdi\\Project Genetic\\plot.py\"";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "python";//@"C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\python.exe";
start.Arguments =string.Format("{0} {1}",file,func);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
}
}
static void runPlotMSE(string list)
{
string file="\"C:\\Users\\Sadegh\\Desktop\\code\\Term5\\AI Abdi\\Project Genetic\\plot mse.py\"";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "python";//@"C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\python.exe";
start.Arguments =string.Format("{0} {1}",file,list);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
}
}
}
}