-
Notifications
You must be signed in to change notification settings - Fork 0
/
MitigationGA
165 lines (142 loc) · 5.14 KB
/
MitigationGA
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class MitigationGA : MonoBehaviour
{
public Material nonBurningTree;
public Material burningTree;
public Material deadTree;
public static ForestFire forestFire = new ForestFire();
public static List<CreatureGA> listOfCreatures = new List<CreatureGA>();
public static List<List<int>> listOfGenes = new List<List<int>>();
public const int numberOfCreaturesPerGeneration = 64; //MULTIPLE OF 4
public const int forestFiresPerCreature = 10;
public const int numberOfTreesPerForest = 1000;
public const int forestFireSize = 75;
public static float maxBurnDistance;
public const float windConstraint = 1.5f;
public static int forestFiresLoaded = 0;
public const int mutationRate = 10; //1 out of x
public const int mutationRepetition = 3;
public static int generationNumber;
public static int seed = 5342;
public int lst;
public static bool loading = false;
//public static int ENDHOUR = 6;
public static int ENDMIN = 10;
//public static object locker;
void Start()
{
RunGA();
StartCoroutine("RuntimeGA");
}
public void RunGA()
{
Renderer.DeleteAllRenders();
generationNumber = 0;
forestFire.ResetAll(numberOfTreesPerForest, seed);
CreateGenes(numberOfCreaturesPerGeneration);
CreateCretures();
// for (int i = 0; i < numberOfCreaturesPerGeneration; i++)
// {
// listOfCreatures[i].Render(nonBurningTree, burningTree, deadTree, i);
// }
}
public IEnumerator RuntimeGA()
{
while ((System.DateTime.Now.Minute < ENDMIN) && (System.DateTime.Now.Hour == 23))
//while (System.DateTime.Now.Hour != ENDHOUR)
{
StartCreatures();
NextGeneration();
sa();
yield return null;
}
}
public void CreateCretures()
{
for (int i = 0; i < numberOfCreaturesPerGeneration; i++)
{
CreatureGA cga = new CreatureGA();
cga.ResetAll(i);
listOfCreatures.Add(cga);
}
}
public void CreateGenes(int numberOfGenesToAdd)
{
for (int i = 0; i < numberOfGenesToAdd; i++)
{
listOfGenes.Add(GenerateSingleGeneString());
}
}
public List<int> GenerateSingleGeneString()
{
List<int> newGene = new List<int>();
List<int> avalibleTreeIds = Enumerable.Range(1, numberOfTreesPerForest - 2).ToList();
int geneLength = Functions.randomInt(1, numberOfTreesPerForest - 1);
for (int i = 0; i < geneLength; i++)
{
int randomPick = Functions.randomInt(0, avalibleTreeIds.Count - 2);
int geneToAdd = avalibleTreeIds[randomPick];
avalibleTreeIds.RemoveAt(randomPick);
newGene.Add(geneToAdd);
}
return newGene;
}
public void StartCreatures()
{
for (int i = 0; i < numberOfCreaturesPerGeneration; i++)
{
listOfCreatures[i].Run();
}
//Renderer.DeleteAllRenders();
// for (int i = 0; i < numberOfCreaturesPerGeneration; i++)
// {
// listOfCreatures[i].Render(nonBurningTree, burningTree, deadTree, i);
// }
}
public void NextGeneration()
{
for (int i = 0; i < numberOfCreaturesPerGeneration; i++)
{
CreatureGA creatureChecking = listOfCreatures[i];
creatureChecking.EvaluateFitness();
listOfCreatures[i] = creatureChecking;
}
listOfCreatures.Sort((s1, s2) => s1.fitnessEvaluationNumber.CompareTo(s2.fitnessEvaluationNumber));
Debug.Log("Lowest Fitness :: " + listOfCreatures[0].fitnessEvaluationNumber + " :: Generation :: " + generationNumber);
// Renderer.DeleteAllRenders();
// for (int i = 0; i < numberOfCreaturesPerGeneration; i++)
// {
// listOfCreatures[i].Render(nonBurningTree, burningTree, deadTree, i);
// }
int halfOfTheNumberOfCreatures = (int)Mathf.Floor(numberOfCreaturesPerGeneration * 0.5f);
//int quaterOfTheNumberOfCreatures = (int)Mathf.Floor(numberOfCreaturesPerGeneration * 0.25f);
listOfCreatures.RemoveRange(halfOfTheNumberOfCreatures - 1, halfOfTheNumberOfCreatures);
listOfGenes.Clear();
for (int i = 0; i < halfOfTheNumberOfCreatures; i += 2)
{
listOfGenes.Add(new List<int>(listOfCreatures[i].treesToRemove));
listOfGenes.Add(new List<int>(listOfCreatures[i + 1].treesToRemove));
listOfGenes.Add(listOfCreatures[i].CreateGenesFromParent(listOfCreatures[i + 1].treesToRemove));
listOfGenes.Add(GenerateSingleGeneString());
}
listOfCreatures.Clear();
CreateCretures();
generationNumber++;
}
public void ls()
{
loading = true;
DataRecorder.CreateBest(nonBurningTree, burningTree, deadTree, lst);
}
public void sa()
{
DataRecorder.CreateSave(listOfGenes[0], seed);
}
public void del()
{
Renderer.DeleteAllRenders();
}
}