better weighing function for Codes; simple bug avoidance heuristic
This commit is contained in:
parent
dc6308e364
commit
005d2a3ab6
6 changed files with 410 additions and 73 deletions
172
bofa/.ipynb_checkpoints/Calculations-checkpoint.ipynb
Normal file
172
bofa/.ipynb_checkpoints/Calculations-checkpoint.ipynb
Normal file
File diff suppressed because one or more lines are too long
141
bofa/Calculations.ipynb
Normal file
141
bofa/Calculations.ipynb
Normal file
File diff suppressed because one or more lines are too long
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -23,71 +23,71 @@ object Scribble {
|
|||
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)
|
||||
|
||||
|
||||
Scribble.lastBofaField = state.bofaField
|
||||
|
||||
|
||||
weightBoard(state.bofaField.x)(state.bofaField.y) = -999
|
||||
printBoard(weightBoard)
|
||||
|
||||
|
||||
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 = {
|
||||
var maxField = (0,0)
|
||||
var maxField = (0, 0)
|
||||
for (x <- 0 until S.width; y <- 0 until S.height) {
|
||||
// generally, the highest is best
|
||||
if (weightBoard(x)(y) > weightBoard(maxField _1)(maxField _2)) maxField = (x,y)
|
||||
if (weightBoard(x)(y) > weightBoard(maxField _1)(maxField _2)) maxField = (x, y)
|
||||
// if equal prefer to go down, right b/c up/left is evil first but
|
||||
if (weightBoard(x)(y) == weightBoard(maxField _1)(maxField _2)){
|
||||
if (weightBoard(x)(y) == weightBoard(maxField _1)(maxField _2)) {
|
||||
if (x > (maxField _1) || y > (maxField _2)) maxField = (x, y)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
state.board(maxField _1)(maxField _2)
|
||||
}
|
||||
|
||||
|
||||
def shortestPathTo(field: Field, state: State) = {
|
||||
0
|
||||
}
|
||||
|
||||
|
||||
def mod(a: Int)(b: Int): Int = (a % b) + (if (a < 0) b else 0)
|
||||
|
||||
|
||||
def getMaxDir(field: Field, directions: Seq[Direction], weightBoard: Array[Array[Int]], state: State): Direction = {
|
||||
var maxDir: Direction = randomDirection(directions)
|
||||
val board = state.board
|
||||
|
||||
for (d <- directions){
|
||||
var (x,y) = (mod (field.x+d.deltaX) (S.width), mod (field.y+d.deltaY) (S.width))
|
||||
var (maxX, maxY) = (mod (field.x+maxDir.deltaX) (S.width), mod (field.y+maxDir.deltaY) (S.width))
|
||||
|
||||
|
||||
for (d <- directions) {
|
||||
var (x, y) = (mod(field.x + d.deltaX)(S.width), mod(field.y + d.deltaY)(S.width))
|
||||
var (maxX, maxY) = (mod(field.x + maxDir.deltaX)(S.width), mod(field.y + maxDir.deltaY)(S.width))
|
||||
|
||||
if (weightBoard(x)(y) > weightBoard(maxX)(maxY)) maxDir = d
|
||||
|
||||
}
|
||||
|
||||
|
||||
maxDir
|
||||
}
|
||||
|
||||
def manhattan(x: Int, y: Int)(a: Int, b: Int): Int = math.abs(x-a) + math.abs(y-b)
|
||||
|
||||
def manhattan(x: Int, y: Int)(a: Int, b: Int): Int = math.abs(x - a) + math.abs(y - b)
|
||||
|
||||
def possibleDirections(field: Field, state: State): Seq[Direction] = {
|
||||
val pd = new mutable.ListBuffer[Direction]
|
||||
val board = state.board
|
||||
|
||||
val toCheck = checkBorder(field, state)
|
||||
|
||||
|
||||
for (d <- toCheck) {
|
||||
if ( d == Left && board(field.x)(field.y).citizens.contains(Gate("l")) ) pd += d
|
||||
else if ( d == Right && board(field.x)(field.y).citizens.contains(Gate("r")) ) pd += d
|
||||
if (d == Left && board(field.x)(field.y).citizens.contains(Gate("l"))) pd += d
|
||||
else if (d == Right && board(field.x)(field.y).citizens.contains(Gate("r"))) pd += d
|
||||
else {
|
||||
val pfield = board(field.x + d.deltaX)(field.y + d.deltaY)
|
||||
if (!(pfield.citizens contains Wall)) {
|
||||
|
@ -95,84 +95,101 @@ class Cortex {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pd
|
||||
}
|
||||
|
||||
|
||||
def preventFallBack(weightBoard: Array[Array[Int]], state: State): Unit = {
|
||||
val lastBofaField = Scribble.lastBofaField
|
||||
|
||||
if (lastBofaField != Field.empty){
|
||||
|
||||
if (lastBofaField != Field.empty) {
|
||||
// if something changed allow fallback
|
||||
log(s"lastFieldWeight=${Scribble.lastFieldWeight}")
|
||||
if (Scribble.lastFieldWeight == weightBoard(lastBofaField.x)(lastBofaField.y)){
|
||||
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()
|
||||
} else{
|
||||
Scribble.lastFieldWeight = weightBoard(state.bofaField.x)(state.bofaField.y)
|
||||
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
|
||||
|
||||
var coords: Seq[Field] = Seq.empty
|
||||
|
||||
var coords: Seq[Field] = Seq.empty
|
||||
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){
|
||||
|
||||
for (field <- coords) {
|
||||
|
||||
val dirs = possibleDirections(field, state)
|
||||
for (d <- dirs) {
|
||||
var (x,y) = (field.x+d.deltaX, field.y+d.deltaY)
|
||||
if ( d == Left && board(field.x)(field.y).citizens.contains(Gate("l")) ) x = S.width-1
|
||||
else if ( d == Right && board(field.x)(field.y).citizens.contains(Gate("r")) ) x = 0
|
||||
var (x, y) = (field.x + d.deltaX, field.y + d.deltaY)
|
||||
if (d == Left && board(field.x)(field.y).citizens.contains(Gate("l"))) x = S.width - 1
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
weightBoard
|
||||
}
|
||||
|
||||
|
||||
def checkBorder(field: Field, state: State): Seq[Direction] = {
|
||||
val dirs = new mutable.ListBuffer[Direction]
|
||||
|
||||
|
||||
if (field.y != 0) dirs += Up
|
||||
if (field.y != (S.height-1)) dirs += Down
|
||||
if (field.y != (S.height - 1)) dirs += Down
|
||||
if (field.x != 0 || field.citizens.contains(Gate("l"))) dirs += Left
|
||||
if (field.x != (S.width-1) || field.citizens.contains(Gate("r"))) dirs += Right
|
||||
|
||||
if (field.x != (S.width - 1) || field.citizens.contains(Gate("r"))) dirs += Right
|
||||
|
||||
dirs
|
||||
}
|
||||
|
||||
def randomDirection(directions: Seq[Direction]) = directions(Random.nextInt(directions.length))
|
||||
|
||||
|
||||
def printBoard(board: Array[Array[Int]]) = {
|
||||
val btp = board.transpose
|
||||
val buf = new StringBuilder
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue