网格与障碍覆盖算法

时间:2015-04-05 15:39:19

标签: algorithm artificial-intelligence grid-layout maze

我必须为机器人代理找到一个算法来执行以下操作(对不起,我真的不知道如何调用它):

  • 机器人在10x10网格上有障碍物(每个方格都是障碍物或可穿越的)

  • 机器人有一个碰撞传感器:当机器人撞到障碍物时它会激活。

  • 在网格上有胡萝卜不断增长。有快速增长的广场和缓慢增长的广场。

  • 每一步,机器人都可以:向前或向左前进或转90°或留在原位

  • 手头不知道胡萝卜和障碍物的位置

  • 胡萝卜在机器人移动时(即使收获后)继续生长

  • 胡萝卜生长在大多数不是障碍的方块中

  • 机器人不知道方块是快速还是缓慢增长

  • 在每个广场上可以有0到20个胡萝卜。在每个时间实例中,对于一个正方形的胡萝卜数量,概率p = 0.01(或快速增长的正方形p = 0.02)

  • 您可以测量您收获的胡萝卜数量。

目标是在2000步中获得最大量的胡萝卜。

是否会有一种懒惰/简单的方法来做到这一点?

到目前为止,我有点失落,因为它不是解决迷宫问题。它会是一种充满洪水的算法吗?还有什么更简单的吗?

我不一定在寻找“解决”问题,而是在可能的情况下进行简单的近似

1 个答案:

答案 0 :(得分:1)

鉴于它不知道食物来源的位置和数量,找到具有完美策略的机器人实施确实是一项工作。

机器人的任何给定策略可能不会在每次运行中产生最大可能的收获。所以问题是,在多次模拟运行中哪种策略最成功。

为了找到方形类型(P(快餐),P(慢食),P(障碍物))的给定统计分布的合适策略,人们可能会提出以下想法:

让Bot(npatch)成为寻找npatch食物斑点的机器人。采取的策略是在第二个食物补丁搜索第二个食物补丁之前吃掉它所发现的东西,依此类推。当它访问npatch食物来源(或发现没有更多食物补丁)时,它会返回到第一个发现并重新收获的食物。

这类机器人(Bot(npatch))现在可以在统计相关数量的模拟运行中相互竞争。最佳机器人是比赛的赢家。

这种方法可以被认为是由遗传算法启发,但没有混合任何基因,只是简单地迭代所有基因(1..npatch)。也许有人知道如何将这个想法变成一个完全遗传算法。这可能涉及转向Bot(npatch,searchStrategy),然后有多个基因来应用遗传算法。

每当模拟的参数发生变化时,必须重复比赛,显然,根据世界各地的食物补丁的数量,如果有些食物补丁,可能会或可能不会找到另一个补丁。已知。

下面的代码用F#编写,是该问题的模拟器(如果我的所有要求都正确,那就是......)。编写一个新的机器人就像编写一个函数一样简单,然后将其传递给模拟器。

考虑一下我的复活节彩蛋,对于那些想尝试自己机器人的人来说。

我写的2个机器人叫做“marvinRobot”,它做的是Marvin会做的事情,而“lazyRobot”是一个机器人,它在它找到的第一个食物源上。

type Square =
    | Empty
    | Obstacle
    | Food of float * (float -> float) // available * growth
    | Unknown

let rnd = new System.Random()
let grow p a =
    let r = rnd.NextDouble()
    if r < p then a + 1.0
    else a

let slowGrowth a = grow 0.01 a
let fastGrowth a = grow 0.02 a

let eatPerTick = 1.0 
let maxFoodPerSquare = 20.0

let randomPick values =
    let count = List.length values
    let r = rnd.Next(0,count-1)
    values.Item(r)

type World = Square[,]

let randomSquare pobstacle pfood =
    let r = rnd.NextDouble()
    match r with
    | x1 when x1 < pobstacle -> Obstacle
    | x2 when x2 < (pobstacle + pfood) && x2 >= pobstacle -> 
        Food(rnd.NextDouble() * maxFoodPerSquare, randomPick [slowGrowth; fastGrowth])
    | _ -> Empty

let createRandomWorld n pobstacle pfood = 
    Array2D.init n n (fun col row -> randomSquare pobstacle pfood)

let createUnknownWorld n =
    Array2D.create n n Unknown

type Position = { Column : int; Row : int }

type RoboState = { Memory : Square[,]; Pos : Position; Heading : Position }
type RoboAction = 
    | TurnRight
    | TurnLeft
    | MoveOne
    | Eat
    | Idle

type RoboActor = World -> RoboState -> RoboAction

let right heading : Position =
    match heading with
    | { Column = 0; Row = 1 } -> { Column = -1; Row = 0 }
    | { Column = -1; Row = 0 } -> { Column = 0; Row = -1 }
    | { Column = 0; Row = -1 } -> { Column = 1; Row = 0 }
    | { Column = 1; Row = 0 } -> { Column = 0; Row = 1 }
    | _ -> failwith "Invalid heading!"

let left heading : Position =
    match heading with
    | { Column = -1; Row = 0 } -> { Column = 0; Row = 1 }
    | { Column = 0; Row = -1 } -> { Column = -1; Row = 0 }
    | { Column = 1; Row = 0 } -> { Column = 0; Row = -1 }
    | { Column = 0; Row = 1 } -> { Column = 1; Row = 0 } 
    | _ -> failwith "Invalid heading!"

let checkAccess n position =
    let inRange v = v >= 0 && v < n
    (inRange position.Column) && (inRange position.Row)

let tickWorld world =
    world 
    |> Array2D.map 
        (fun sq -> 
            match sq with 
            | Empty -> Empty 
            | Obstacle -> Obstacle 
            | Food(a,r) -> Food(min (r a) maxFoodPerSquare, r)
            | Unknown -> Unknown
        )

let rec step robot world roboState i imax acc = 
    if i < imax then
        let action = robot world roboState
        match action with
        | TurnRight ->
            let rs1 = { roboState with Heading = right roboState.Heading }
            let wrld1 = tickWorld world
            step robot wrld1 rs1 (i+1) imax acc
        | TurnLeft ->
            let rs1 = { roboState with Heading = left roboState.Heading }
            let wrld1 = tickWorld world
            step robot wrld1 rs1 (i+1) imax acc
        | MoveOne ->
            let rs1 =
                let c = 
                    { Column = roboState.Pos.Column + roboState.Heading.Column 
                      Row = roboState.Pos.Row + roboState.Heading.Row
                    }
                if checkAccess (Array2D.length1 world) c 
                then 
                    match world.[c.Column,c.Row] with
                    | Obstacle -> 
                        roboState.Memory.[c.Column,c.Row] <- Obstacle
                        roboState
                    | _ -> { roboState with Pos = c }
                else
                    roboState
            let wrld1 = tickWorld world
            step robot wrld1 rs1 (i+1) imax acc
        | Eat -> 
            let eat,acc1 = 
                match world.[roboState.Pos.Column,roboState.Pos.Row] with
                | Empty -> Empty,acc
                | Obstacle -> Obstacle,acc
                | Food(a,r) -> 
                    let eaten = if a >= eatPerTick then eatPerTick else 0.0
                    printfn "eating %f carrots" eaten
                    Food(a - eaten, r),eaten + acc
                | Unknown -> Unknown,acc
            world.[roboState.Pos.Column,roboState.Pos.Row] <- eat
            let wrld1 = tickWorld world
            step robot wrld1 roboState (i+1) imax acc1
        | Idle ->
            step robot (tickWorld world) roboState (i+1) imax acc
    else
        acc

let initRoboState n = 
    {   Memory = createUnknownWorld n; 
        Pos = { Column = 0; Row = 0;}; 
        Heading = {Column = 1; Row = 0}
    }

let simulate n pobstacle pfood imax robot =
    let w0 = createRandomWorld n pobstacle pfood
    let r0 = initRoboState n
    printfn "World: %A" w0
    printfn "Initial Robo State: %A" r0
    let result = step robot w0 r0 0 imax 0.0
    printfn "Final Robo State: %A" r0
    result

// Not that Marvin would care, but the rule for this simulator is that the 
// bot may only inspect the square in the world at the current position.
// This means, IT CANNOT SEE the neighboring squares.
// This means, that if there is a obstacle next to current square, 
// it costs a simulation tick to find out, trying to bump against it.
// Any access to other squares in world is considered cheating!
// world is passed in spite of all said above to allow for alternate rules.
let marvinRobot world roboState =
    Idle

// Tries to find a square with food, then stays there, eating when there is something to eat.
let lazyRobot (world : World) (roboState : RoboState) =
    let search() =
        let status action : RoboAction =
            match action with
            | TurnLeft -> printfn "%A TurnLeft at %A (heading: %A)" world.[roboState.Pos.Column,roboState.Pos.Row] roboState.Pos roboState.Heading
            | TurnRight -> printfn "%ATurnRight at %A (heading: %A)" world.[roboState.Pos.Column,roboState.Pos.Row]  roboState.Pos roboState.Heading
            | MoveOne -> printfn "%A MoveOne at %A (heading: %A)" world.[roboState.Pos.Column,roboState.Pos.Row] roboState.Pos roboState.Heading
            | Idle -> printfn "%A Idle at %A (heading: %A)" world.[roboState.Pos.Column,roboState.Pos.Row] roboState.Pos roboState.Heading
            | Eat -> printfn "%A Eat at %A (heading: %A)" world.[roboState.Pos.Column,roboState.Pos.Row] roboState.Pos roboState.Heading
            action
        let neighbors = 
            [ roboState.Heading, MoveOne;
              (roboState.Heading |> right),TurnRight;
              (roboState.Heading |> left),TurnLeft;
              (roboState.Heading |> right |> right),TurnRight
            ]
            |> List.map (fun (p,a) -> (p.Column,p.Row),a)
            |> List.map (fun ((c,r),a) -> (roboState.Pos.Column + c,roboState.Pos.Row + r),a)
            |> List.filter (fun ((c,r),a) -> checkAccess (Array2D.length1 world){Position.Column = c; Row = r})
            |> List.sortBy (fun ((c,r),a) -> match roboState.Memory.[c,r] with | Food(_,_) -> 0 | Unknown -> 1 | Empty -> 2 | Obstacle -> 3)
            |> List.map (fun ((c,r),a) -> { Column = c; Row = r},a)
        if neighbors.IsEmpty then failwith "It's a trap!" // can happen if bot is surrounded by obstacles, e.g.  in a corner
        else
            let p,a = neighbors.Head
            status a
    roboState.Memory.[roboState.Pos.Column, roboState.Pos.Row] <- 
            world.[roboState.Pos.Column,roboState.Pos.Row]
    match world.[roboState.Pos.Column,roboState.Pos.Row] with
    | Food(a,_) -> 
        printfn "Found food at %A" roboState.Pos
        Eat
    | _ -> 
        search()

//simulate 10 0.1 0.05 2000 marvinRobot
simulate 10 0.1 0.1 2000 lazyRobot

最后一个提示:如果您使用0.0食物补丁进行模拟,您的机器人应该已经访问了地图上的所有方块。如果它不能做到这一点,肯定不是一个好的机器人;)