-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathML.cs
161 lines (146 loc) · 4.44 KB
/
ML.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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ML : MonoBehaviour
{
God god;
public List<NeuralNetwork> nets;
public float cost;
Intersection int_controller;
public int loc_x;
public int loc_y;
int old_genome;
// Use this for initialization
void Start ()
{
debug = false;
old_genome = 0;
god = GameObject.Find ("God").GetComponent<God> ();
Time.timeScale = 10.0F;
int_controller = this.GetComponent<Intersection> ();
nets = new List<NeuralNetwork> ();
for (int i = 0; i < god.MAX_GENOMES; i++) {
NeuralNetwork net = new NeuralNetwork (new int[] { 12, 12, 11, 10, 8 });
net.Mutate ();
nets.Add (net);
}
StartCoroutine (ChangeState ());
}
void FixedUpdate ()
{
if (god.genome != old_genome) {
old_genome = god.genome;
}
}
void OnTriggerExit (Collider colid)
{
if (colid.gameObject.tag == "Cart") {
nets [god.genome].AddCost (colid.GetComponent<Cart> ().wait_time);
cost = nets [god.genome].GetCost ();
colid.GetComponent<Cart> ().wait_time = 0;
}
}
IEnumerator ChangeState ()
{
while (true) {
List<float> inputs = new List<float> (new float[12]);
List<GameObject> valid_carts = new List<GameObject> ();
try {
GameObject[] carts = GameObject.FindGameObjectsWithTag ("Cart");
foreach (GameObject cart in carts) {
if (cart.GetComponent<Cart> ().current_x == loc_x && cart.GetComponent<Cart> ().current_y == loc_y) {
valid_carts.Add (cart);
}
}
} catch {
}
foreach (GameObject cart_obj in valid_carts) {
Cart cart = cart_obj.GetComponent<Cart> ();
if (cart.dir == "South" && cart.next_move == "South") {
inputs [0] += 0.1f;
} else if (cart.dir == "South" && cart.next_move == "West") {
inputs [1] += 0.1f;
} else if (cart.dir == "South" && cart.next_move == "East") {
inputs [2] += 0.1f;
} else if (cart.dir == "North" && cart.next_move == "North") {
inputs [3] += 0.1f;
} else if (cart.dir == "North" && cart.next_move == "West") {
inputs [4] += 0.1f;
} else if (cart.dir == "North" && cart.next_move == "East") {
inputs [5] += 0.1f;
} else if (cart.dir == "East" && cart.next_move == "South") {
inputs [6] += 0.1f;
} else if (cart.dir == "East" && cart.next_move == "East") {
inputs [7] += 0.1f;
} else if (cart.dir == "East" && cart.next_move == "North") {
inputs [8] += 0.1f;
} else if (cart.dir == "West" && cart.next_move == "South") {
inputs [9] += 0.1f;
} else if (cart.dir == "West" && cart.next_move == "West") {
inputs [10] += 0.1f;
} else if (cart.dir == "West" && cart.next_move == "North") {
inputs [11] += 0.1f;
}
}
float highest_input = 0;
foreach (float input in inputs) {
if (input > highest_input) {
highest_input = input;
}
}
float scaling_factor = 1;
if (highest_input != 0) {
scaling_factor = 1 / highest_input;
}
for (int i = 0; i < inputs.Count; i++) {
inputs [i] = inputs [i] * scaling_factor;
}
string input_out = "";
foreach (float input in inputs) {
input_out += input + " - ";
}
float[] outputs = nets [god.genome].FeedForward (inputs.ToArray ());
int best_index = 0;
for (int i = 1; i < outputs.Length; i++) {
if (outputs [i] > outputs [best_index]) {
best_index = i;
}
}
// Best index between 0 and 7
int_controller.current_state = best_index;
yield return new WaitForSeconds (5f);
}
}
public void NewGeneration ()
{
string cost_list = "";
float cost_total = 0;
nets.Sort ();
foreach (NeuralNetwork net in nets) {
cost_list += net.GetCost () + " - ";
cost_total += net.GetCost ();
}
Debug.Log (cost_list + " AVG: " + cost_total / god.MAX_GENOMES);
string breed_net = "Breeding: ";
int skip_zero = 0;
foreach (NeuralNetwork net in nets) {
if (net.GetCost () == 0) {
skip_zero++;
} else {
break;
}
}
for (int i = 0; i < god.MAX_GENOMES / 2; i++) {
breed_net += nets [i + (god.MAX_GENOMES / 2)].GetCost () + " - ";
nets [i] = new NeuralNetwork (nets [i + (god.MAX_GENOMES / 2)]);
nets [i].Mutate ();
nets [i + (god.MAX_GENOMES / 2)] = new NeuralNetwork (nets [i + (god.MAX_GENOMES / 2)]); //too lazy to write a reset neuron matrix values method....so just going to make a deepcopy lol
}
Debug.Log (breed_net);
for (int i = 0; i < god.MAX_GENOMES; i++) {
nets [i].SetCost (0f);
}
nets = nets.OrderBy( x => Random.value ).ToList( );
}
}