Skip to content
Snippets Groups Projects
Commit c5a43a70 authored by Stephen D's avatar Stephen D
Browse files

More work

parent 597c8271
No related branches found
No related tags found
No related merge requests found
......@@ -8,8 +8,6 @@ const POP_SIZE: usize = 1_000;
const SELECTION_SIZE: usize = 50;
fn main() {
let mut rng = rand::thread_rng();
let mut res: Vec<_> = (0..POP_SIZE)
.into_par_iter()
.map(|_| {
......@@ -27,12 +25,13 @@ fn main() {
.collect();
// 100 generations
for gen in 0..10 {
for gen in 0..30 {
res.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
let best = &res[(POP_SIZE - SELECTION_SIZE)..];
println!("gen {}: {:?}", gen, best.last().unwrap().1);
let avg: f32 = best.iter().map(|(_, b)| *b).sum::<f32>() / best.len() as f32;
println!("gen {}\tavg {}\tbest {}", gen, avg, best.last().unwrap().1);
let next_gen: Vec<_> = (0..POP_SIZE)
.into_iter()
......
......@@ -24,6 +24,11 @@ fn main() {
for i in 0..1_000 {
println!("{}", i);
println!("Mass: {}", universe.probes[0].mass);
println!(
"Dist: {}",
(universe.probes[0].pos - universe.planets[0].pos).norm()
);
println!("Vel: {}", universe.probes[0].vel);
if !(window.render() && universe.step(1.0)) {
break;
}
......
......@@ -78,7 +78,7 @@ impl NeuralNet {
.zip(row1)
.map(|(x1, x2)| {
((x1 + x2) / 2.0)
* if rng.gen::<f64>() > 0.80 {
* if rng.gen::<f64>() > 0.90 {
rng.gen_range(0.99..1.01)
} else {
1.0
......
......@@ -86,7 +86,7 @@ impl Probe {
}
pub fn step(&mut self, delta: f32) {
self.pos += self.vel * delta / self.mass;
self.pos += self.vel * delta;
if let Some(n) = &mut self.node {
n.set_local_translation(Translation::from(self.pos));
}
......@@ -104,7 +104,7 @@ impl Probe {
let diff = planet.pos - self.pos;
let dist_squared = diff.magnitude_squared();
let force_magnitude = delta * G * self.mass * planet.mass / dist_squared;
let force_magnitude = delta * G * planet.mass / dist_squared;
self.vel += force_magnitude * diff.normalize();
}
......@@ -147,7 +147,7 @@ impl Probe {
let x = output[0] as f32;
let y = output[1] as f32;
let z = output[2] as f32;
let scale = output[3] as f32 * 0.001 * delta;
let scale = output[3] as f32 * 0.1 * delta;
if scale < 0.0 {
return; // Do nothing
......@@ -159,4 +159,17 @@ impl Probe {
self.vel += delta_v;
}
pub fn consume(&mut self, planet: &mut Planet, delta: f32) {
let dist_squared = na::distance_squared(&Point::from(self.pos), &Point::from(planet.pos));
let min_dist = self.radius() + planet.radius + 10.0; // Can eat at distance
if dist_squared > min_dist * min_dist {
return;
}
let amount = delta.min(planet.mass) * 0.001;
self.mass += amount;
planet.mass -= amount;
}
}
......@@ -11,6 +11,7 @@ pub struct Universe {
pub planets: Vec<Planet>,
pub probes: Vec<Probe>,
pub brain: NeuralNet,
fitness: f32,
}
impl Universe {
......@@ -39,6 +40,7 @@ impl Universe {
planets,
probes,
brain,
fitness: 0.0,
}
}
......@@ -65,6 +67,7 @@ impl Universe {
planets,
probes,
brain,
fitness: 0.0,
}
}
......@@ -96,6 +99,7 @@ impl Universe {
planets,
probes,
brain,
fitness: 0.0,
}
}
......@@ -104,24 +108,36 @@ impl Universe {
for p in &mut self.probes {
p.think(&self.planets, delta / 100.0);
for planet in &self.planets {
for planet in &mut self.planets {
p.calculate_gravity(planet, delta / 100.0);
if p.collides_with(planet) {
p.vel *= 0.0;
}
p.consume(planet, delta);
}
p.step(delta / 100.0);
// as close to 100 altitude for as long as possible
//let v = (p.pos - self.planets[0].pos).norm() - 100.0;
//self.fitness += 100.0 - v * v * 10.0
}
}
for i in 0..self.probes.len() {
if self.probes[i].mass < 0.0 {
if self.probes[i].mass < 0.0001 {
self.probes.remove(i);
}
}
for i in 0..self.planets.len() {
if self.planets[i].mass < 0.0001 {
self.planets.remove(i);
}
}
!self.probes.is_empty()
}
......@@ -130,9 +146,12 @@ impl Universe {
return -100.0;
}
//(1.0 / ((self.probes[0].pos - self.planets[0].pos).norm_squared() - 10_000.0)).abs()
// self.fitness
// as much energy as possible
// self.probes[0].mass
// get as high as possible
(self.probes[0].pos - self.planets[0].pos).norm_squared()
self.probes[0].pos.norm_squared()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment