This page was automatically generated by NetLogo 4.1.1.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site.


In order for this to work, this file, your model file (DRA-CRRA-EU-revCD-3l2p.nlogo), and the file NetLogoLite.jar must all be in the same directory. (You can copy NetLogoLite.jar from the directory where you installed NetLogo.)

On some systems, you can test the applet locally on your computer before uploading it to a web server. It doesn't work on all systems, though, so if it doesn't work from your hard drive, please try uploading it to a web server.

You don't need to include everything in this file in your page. If you want, you can just take the HTML code beginning with <applet> and ending with </applet>, and paste it into any HTML file you want. It's even OK to put multiple <applet> tags on a single page.

If NetLogoLite.jar and your model are in different directories, you must modify the archive= and value= lines in the HTML code to point to their actual locations. (For example, if you have multiple applets in different directories on the same web server, you may want to put a single copy of NetLogoLite.jar in one central place and change the archive= lines of all the HTML files to point to that one central copy. This will save disk space for you and download time for your users.)

powered by NetLogo

view/download model file: DRA-CRRA-EU-revCD-3l2p.nlogo

WHAT IS IT?

This section could give a general understanding of what the model is trying to show or explain.


HOW IT WORKS

This section could explain what rules the agents use to create the overall behavior of the model.


HOW TO USE IT

This section could explain how to use the model, including a description of each of the items in the interface tab.


THINGS TO NOTICE

This section could give some ideas of things for the user to notice while running the model.


THINGS TO TRY

This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.


EXTENDING THE MODEL

This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.


NETLOGO FEATURES

This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.


RELATED MODELS

This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.


CREDITS AND REFERENCES

This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.


PROCEDURES

;; Following George Szeps' paper in Complexity 1997 (for which I was a referee), I thought I'd take
;; Nigel Gilbert's GA code for NetLogo 3.1.1 and put a fitness function in which agents choose
;; between two lotteries, based on the calculated means and variances of the two lotteries and
;; the agent's Constant Relative Risk Aversion coefficient, rho = gamma*w, after which the chosen lottery would
;; be realised and the prize added to (or subtracted from) the agent's fitness.
;; Thanks to Nigel Gilbert and Luis Izquierdo
;; -- Robert Marks   {file-print commented out}

globals [

    number-of-turtles  ; the number of agents (turtles)
    chromo-length      ; the length of a chromosome (the number of 
                       ;    bits)
    twochromo-len/two      ; half of the range of rho
    mean-rho           ; mean across turtles                   
    mutation-rate      ; avg. number of mutations per 
                       ;    chromosome per generation 
    generation         ; count of generations
    running-mean-rho   ; running mean rho
    
    prizes             ; list of two randomly chosen prizes
    lottery            ; list of two probabilities  

    ]
   
turtles-own [
    chromosome         ; a list of ones and zeros
    rho                ; risk-preference parameter,  CRRA
    fitness            ; the fitness of this turtle
    cohort             ; the geration this turtle was born in
    
    ]
    
;===========  obtain payoff for each agent as it faces a lottery ==============

to find-fitness            ; turtle procedure.  Report my fitness.  
                           ; This procedure will change according to
                           ; the problem that the Genetic Algorithm 
                           ; is expected to solve.
  set fitness 0
  set rho decimal
  repeat 1000 [
    set fitness (fitness + payoff )
    ]                          
 
end

to-report decimal 
  let dec 0
  foreach chromosome [set dec (dec + dec + ? ) ] 
  report  ((twochromo-len/two - dec) / 100 )
    ;; show rho 
    ;; report rho
end

to-report lottery-values ; turtle procedure. Set up two three-outcome lotteries, where 
                 ; each outcome is chosen randomly from [-10 +10], and the
                 ; two probabilities of each of lottery are chosen.
                 ; Calculate the mean and variance of the lotteries, and
                 ; an outcome of the lotteries. The agent (turtle) chooses one of the
                 ; two lotteries, based on a mean-variance ranking, which assumes a normal
                 ; distribution for each lottery and a CRRA utility function.  
                 ; The agent's fitness is its final wealth after N choices.
                 ; (Not yet: If an agent's wealth falls below -10, then
                 ; the agent becomes bankrupt, and "dies".  Each agent starts with an
                 ; endowment of 50, before facing N pairs of lotteries to choose between.)
  
   setup-prizes
   create-lottery
   let utprize1 ((fitness + (item 0 prizes)) ^ (1 - rho )) / (1 - rho )
   let utprize2 ((fitness + (item 1 prizes)) ^ (1 - rho )) / (1 - rho )
   let exputil ((item 0 lottery) * utprize1) + ((item 1 lottery) * utprize2)          
   let l-prob random-float 1
   let act-val-lot   
     ifelse-value (l-prob <= (item 0 lottery))
        [(item 0 prizes)]
        [(item 1 prizes)]
   ;; show act-val-lot         
   report (list exputil act-val-lot  )  
end


to setup-prizes
  set prizes 0
  set prizes n-values 2 [max-abs-prize - random (2 * max-abs-prize + 1)] 
    ;; two random prizes between -max-abs-prize and +max-abs-prize
    ;; show prizes
end

to create-lottery
  let probabilities 0
  set probabilities n-values 2 [random-float 1]
  let sum-of-probabilities (sum probabilities)
  set lottery map [? / sum-of-probabilities] probabilities
  ;; show lottery
  ;; lottery is a list of two random probs summing to 1
end
 

