-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocket.cs
More file actions
124 lines (108 loc) · 3.12 KB
/
Rocket.cs
File metadata and controls
124 lines (108 loc) · 3.12 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vector;
namespace SmartRockets
{
public enum State { Alive, Dead, Finished};
class Rocket
{
private Vector2 Velocity;
private Vector2 Acceleration;
public DNA dna;
public int genesCounter;
public float record;
public State state;
public float fitness = 0;
public Vector2 Pos { get; set; }
public void calculateFitness()
{
// fitness = (float)Math.Pow(1 / (record+0.0001f), 2);
fitness = 1 / (record+0.0001f);
if (state == State.Dead)
{
fitness *= 0.005f;
}else if(state == State.Finished)
{
fitness *= 10;
}
}
public Rocket(Vector2 pos)
{
record = 99999;
state = State.Alive;
Velocity = new Vector2();
Acceleration = new Vector2();
Pos = pos;
genesCounter = 0;
dna = new DNA();
}
public void Render(Graphics g)
{
Color col = Color.Magenta;
switch (state)
{
case State.Alive:
col = Color.FromArgb(100, Color.White);
break;
case State.Dead:
col = Color.FromArgb(100, Color.Red);
break;
case State.Finished:
col = Color.FromArgb(100, Color.Magenta);
break;
}
g.FillEllipse(new SolidBrush(col), Pos.X-5, Pos.Y-5, 10, 10);
}
public void ApplyForce(Vector2 force)
{
Acceleration.Add(force);
}
public void Run()
{
ApplyForce(dna.genes[genesCounter]);
genesCounter++;
Update();
}
public void Update()
{
Velocity.Add(Acceleration);
Pos.Add(Velocity);
Acceleration.Multiply(0);
float time = ((float)genesCounter / DNA.lifetime) * 100;
float temp = Vector2.Distance(Pos, Population.target);
record = time + temp;
//if (temp + time < record)
//{
// record = temp + time;
//}
if(temp <= 5)
{
Pos = new Vector2(Population.target.X, Population.target.Y);
record = time;
state = State.Finished;
}
if (isOutOfMap())
{
state = State.Dead;
}else if(Form1.obstacles.Any(o => o.Distance(Pos) <= 25))
{
state = State.Dead;
}
}
private bool isOutOfMap()
{
return (Pos.Y < - 5) || (Pos.X > Form1.WH.Width + 5)
|| (Pos.Y > Form1.WH.Height + 5) || (Pos.X < - 5);
}
}
public class DoneException : Exception
{
public DoneException(string message) : base(message)
{
}
}
}