better weighing function for Codes; simple bug avoidance heuristic

This commit is contained in:
Armin Friedl 2017-11-06 17:32:36 +01:00
parent dc6308e364
commit 005d2a3ab6
6 changed files with 410 additions and 73 deletions

File diff suppressed because one or more lines are too long

141
bofa/Calculations.ipynb Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,3 +1,9 @@
bot.zip: src/main/scala/bot/*.scala
cd src/main/scala/bot && sed -i 's/Boolean = true/Boolean = false/' DebugUtil.scala
release:
cd src/main/scala/bot && sed -i 's/Dbg: Boolean = true/Dbg: Boolean = false/' DebugUtil.scala
cd src/main/scala/bot && sed -i 's/Dbg: Boolean = false/Dbg: Boolean = true/' DebugUtil.scala
cd src/main/scala && zip -r bot bot && mv bot.zip $(CURDIR)
dbg:
cd src/main/scala/bot && sed -i 's/Dbg: Boolean = false/Dbg: Boolean = true/' DebugUtil.scala
cd src/main/scala/bot && sed -i 's/Dbg: Boolean = true/Dbg: Boolean = false/' DebugUtil.scala
cd src/main/scala && zip -r bot bot && mv bot.zip $(CURDIR)

View file

@ -5,7 +5,6 @@ import DebugUtil.{log,dbg,time}
object Main {
def main(args: Array[String]) = {
var input: String = ""
//Initialize general game settings
@ -20,12 +19,14 @@ object Main {
log("init cortex")
val cortex = new Cortex()
log("init debug")
DebugUtil.state = Some(currentState)
var tmpRound = currentState.round
// Main loop
while (input != null ){
if (tmpRound != currentState.round){
tmpRound = currentState.round
log("\n");
log(s"********** ROUND ${tmpRound} **********")
}
def cmd: Seq[String] = input.split(" ")
// dispatch

View file

@ -24,8 +24,9 @@ class Cortex {
def move(state: State, time: Int): Direction = {
val directions: Seq[Direction] = possibleDirections(state.bofaField, state)
val weightBoard = weightCodes(1000, decr, state)
val weightBoard = weightCodes(1000, weigh, state)
preventFallBack(weightBoard, state)
preventLethal(weightBoard, state)
val dir = getMaxDir(state.bofaField, directions, weightBoard, state)
@ -37,9 +38,8 @@ class Cortex {
dir
}
def decr(dist: Int, start: Int) = {
val d = (start/math.pow(2.5, (dist+1))).intValue()
if (d < 5) 5 else d
def weigh(start: Int, dist: Int, cur: Int) = {
(start / math.pow(1.3, (dist + 1))).intValue()
}
def getMaxField(weightBoard: Array[Array[Int]], state: State): Field = {
@ -108,21 +108,36 @@ class Cortex {
if (Scribble.lastFieldWeight == weightBoard(lastBofaField.x)(lastBofaField.y)) {
Scribble.lastFieldWeight = weightBoard(state.bofaField.x)(state.bofaField.y)
val x: Int = weightBoard(lastBofaField.x)(lastBofaField.y)
weightBoard(lastBofaField.x)(lastBofaField.y) = (x - (0.1 * x)).intValue()
weightBoard(lastBofaField.x)(lastBofaField.y) = (0.5 * x).intValue()
} else {
Scribble.lastFieldWeight = weightBoard(state.bofaField.x)(state.bofaField.y)
}
}
}
def weightCodes(start: Int, drop: (Int, Int) => Int, state: State): Array[Array[Int]] = {
def preventLethal(weightBoard: Array[Array[Int]], state: State): Unit = {
val board = state.board
for (bug <- state.bugs) {
val dirs = possibleDirections(bug, state)
for (d <- dirs) {
var (x, y) = (bug.x + d.deltaX, bug.y + d.deltaY)
if (d == Left && board(bug.x)(bug.y).citizens.contains(Gate("l"))) x = S.width - 1
else if (d == Right && board(bug.x)(bug.y).citizens.contains(Gate("r"))) x = 0
weightBoard(x)(y) = (weightBoard(x)(y) * 0.1).intValue()
}
weightBoard(bug.x)(bug.y) = 1
}
}
def weightCodes(start: Int, weigh: (Int, Int, Int) => Int, state: State): Array[Array[Int]] = {
val board = state.board
val codes = state.codes
val weightBoard = Array.ofDim[Int](S.width, S.height)
for (x <- 0 until S.width; y <- 0 until S.height) weightBoard(x)(y) = 10
log(s"Codes: ${codes.length}")
for (code <- codes) {
val visited = Array.ofDim[Boolean](S.width, S.height)
for (x <- 0 until S.width; y <- 0 until S.height) visited(x)(y) = false
@ -131,14 +146,16 @@ class Cortex {
val frontier = new mutable.ListBuffer[Field]
frontier += code
visited(code.x)(code.y) = true
var weight = start
var dist = 0
while (weight >= drop(dist, start) && !frontier.isEmpty) {
while (!frontier.isEmpty) {
coords = frontier.toList
frontier.clear()
for (field <- coords) {
val dirs = possibleDirections(field, state)
for (d <- dirs) {
var (x, y) = (field.x + d.deltaX, field.y + d.deltaY)
@ -146,13 +163,13 @@ class Cortex {
else if (d == Right && board(field.x)(field.y).citizens.contains(Gate("r"))) x = 0
if (!visited(x)(y)) frontier += board(x)(y)
visited(x)(y) = true
}
if (!visited(field.x)(field.y)) weightBoard(field.x)(field.y) += weight
visited(field.x)(field.y) = true
weightBoard(field.x)(field.y) += weight
}
weight -= drop(dist, start)
weight = weigh(start, dist, weight)
dist += 1
}
}

View file

@ -3,8 +3,8 @@ package bot
object DebugUtil {
var state: Option[State] = None // we need this to get access to the current game round
val Dbg: Boolean = false
val Log: Boolean = false
val Time: Boolean = false
val Log: Boolean = true
val Time: Boolean = true
def dbg(msg: String) = if(Dbg) Console.err.println(s"$round $msg")
def log(msg: String) = if(Log) Console.err.println(s"$round $msg")