📄 ---random combinations and permutations.nlogo
字号:
globals [
instructions ;; The messages that appear in the instructions monitor
;; Booleans to preempt run-time errors and/or user confusions
am-I-set-up?
combi-exists?
abort-create-combi?
patches-in-set-block ;; agentset of patches where the user sets up the game
patches-in-guess-block ;; agentset of patches where the random guesses are generated
num-choices
color-list ;; the source options of colors to choose from
dice-list ;; the source options of dice faces to choose from
color-rotation ;; the list of colors available to choose from in a particular experiment
dice-rotation ;; the list of dice faces available to choose from in a particular experiment
;; variables for keeping track of the accumulating statistical data
count-steps ;; counts the number of outcomes in the current sample
#combi-successes-per-sample-list ;; list of outcomes per sample that were exactly like the original combination
#permis-successes-per-sample-list ;; list of outcomes per sample that are the original combination or its permutation
samples-counter ;; how many sample have elapsed in an experiment
permis-choices ;; list of choices in original combination irrespective of their original order
permis-choices-check ;; list of choices in a guessed combination irrespective of their original order
permis-success? ;; Boolean that tracks whether there has been a hit under the permutation condition
count-combi-successes ;; counts up hits under the combi condition
count-permis-successes ;; counts up hits under the permutation condition
mean-combi-per-sample ;; mean number of same-order outcomes per sample
mean-permis-per-sample ;; mean number of either-order outcomes per sample
all-combi-match? ;; Boolean that is true when a combination has been discovered
all-permis-match? ;; Boolean that is true when a perumation of a combination has been discovered
;; Boolean associated with the Hide/Reveal button
hide?
]
patches-own
[
my-color ;; color property for the experimental mode in which the combinations consist of colors
my-shape ;; shape property for the experimental mode in which the combinations consist of dice face.
;; patches register the shape of the dice-turtles that are in the original combination
my-guess-block-buddy ;; each patch in the set-block has a buddy patch in the guess-block
]
breeds
[
frames ;; frames are the black perimeters of patches that help the user distinguish between
;; neighboring patches of same color. The frames are constant throughout the experiment
;; and are never part of the statistical analysis.
dice ;; dice are the dice-face turtles
]
to startup
initialize
ask patches [ set pcolor brown ]
set instructions "Hi! " + " Press Setup or set new values in the 'width' and 'height' sliders and then press Setup."
end
to initialize
ca
;; building the optional colors for the color combinations
set color-list [ green blue magenta cyan pink yellow ]
;; building the optional shapes for the dice combinations
set dice-list [ "one" "two" "three" "four" "five" "six" ]
;; These two variables track the combination search according to the two interpretation of what a success is (see Info tab)
set all-combi-match? false
set all-permis-match? false
;; Variables for managing use of the model
set combi-exists? false
set am-I-set-up? false
;; List variables for selecting and searching values
set color-rotation []
set dice-rotation []
;; List variables for accumulating the experimental outcomes
set permis-choices []
set #combi-successes-per-sample-list []
set #permis-successes-per-sample-list []
set bars? true
set abort-create-combi? false
set num-choices #choices
set hide? true
end
;;--------------------------------------------------------------------------------------------------------
;;--------------------------------------------------------------------------------------------------------
to setup
set-default-shape frames "frame"
set am-I-set-up? false
initialize
create-set-block ;; the set-block is the group of patches to be used in setting the combination
create-guess-block ;; the set-block is the group of patches to be used in guessing the combination
ask patches-in-set-block [ create-frames ]
ask patches-in-guess-block [ create-frames ]
set instructions "OK, you have created a " + width + "-by-" + height + " block with " + count patches-in-set-block +
" squares in it. Now press Create Combi."
set am-I-set-up? true
end
;; The following procedure is perhaps more elaborate than is called for here, but it is powerful in that
;; it will work if you decide to modify the 'width' and 'height' setting to greater maximum values
to create-set-block
let x-pos false
let x-neg false
let y-pos false
let y-neg false
ifelse ( width / 2 ) = floor ( width / 2 )
[ set x-pos (width / 2) set x-neg ( - ( ( width / 2 ) - 1 ) ) ]
[ set x-pos floor (width / 2) set x-neg ( - ( floor ( width / 2 ) ) ) ]
ifelse ( height / 2 ) = floor ( height / 2 )
[ set y-pos ( height / 2 ) set y-neg ( - ( ( height / 2 ) - 1 ) ) ]
[ set y-pos floor ( height / 2 ) set y-neg ( - ( floor ( height / 2 ) ) ) ]
set patches-in-set-block patches with [ pxcor <= x-pos and pxcor >= x-neg and pycor <= y-pos + 1 and pycor >= y-neg + 1 ]
ask patches-in-set-block [ set pcolor green ]
ask patches with [ pcolor != green ] [ set pcolor brown ]
end
;; To create the guess-block of patches, each patch in the set-block assigns a value to a patch 3 patches lower down
;; The patch variable my-color is used in this context even though it is not a color context, to save an agentset variable
to create-guess-block
ask patches-in-set-block
[
set my-guess-block-buddy patch-at 0 -3
ask my-guess-block-buddy [ set my-color "buddy" ]
]
set patches-in-guess-block patches with [ my-color = "buddy" ]
end
to create-frames ;;patches procedure
sprout-frames 1
[ set color black ]
end
;; procedure for choosing the combination
to create-combi
if not am-I-set-up? [ wait .1 alert-setup stop ]
if not dice? [ask dice [ die ] ]
if abort-create-combi? [ stop ]
set instructions "Click on the green squares repeatedly to create your combination. " +
"Then Unpress Create Combi."
ifelse dice? [ set-dice-rotation ] [ set-color-rotation ]
assign-color-or-image
;; explaining to the user the order of actions that is suitable for running this model
if ( dice? and ( #choices != num-choices ) ) or
( not dice? and ( #choices != num-choices ) )
[
set #choices num-choices
set instructions "Sorry. To change the '#choices' slider," +
" unpress Create Combi, set the slider and press SETUP." wait 5
]
how-many-of-each-choice?
set combi-exists? true
end
;; coordinating between user's clicks on the patches in the combi and shapes/colors of these patches
to assign-color-or-image
if mouse-down?
[
ask patches-in-set-block with [ ( pxcor = round mouse-xcor ) and ( pycor = round mouse-ycor ) ]
[
ifelse dice?
[
ifelse not any? dice-here
[
make-dice
]
[
;; see NETLOGO FEATURE in the Information tab.
ask dice-here [ set shape item ( ( 1 + position shape dice-rotation ) mod num-choices ) dice-rotation ]
]
]
[
;; see NETLOGO FEATURE in the Information tab.
set pcolor item ( ( 1 + position pcolor color-rotation ) mod num-choices ) color-rotation
]
wait .3 ;; this wait gives the user a chance to lift the finger after clicking so that the procedure doesn't go again
]
]
ask patches-in-set-block
[
ifelse dice?
[ ask dice-here [ set my-shape shape ] ]
[ set my-color pcolor ]
]
if dice?
[
ask patches-in-set-block
[
ask my-guess-block-buddy
[
if not any? dice-here [ make-dice ]
ask dice-here [ ht ]
]
]
]
end
to make-dice
sprout-dice 1
[
set color black
set shape "one"
]
end
;; Creates a new list with items from the color-list. For instance, if #choices is 3, then the
;; new list will contain the first 3 color names from the color-list
;; See also NETLOGO FEATURE in the Information tab.
to set-color-rotation
let color-list-counter 0
ifelse length color-rotation = num-choices [ stop ] [set color-rotation [] ]
repeat num-choices
[
set color-rotation lput ( item color-list-counter color-list ) color-rotation
set color-list-counter color-list-counter + 1
]
end
;; Creates list from part of the dice-list. See also NETLOGO FEATURE in the Information tab.
to set-dice-rotation
let dice-list-counter 0
ifelse length dice-rotation = num-choices [ stop ] [ set dice-rotation [] ]
repeat num-choices
[
set dice-rotation lput ( item dice-list-counter dice-list ) dice-rotation
set dice-list-counter dice-list-counter + 1
]
end
;; For the permutations condition, counts up how many times each choice appears
to how-many-of-each-choice?
let rotation-counter 0
set permis-choices []
repeat num-choices
[
ifelse dice?
[ set permis-choices lput count dice with
[ ( pycor > 0 ) and (shape = item rotation-counter dice-rotation ) ] permis-choices ]
[ set permis-choices lput count patches-in-set-block with
[pcolor = (item rotation-counter color-rotation ) ] permis-choices ]
set rotation-counter rotation-counter + 1
]
end
;; the core super-procedure for generating random combinations and searching for matches with the original combination
to search-combi
if ( dice? ) and ( sum permis-choices != count patches-in-set-block ) [ alert-forgot-choice stop ]
;; managing the user-code interface
if not am-I-set-up? or not combi-exists? [ alert-setup stop ]
set abort-create-combi? true
set instructions "The program guesses combinations randomly and tracks" +
" the number of times it discovers your combination."
if dice? and count dice < width * height
[ set instructions "You are in dice mode. Please first set up all your dice or change to color mode" stop ]
ifelse dice?
[ set #choices length dice-rotation ]
[ set #choices length color-rotation ]
;; These two Boolean variables track whether a search is successful. The point is that by default the search is
;; considered a success, as if the guessed combi matches the user's combi. Later, in the check procedure, if
;; there is a mismatch it is found out.
set all-combi-match? true
set all-permis-match? true
ask patches-in-set-block [ guess ]
ask patches-in-set-block [ check ]
check-permis
set count-steps count-steps + 1
if all-combi-match?
[ set count-combi-successes count-combi-successes + 1 ]
if all-permis-match?
[ set count-permis-successes count-permis-successes + 1 ]
;; for 'single-success?' true, we want the program to stop after a combination has been found that matches the user's combination
if single-success? [
ifelse Analysis-Type = "both" [
if all-combi-match? [
congratulate-combi
stop
]
if all-permis-match? [
congratulate-permi
stop
]
] [
ifelse Analysis-Type = "combination" [
if all-combi-match? [
congratulate-combi
stop
]
] [
if all-permis-match? [
congratulate-permi
stop
]
]
]
]
if count-steps = sample-size [ plot-sample ]
end
to congratulate-combi
let calc-combi-help ( length #combi-successes-per-sample-list * sample-size ) + count-steps
set instructions "Congratulations! " + " You discovered the hidden combination in "
+ calc-combi-help + " steps. " + "Set up and try again."
little-setup
end
to congratulate-permi
let calc-permis-help ( length #permis-successes-per-sample-list * sample-size ) + count-steps
set instructions "Congratulations! " + " You discovered a permutation of the hidden combination in "
+ calc-permis-help + " steps. " + "Set up and try again."
wait 1
little-setup
end
to little-setup
set count-steps 0
set count-combi-successes 0
set #combi-successes-per-sample-list []
set count-permis-successes 0
set #permis-successes-per-sample-list []
end
;; the model guesses by trying out a combination of random values from the dice or color lists
to guess ;; patches-in-set-block procedure
ifelse dice?
[
ask my-guess-block-buddy
[
ask dice-here
[
set shape item ( random length dice-rotation ) dice-rotation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -