Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: bendlas/ai-challenge
base: f053024ba6
...
head fork: bendlas/ai-challenge
compare: 462732ea93
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
3  demo.sh
@@ -8,6 +8,7 @@ tools/playgame.py \
-e \
--strict \
--capture_errors \
---loadtime=10000 \
+--loadtime=3000 \
+--turntime=500 \
"$@" "python tools/sample_bots/python/HunterBot.py" "python tools/sample_bots/python/LeftyBot.py" "python tools/sample_bots/python/GreedyBot.py" "java -cp `lein classpath` clojure.main @MyBot.clj" \
-So | java -jar tools/visualizer.jar
View
15 smalldemo.sh
@@ -0,0 +1,15 @@
+tools/playgame.py \
+--player_seed 42 \
+--end_wait=0.25 \
+--verbose \
+--log_dir tools/game_logs \
+--turns 1000 \
+--map_file tools/maps/mymap.map \
+--food sections \
+-e \
+--strict \
+--capture_errors \
+--loadtime=5000 \
+--turntime=500 \
+"$@" "java -cp `lein classpath` clojure.main @MyBot.clj" "python tools/sample_bots/python/LeftyBot.py" \
+-So | java -jar tools/visualizer.jar
View
4 src/aichallenge/ant.clj
@@ -1,6 +1,7 @@
(ns aichallenge.ant
"Library to interact with the game"
- (:require [clojure.string :as string]))
+ (:require [clojure.string :as string]
+ [aichallenge.util :as u]))
;;****************************************************************
;; Constants and lookups
@@ -107,6 +108,7 @@
rows (*game-info* :rows)
cols (*game-info* :cols)
[r c] (map + ant dir-vector)]
+ (u/perrln "Ant: " ant " Dir: " dir)
[(cond
(< r 0) (+ rows r)
(>= r rows) (- r rows)
View
15 src/aichallenge/bot.clj
@@ -7,10 +7,21 @@
[strategy :as s]
[util :as u])))
+(defn- move
+ [offset]
+ (cond
+ (= offset [1 0]) :souht
+ (= offset [0 -1]) :west
+ (= offset [-1 0]) :north
+ (= offset [0 1]) :east))
+
(defn dir-for-ant
[ant knowledge]
- (first (filter #(ant/valid-move? (:state knowledge) ant %)
- (shuffle [:north :east :west :south]))))
+ (let [moves (:assigned-moves knowledge)]
+ (if (not (contains? moves ant))
+ (first (filter #(ant/valid-move? (:state knowledge) ant %)
+ (shuffle [:north :east :west :south])))
+ (move (get moves ant)))))
(defn moves-for-step
"Based on current knowledge, return next move for every ant"
View
72 src/aichallenge/strategy.clj
@@ -2,6 +2,15 @@
(:use [aichallenge.matrix :as m])
(:use [aichallenge.util :as u]))
+(defn- assign-move
+ [ant move knowledge]
+ (let [free-ants (:free-ants knowledge)
+ moves (:assigned-moves knowledge)]
+ (assoc knowledge :free-ants (filter #(not (= %1 ant)) free-ants))
+ (if (empty? moves)
+ (assoc knowledge :assigned-moves {ant move})
+ (assoc knowledge :assigned-moves (assoc (:assigned-moves knowledge) ant move)))))
+
(defn- in-danger?
[knowledge]
false)
@@ -12,13 +21,59 @@
knowledge ;protect hill
knowledge))
+(defn- expand
+ [[r c]]
+ [[(dec r) c] [r (inc c)] [(inc r) c] [r (dec c)]])
+
+(defn- best-option
+ [options target]
+ (let [pow2 (fn [x] (* x x))
+ distance (fn [opt] (Math/sqrt (+ (pow2 (- (first target) (first opt))) (pow2 (- (last target) (last opt))))))]
+ (reduce #(cond
+ (nil? %2) %1
+ (<= (distance %1) (distance %2)) %1
+ :esle %2) options)))
+
+(defn- update-knowledge-with-move
+ [ant expanded knowledge]
+ (let [desired-move (best-option expanded ant)
+ offset [(- (first desired-move) (first ant)) (- (last desired-move) (last ant))]]
+ (assign-move ant offset knowledge)))
+
+(defn- get-route
+ [start target knowledge]
+ (let [water (:water (:state knowledge))
+ vm (:visual-memory knowledge)
+ acceptable? (fn [x] (and (not (contains? water x))
+ (= (m/get-matrix vm (first x) (last x)) 0)))]
+ (loop [options #{start}
+ processed #{}
+ new-knowledge knowledge]
+ (if (empty? options)
+ new-knowledge
+ (let [cur (best-option options target)]
+ (if (= cur target)
+ (update-knowledge-with-move target processed new-knowledge)
+ (recur (filter #(not (= cur %1)) (into options (filter
+ #(and (acceptable? %1) (not (contains? options %1)) (not (contains? processed %1)))
+ (expand cur))))
+ (conj processed cur)
+ new-knowledge)))))))
+
(defn- gatherer-strategy
[knowledge]
- (let [food (:food (:state knowledge))]
- (if (empty? food)
+ (loop [food (:food (:state knowledge))
+ ants (:free-ants knowledge)
+ knowledge knowledge]
+ (if (or (empty? food) (empty? ants))
knowledge
- knowledge ;there is food, lets go and get it
- )))
+ (let [target (first ants)
+ start (best-option food target)]
+ (recur (filter #(not (= start %1)) food)
+ (filter #(not (= target %1)) ants)
+ (if (or (nil? start) (nil? target))
+ knowledge
+ (get-route start target knowledge)))))))
(defn- explorer-strategy
"Given the cost map, apply exploration adjustment"
@@ -27,8 +82,9 @@
(defn update-strategy
[knowledge]
- (->
- (warrior-strategy knowledge)
- (gatherer-strategy)
- (explorer-strategy)))
+ (let [new-knowledge (assoc knowledge :free-ants (:ants (:state knowledge)))]
+ (->
+ (warrior-strategy new-knowledge)
+ (gatherer-strategy)
+ (explorer-strategy))))

No commit comments for this range

Something went wrong with that request. Please try again.