Commit be86fbfb authored by Jorn W. Janneck's avatar Jorn W. Janneck

...

parent 196d4f53
(ns amoebas.examples
(:use amoebas.defs amoebas.lib amoebas.run)
(:use amoebas.defs amoebas.lib amoebas.run amoebas.util)
)
;;
......@@ -219,7 +219,178 @@
)
)
)
;;
;; A very simple example of random mutation. With some probability, this species modifies its
;; own parameters when it divides.
;;
(defn create-mutating-slightlybrainy
[low-energy divide-energy select-target mutation-rate mutation-range]
(fn [energy health species env data]
(let
[
do-move (fn []
(let ;; otherwise we gotta move...
[
empty-nb (empty-neighbors env)
by-fuel (sections-by-fuel empty-nb env)
]
(if (empty? empty-nb)
{:cmd :rest} ;; hunker down, we can't move --- FIXME: perhaps we should hit someone?
{:cmd :move :dir (last by-fuel)} ;; move toward the most fuel
)
)
)
do-fuel (fn []
(if (< MaxFuelingEnergy (:fuel (env Here))) ;; are we *at* a McDonald's?
{:cmd :rest} ;; chomp chomp
(do-move)
)
)
do-hit (fn []
(let
[hs (hostiles species Neighbors env)]
(if (empty? hs) ;; nobody to hit?
(do-fuel) ;; eat
{:cmd :hit :dir (Neighbor-To-Dir (select-target hs species env))}
)
)
)
do-div (fn [empty-nb]
(if (<= (rand) mutation-rate)
{:cmd :divide :dir (rand-nth empty-nb)
:function
(create-mutating-slightlybrainy
(bound MoveEnergy (+ low-energy (rand-int (inc (* 2 mutation-range))) (- mutation-range)) MaxAmoebaEnergy)
(bound MinDivideEnergy (+ divide-energy (rand-int (inc (* 2 mutation-range))) (- mutation-range)) MaxAmoebaEnergy)
select-target
mutation-rate
mutation-range) }
{:cmd :divide :dir (rand-nth empty-nb) }
)
)
]
(cond
(< energy low-energy) ;; need some chow?
(do-fuel)
(< divide-energy energy) ;; parenthood!
(let
[empty-nb (empty-neighbors env)]
(if (empty? empty-nb) ;; nowhere to put that crib?
(do-hit) ;; then screw parenthood, hit someone
(do-div empty-nb) ;; oooh, look, it's... an amoeba :-(
)
)
(hostiles species Neighbors env) ;; someone looking at us funny?
(do-hit) ;; whack 'em
:else
(do-fuel) ;; let's eat some more
)
)
)
)
;;
;; This species tries to determine its distance from the edge, and goes into hinterland mode
;; when it's far enough from the action.
;;
;; :data fields:
;; :edge-distance
;;
(defn create-simpleorientingamoeba
[
low-energy
divide-energy
edge-limit
select-target
]
(fn [energy health species env data]
(let
[
fs (friendlies species Environment env) ;; all friendlies
hs (hostiles species Environment env) ;; all friendlies
hss (map #(hostiles species % env) Env-Sections) ;; all hostiles, by section
opens (filter #(region-empty? (Env-Sections %)) Dirs) ;; direction of all open sections, i.e. without hostiles or friendlies
edge-min (apply min ;; minimal edge distance, obtained from friendlies
(cons WorldSize
(map #(+ (:edge-distance (:data (env %)) 0) (distance Here %)) fs)
))
hostile-dist (apply min (cons WorldSize (map #(distance Here %) hs))) ;; minimal distance of visible hostiles
do-move (fn []
(let ;; otherwise we gotta move...
[
empty-nb (empty-neighbors env) ;; these are the empty neighbors
by-fuel (sections-by-fuel empty-nb env) ;; this sorts them by the amount of fuel in the corresponding sections
]
(if (empty? empty-nb) ;; no empty neighbors?
{:cmd :rest} ;; hunker down, we can't move --- FIXME: perhaps we should hit someone?
{:cmd :move :dir (last by-fuel)} ;; move toward the most fuel
)
)
)
do-fuel (fn []
(if (< MaxFuelingEnergy (:fuel (env Here))) ;; are we *at* a McDonald's?
{:cmd :rest} ;; chomp chomp
(do-move) ;; otherwise, keep looking
)
)
do-hit (fn []
(let
[hs (hostiles species Neighbors env)] ;; hostile neighbors
(if (empty? hs) ;; nobody to hit?
(do-fuel) ;; eat
{:cmd :hit :dir (Neighbor-To-Dir (select-target hs species env))} ;; KAPOW!
)
)
)
do-div (fn [empty-nb]
{:cmd :divide :dir (rand-nth empty-nb)} ;; amoeba parenting: drop the child wherever...
)
]
(cond
(< energy low-energy) ;; need some chow?
(do-fuel)
(< divide-energy energy) ;; parenthood!
(let
[empty-nb (empty-neighbors env)]
(if (empty? empty-nb) ;; nowhere to put that crib?
(do-hit) ;; then screw parenthood, hit someone
(do-div empty-nb) ;; oooh, look, it's... an amoeba :-(
)
)
(hostiles species Neighbors env) ;; someone looking at us funny?
(do-hit) ;; whack 'em
:else
(do-fuel) ;; let's eat some more
)
)
)
)
;;
......@@ -343,6 +514,39 @@
})
)
;; Does the rate of mutation matter?
(def T12 (tournament
{
:blue (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.1 1)
:yellow (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 1)
:orange (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.5 1)
:red (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.7 1)
:white (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.9 1)
})
)
;; Is mutation an advantage at all?
(def T13 (tournament
{
:red (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 1)
:blue (create-slightlybrainy 10 70 most-energy-target-selector)
})
)
;; Slow or fast mutation?
(def T14 (tournament
{
:blue (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 1)
:yellow (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 2)
:orange (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 3)
:red (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 5)
:white (create-mutating-slightlybrainy 10 70 most-energy-target-selector 0.3 10)
})
)
......@@ -51,6 +51,7 @@
{ [-1 -1] 0 [0 -1] 1 [1 -1] 2 [1 0] 3 [1 1] 4 [0 1] 5 [-1 1] 6 [-1 0] 7 }
)
(def Dirs (range 8))
(defn- add-dir [dir d] (mod (+ dir d) 8) )
......@@ -97,6 +98,16 @@
(mapv + p1 p2)
)
(defn distance
"distance between two points coordinate vectors,
as defined by the L^infty norm of their difference;
corresponds to the number of steps to get from one to the other"
[[ax ay] [bx by]]
(max (abs (- ax bx)) (abs (- ay by)))
)
;;
;; sections
;; sections are regions that are contiguous parts of the environment
......@@ -241,6 +252,12 @@
(filter #(cell-empty? env %) region)
)
(defn region-empty?
[region env]
(every? #(cell-empty? env %) region)
)
;;
;; section sorters
;; take a list of directions (numbers from 0 to 7) as input
......
......@@ -38,6 +38,13 @@
(apply + s)
)
(defn abs
"computes the absolute value of its argument"
[r]
(if (< r 0) (- r) r)
)
(defn population-stats
"given a population p, which is a map from locations to amoebas,
compute the subpopulation sizes for each species,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment