Can anyone help me with this code?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Can anyone help me with this code?

hannimpeha
I tried to implement value-iteration technique in netlogo following Jose Vidal's.
 However, my agent is not following the green patch and just wandering around.
 What do you think is the problem?
 Thank you so much
 

 extensions [table]
 

 globals [u actions headings best-action]
 

 

 to put-utility [x y dir utility]
   let state (list x y dir)
   table:put u state utility
 end
 

 

 to-report get-utility [x y dir]
   let state (list x y dir)
   ifelse(table:has-key? u state)[
     report table:get u (list x y dir)
   ][
   put-utility x y dir 0
     report 0]
 end
 

 to setup
   ca
   reset-ticks
   ask patches [
     let coin random 10
     set pcolor white
     if(coin = 0)[
       set pcolor red
     ]
     ask patch -16 16[
       set pcolor green
     ]
    ]
 set headings [0 90 180 270]
 set actions [ "fd 1" "lt 90" "rt 90" ]
 set u table:make
 create-turtles 1 [
   set color blue
   set heading one-of headings
   setxy random-xcor random-ycor
 ]
 end
 

 to go
   tick
   ask turtles [run best-action]
 end
 

 to value-iteration
   let delta 1000
   let my-turtle 0
   create-turtles 1 [
     set my-turtle self
     set hidden? true
   ]
   while [delta > epsilon * (1 - gamma) / gamma][
     set delta 0
     ask patches [
       foreach headings [?1 ->
         let x pxcor
         let y pycor
         let dir ?1
         ask my-turtle [
           setxy x y
           set heading dir
           let best-utility item 1 get-best-action
           let current-utility get-utility x y dir
           let new-utility (get-reward + gamma * best-utility)
           put-utility x y dir new-utility
           if (abs (current-utility - new-utility) > delta) [
             set delta abs(current-utility - new-utility)
         ]
         ]
       ]
     ]
    plot delta
   ]
 end
 

 to-report get-best-action
   let x xcor
   let y ycor
   let dir heading
   let best-utility -10000
   foreach actions [?1 ->
     run ?1
     let utility-of-action get-utility xcor ycor dir
     if (utility-of-action > best-utility)[
     set best-action ?1
     set best-utility utility-of-action
   ]
   setxy x y
   set heading dir
   ]
 report(list best-action best-utility)
 end
 

 to-report get-reward
   if (pcolor = green) [report 10]
   if (pcolor = red) [report -10]
   report 0
 
 end