Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
von-neumann-probes
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephen D
von-neumann-probes
Commits
c5a43a70
Commit
c5a43a70
authored
3 years ago
by
Stephen D
Browse files
Options
Downloads
Patches
Plain Diff
More work
parent
597c8271
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
src/bin/learn.rs
+3
-4
3 additions, 4 deletions
src/bin/learn.rs
src/bin/simulate.rs
+5
-0
5 additions, 0 deletions
src/bin/simulate.rs
src/neural_net.rs
+1
-1
1 addition, 1 deletion
src/neural_net.rs
src/probe.rs
+16
-3
16 additions, 3 deletions
src/probe.rs
src/universe.rs
+23
-4
23 additions, 4 deletions
src/universe.rs
with
48 additions
and
12 deletions
src/bin/learn.rs
+
3
−
4
View file @
c5a43a70
...
...
@@ -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
..
1
0
{
for
gen
in
0
..
3
0
{
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 {}
\t
avg {}
\t
best {}"
,
gen
,
avg
,
best
.last
()
.unwrap
()
.1
);
let
next_gen
:
Vec
<
_
>
=
(
0
..
POP_SIZE
)
.into_iter
()
...
...
This diff is collapsed.
Click to expand it.
src/bin/simulate.rs
+
5
−
0
View file @
c5a43a70
...
...
@@ -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
;
}
...
...
This diff is collapsed.
Click to expand it.
src/neural_net.rs
+
1
−
1
View file @
c5a43a70
...
...
@@ -78,7 +78,7 @@ impl NeuralNet {
.zip
(
row1
)
.map
(|(
x1
,
x2
)|
{
((
x1
+
x2
)
/
2.0
)
*
if
rng
.gen
::
<
f64
>
()
>
0.
8
0
{
*
if
rng
.gen
::
<
f64
>
()
>
0.
9
0
{
rng
.gen_range
(
0.99
..
1.01
)
}
else
{
1.0
...
...
This diff is collapsed.
Click to expand it.
src/probe.rs
+
16
−
3
View file @
c5a43a70
...
...
@@ -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.
00
1
*
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
;
}
}
This diff is collapsed.
Click to expand it.
src/universe.rs
+
23
−
4
View file @
c5a43a70
...
...
@@ -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.0
001
{
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
()
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment