add seed random on rapsberry emulator, add launch params: -r (use random), -s [number] - set seed in random

This commit is contained in:
MaximZaitsev
2016-08-30 15:57:31 +03:00
parent f7b164b9fb
commit a084404fb7
4 changed files with 46 additions and 5 deletions
+9
View File
@@ -1,5 +1,6 @@
import control.car.ControllerToUsb
import control.emulator.ControllerEmulator
import control.emulator.Rng
import net.Client
import net.server.handlers.AbstractHandler
import net.server.handlers.debug.Memory
@@ -22,13 +23,21 @@ fun main(args: Array<String>) {
val clArgs = js("process.argv")
var runAsEmulator = false
var runTests = false
var nextSeed = false
for (arg in clArgs) {
if (nextSeed) {
Rng.SEED = parseInt(arg, 10).toLong()
Rng.curState = Rng.SEED.toLong()
nextSeed = false
}
when (arg) {
"-t" -> runTests = true
"-e" -> runAsEmulator = true
"-tr1" -> Room.walls = Room.testRoom1()
"-tr2" -> Room.walls = Room.testRoom2()
"-tr3" -> Room.walls = Room.testRoom3()
"-r" -> Room.randomOn()
"-s" -> nextSeed = true
}
}
val carController =
@@ -18,8 +18,6 @@ class ControllerEmulator : Controller {
private val MOVE_VELOCITY = 0.05//sm/ms
private val ROTATION_VELOCITY = 0.05//degrees/ms
private val ADD_RANDOM = false
enum class MoveDirection {
LEFT,
RIGHT,
@@ -113,7 +111,7 @@ class ControllerEmulator : Controller {
if (distance == -1) {
distances.add(distance)
} else {
val delta = if (ADD_RANDOM) getRandomIntFrom(IntArray(5, { x -> x - 2 })) else 0//return one of -2 -1 0 1 or 2
val delta = if (Room.ADD_RANDOM) Rng.nextInt(-2, 2) else 0//return one of -2 -1 0 1 or 2
distances.add(distance + delta)
}
}
@@ -184,8 +182,8 @@ class ControllerEmulator : Controller {
//refresh car state
val carInstance = CarState.instance
val commandDistance = currentCommand.second
val delta = if (ADD_RANDOM) Math.random() * 0.2 + 0.9 else 1.0// delta in [0.9, 1.1)
val commandDistanceIncludeRandom = (commandDistance * delta).toInt()
val coef = if (Room.ADD_RANDOM) Rng.nextInt(900, 1100).toDouble() / 1000.0 else 1.0
val commandDistanceIncludeRandom = (commandDistance * coef).toInt()
when (currentCommand.first) {
MoveDirection.FORWARD -> carInstance.moving(commandDistanceIncludeRandom)
MoveDirection.BACKWARD -> carInstance.moving(commandDistanceIncludeRandom)
@@ -0,0 +1,29 @@
package control.emulator
object Rng {
var SEED = 55L
var curState = SEED
val a = 1664525L
val c = 1013904223L
val mod = 2147483648L
fun abs(value: Long): Long {
if (value < 0)
return -value
return value
}
fun nextInt(): Int {
val res = curState
curState = abs(a * curState + c) % mod
return (res % mod).toInt()
}
fun nextInt(min_val: Int, max_val: Int): Int {
val rand = nextInt()
val size = max_val - min_val + 1
val randInRange = rand % size
val res = randInRange + min_val
return res
}
}
+5
View File
@@ -6,6 +6,7 @@ object Room {
var walls = testRoom1()
var ADD_RANDOM = false
/*
_________
@@ -15,6 +16,10 @@ object Room {
*/
fun randomOn() {
ADD_RANDOM = true
}
fun testRoom1(): List<Wall> {
val upLine = Line(0.0, 1.0, -300.0)
val leftLine = Line(1.0, 0.0, 150.0)