-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockets.cpp
More file actions
68 lines (53 loc) · 1.68 KB
/
Rockets.cpp
File metadata and controls
68 lines (53 loc) · 1.68 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
#include "Rockets.h"
#include <iostream>
#pragma once
CoreAidan::Rockets::Rockets(GameDataRef data) : _data(data){
_populationSize = 100;
for (int i = 0; i < _populationSize; i++) {
_rockets.push_back(new Rocket(_data));
}
}
void CoreAidan::Rockets::update(Vector2d target){
for (std::vector<Rocket*>::iterator it = _rockets.begin(); it != _rockets.end(); ++it) {
(*it)->update(target);
}
}
void CoreAidan::Rockets::draw(){
for (std::vector<Rocket*>::iterator it = _rockets.begin(); it != _rockets.end(); ++it) {
(*it)->draw();
}
}
void CoreAidan::Rockets::evaluate(Vector2d vector) {
float maxFit = 0;
for (std::vector<Rocket*>::iterator it = _rockets.begin(); it != _rockets.end(); ++it) {
(*it)->calcFitness(vector);
if ((*it)->fitness > maxFit) {
maxFit = (*it)->fitness;
}
}
for (std::vector<Rocket*>::iterator it = _rockets.begin(); it != _rockets.end(); ++it) {
(*it)->fitness /= maxFit;
}
_matingpool.clear();
for (std::vector<Rocket*>::iterator it = _rockets.begin(); it != _rockets.end(); ++it) {
float n = (*it)->fitness * 100;
for (int i = 0; i < n; i++) {
_matingpool.push_back(*it);
}
}
std::cout << "mating pool size: " << _matingpool.size() << std::endl;
}
void CoreAidan::Rockets::selection(){
std::vector<Rocket*> newRockets;
for (int i = 0; i < _rockets.size(); i++) {
DNA parentA = _matingpool.at(rand() % _matingpool.size())->dna;
DNA parentB = _matingpool.at(rand() % _matingpool.size())->dna;
DNA child = parentA.crossover(parentB);
newRockets.push_back(new Rocket(_data, child));
}
_rockets = newRockets;
}
bool CoreAidan::Rockets::isDone() {
std::vector<Rocket*>::iterator it = _rockets.begin();
return (*it)->isDone();
}