to-report payoff    ;; turtle procedure. Choose the highest-utility (CRRA) lottery of three
 let lot1 lottery-values
 let lot2 lottery-values
 let lot3 lottery-values
 report ifelse-value (item 0 lot1 >= item 0 lot2)
   [ifelse-value (item 0 lot1 >= item 0 lot3)
     [(item 1 lot1)  ]
     [(item 1 lot3)  ]]
   [ifelse-value (item 0 lot2 >= item 0 lot3) 
     [(item 1 lot2) ]
     [(item 1 lot3) ]]
   
end  
                
                         
                           
;======================== end of my-fitness procedures========================

to mutate              ; turtle procedure.  Flip a bit with 
                       ; a probability of 1 in 1/mutation-rate
    let place 0
    let old 0
    let new 0
    set place (random (chromo-length / mutation-rate))
    if (place < chromo-length) [ 
        set old (item place chromosome)
        ifelse (old = 0)
            [set new 1]
            [set new 0]
        set chromosome (replace-item place chromosome new)
    ]
end

to cross [ mate ]    ; turtle procedure. Exchange a chunk of my 
                     ; and my mate's chromosomes by swapping
                     ; the genes from the beginning to a 
                     ; randomly chosen stopping place 
    let place 0
    let my-gene 0
    let your-gene 0
    repeat random (chromo-length) [
        set my-gene (item place chromosome)
        set your-gene [item place chromosome] of mate
        set chromosome 
          (replace-item place chromosome your-gene)
        ; set [chromosome] of mate
        ask mate [ set chromosome  
          (replace-item place [chromosome] of mate my-gene) ]
        set place place + 1
    ]
end

to breed-a-turtle [ parent ]   ; turtle procedure.  
                               ; Make new turtle and give it
                               ; the genes of its parent turtle.
  create-turtles 1 [
    set cohort generation + 1
    set chromosome [chromosome] of parent
    ]
end

to-report select-a-turtle      ; turtle procedure.  
                               ; Use a tournament selection.  
                               ; The turtle reported is the 
                               ; fitter of two chosen at random.
    let turtleA 0
    let turtleB 0
    set turtleA 
      one-of turtles with [ cohort = generation ]
    set turtleB 
      one-of turtles with [ cohort = generation ]
    ifelse ([fitness] of turtleA > [fitness] of turtleB) 
        [report turtleA]
        [report turtleB] 
end
  

to colour-turtle              ; turtle procedure.  Set the  
                              ; colour of the displayed turtle,
                              ; using the binary value of 
                              ; the chromosome to index the
                              ; colour scale
  set color wrap-color reduce [ ?1 * 2 + ?2 ] chromosome
end

to setup                        ; observer procedure.  
    clear-turtles
    clear-all-plots
    set generation 0
    set mean-rho 0
    set running-mean-rho 0
    set chromo-length 21
    set twochromo-len/two ( 2 ^ (chromo-length - 1))
    set number-of-turtles 100
    set mutation-rate mutatn-rate
    set-current-plot "Fitness"
        set-current-plot-pen "axis"
        auto-plot-off
        plotxy 0 0
        plotxy 1000000000 0
        auto-plot-on
    set-current-plot "rho"    
        set-current-plot-pen "axis"
        auto-plot-off
        plotxy 0 0
        plotxy 1000000000 0
        auto-plot-on    
    create-turtles number-of-turtles [
      set cohort 0
      set fitness 0
      ; make the chromosome a list of random 0s and 1s
      set chromosome n-values chromo-length [ random 2 ]
      ]
    ask turtles [ 
      find-fitness 
      display-turtles
      ]
 
end

    

to display-turtles             ; turtle procedure. Display the 
                               ; turtle on the perimeter of 
                               ; a circle
      colour-turtle
      set heading who * (360 / number-of-turtles)
      fd 10
end

to go                          ; observer procedure.
  let turtleA 0
  let turtleB 0 
  ; Breed  60% of the new population and mate them to
  ; produce two offspring
  repeat (0.3 * number-of-turtles) [
       set turtleA select-a-turtle
       set turtleB select-a-turtle
       ask turtleA [ cross turtleB ]
       breed-a-turtle turtleA
       breed-a-turtle turtleB
       ]
   ; Just copy 40% of the population into the 
   ; new generation   
   repeat (0.4 * number-of-turtles) [
       breed-a-turtle select-a-turtle
       ]
   ; Kill off the old generation
   ask turtles with [ cohort = generation ] [ die ]
   ; Mutate the new generation, display them, and 
   ; find their fitness
   ask turtles [
        mutate
        display-turtles
        find-fitness
    ]
    set mean-rho mean [rho ] of turtles
    set running-mean-rho ((running-mean-rho * generation ) + mean-rho) / (generation + 1)
    ; file-open "./mean-rhos.txt"
    ; file-write mean-rho 
    ; file-print  running-mean-rho
    ; file-close
    set generation generation + 1
    plot-results
    if (generation =  1150) [ stop ]
end

to plot-results                ; observer procedure.  Plot the current 
                               ; mean fitness
    set-current-plot "Fitness"
    set-current-plot-pen "average"
    plot mean [ fitness ] of turtles
    set-current-plot-pen "min-f"
    plot min [ fitness ] of turtles
    set-current-plot-pen "max-f"
    plot max [ fitness ] of turtles
    
    set-current-plot "rho"
    set-current-plot-pen "avrho"
    plot mean [rho ] of turtles
    set-current-plot-pen "maxrho"
    plot max [rho ] of turtles
    set-current-plot-pen "minrho"
    plot min [rho ] of turtles
    
end