This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Representation.cs
157 lines (148 loc) · 5.1 KB
/
Representation.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
using System;
using System.Text.Json.Serialization;
namespace EnemyGenerator
{
/// This class represents an individual.
///
/// Individuals are composed of an enemy, a weapon, their fitness value,
/// their difficulty degree, and the generation when they were created.
/// These attributes are the most common variables from enemies in
/// different games.
///
/// Why individuals are represented by a class instead of a struct? When
/// using MAP-Elites some slots may be empty, then the `null` option makes
/// easier to manage the MAP-Elites population.
[Serializable]
public class Individual
{
[JsonInclude]
public Enemy enemy;
[JsonInclude]
public Weapon weapon;
[JsonInclude]
public float difficulty;
[JsonInclude]
public float fitness;
[JsonInclude]
public int generation;
/// Individual contructor.
public Individual(
Enemy _enemy,
Weapon _weapon
) {
enemy = _enemy;
weapon = _weapon;
}
/// Return a clone of the individual.
///
/// We create a new individual by passing `enemy` and `weapon` in the
/// Individual constructor. Since both are structs, we can copy them by
/// value instead of doing a deep copy.
public Individual Clone()
{
Individual individual = new Individual(enemy, weapon);
individual.difficulty = difficulty;
individual.fitness = fitness;
individual.generation = generation;
return individual;
}
/// Print the individual attributes.
public void Debug()
{
Console.WriteLine(" G=" + generation);
Console.WriteLine(" F=" + fitness);
Console.WriteLine(" D=" + difficulty);
Console.WriteLine(" He=" + enemy.health);
Console.WriteLine(" St=" + enemy.strength);
Console.WriteLine(" AS=" + enemy.attackSpeed);
Console.WriteLine(" MT=" + enemy.movementType);
Console.WriteLine(" MS=" + enemy.movementSpeed);
Console.WriteLine(" AT=" + enemy.activeTime);
Console.WriteLine(" RT=" + enemy.restTime);
Console.WriteLine(" WT=" + weapon.weaponType);
Console.WriteLine(" PS=" + weapon.projectileSpeed);
Console.WriteLine();
}
/// Return a random individual.
public static Individual GetRandom(
ref Random _rand
) {
SearchSpace ss = SearchSpace.Instance;
// Create a random enemy
Enemy e = new Enemy(
Common.RandomInt(ss.rHealth, ref _rand),
Common.RandomInt(ss.rStrength, ref _rand),
Common.RandomFloat(ss.rAttackSpeed, ref _rand),
Common.RandomElementFromArray(ss.rMovementType, ref _rand),
Common.RandomFloat(ss.rMovementSpeed, ref _rand),
Common.RandomFloat(ss.rActiveTime, ref _rand),
Common.RandomFloat(ss.rRestTime, ref _rand)
);
// Create a random weapon
Weapon w = new Weapon(
Common.RandomElementFromArray(ss.rWeaponType, ref _rand),
Common.RandomFloat(ss.rProjectileSpeed, ref _rand)
);
// Combine the genes to create a new individual
Individual individual = new Individual(e, w);
individual.difficulty = Common.UNKNOWN;
individual.generation = Common.UNKNOWN;
individual.fitness = Common.UNKNOWN;
return individual;
}
}
/// This struct represents an enemy.
[Serializable]
public struct Enemy
{
[JsonInclude]
public int health;
[JsonInclude]
public int strength;
[JsonInclude]
public float attackSpeed;
[JsonInclude]
public MovementType movementType;
[JsonInclude]
public float movementSpeed;
[JsonInclude]
public float activeTime;
[JsonInclude]
public float restTime;
/// Enemy contructor.
public Enemy(
int _health,
int _strength,
float _attackSpeed,
MovementType _movementType,
float _movementSpeed,
float _activeTime,
float _restTime
) {
health = _health;
strength = _strength;
attackSpeed = _attackSpeed;
movementType = _movementType;
movementSpeed = _movementSpeed;
activeTime = _activeTime;
restTime = _restTime;
}
}
/// This struc represents a weapon.
[Serializable]
public struct Weapon
{
[JsonInclude]
public WeaponType weaponType;
[JsonInclude]
public float projectileSpeed;
/// Weapon constructor.
public Weapon(
WeaponType _weaponType,
float _projectileSpeed
) {
weaponType = _weaponType;
projectileSpeed = _projectileSpeed;
}
}
}