📄 ---partition perms distrib.nlogo
字号:
globals [
;; the-short-list is a list of the addends for the current total
;; it is used to display the addends in the command center, and it is initialized for each new search
;; also, the "sum the-short-list" gives us the running total and is used to check if we're at the target yet
the-short-list
;; used to display in a monitor the previous list of addends
previous-short-list
;; the-long-list is a list of all the-short-lists, and it is used for the histogram
the-long-list
;; addend is the number randomly generated by the model at each trial. Note that in this version of the model,
;; addends are drawn randomly from a sample space of addends that can still fit into the target total,
;; given the running total. So if the target-total slider is set to 20, and the running total is at 16,
;; then the addends are drawn randomly from a sample space between 1 and 4.
;; Thus, the active sample space decreases as the running total grows.
addend
;; successes-so-far counts up how many times the-short-list has summed up to the total since last setup
;; it should be periodically equal to the quotient of "sum the-long-list / target-total"
successes-so-far
;; counter of how many addends each series is taking
#addends-so-far-in-this-series
;; list of how many addends it took to complete each total
partition-series-list
]
to startup
setup
end
to setup
ca
;; during setup, both the-short-list and the-long-list are initialized,
;; but during the run, the-short-list keeps initializing at each success, whereas the-long-list does not
set the-long-list []
set the-short-list []
set partition-series-list []
set-current-plot "Addends"
set-plot-x-range 1 ( target-total + 1 )
end
to go
if ( successes-so-far >= num-successes ) [ stop ]
random-guess-and-update-total
update-plot
finish-up
end
to random-guess-and-update-total
let running-total sum the-short-list
let temp-sample-space 0
ifelse diminishing-sample-space?
[
;; we draw a random number from the space remaining between the running total and the target-total
set temp-sample-space ( target-total - running-total )
]
[
;; we draw a random number from the entire target-total
set temp-sample-space target-total
]
;; we add 1 because random x reports numbers between 0 and (x-1)
set addend ( 1 + random temp-sample-space )
;; if the addend is small enough not to exceed the target-total, we keep it in the-short-list, and paint it in
if sum the-short-list + addend <= target-total [
set the-short-list lput addend the-short-list
paint
]
set #addends-so-far-in-this-series #addends-so-far-in-this-series + 1
set the-long-list lput addend the-long-list
end
;;; make a histogram of all the addends in the-long-list at the right times
to update-plot
set-current-plot "Addends"
histogram-list the-long-list
end
to paint
let pxcor-of-right-most-colored-patch 0
ifelse length the-short-list = 1
;; we want to create a ribbon of addends, each addend with a different color
[set pxcor-of-right-most-colored-patch ( -1 * screen-edge-x - 1 ) ]
[set pxcor-of-right-most-colored-patch ( max values-from patches with [ pcolor != black ] [ pxcor ] )]
;; temp-color will be any of 14 colors from the NetLogo color space
let temp-color ( 10 * random 14 ) + 5
;; we color only the patches that reflect the latest addend
ask patches with
[ ( pxcor > pxcor-of-right-most-colored-patch ) and ( pxcor <= ( pxcor-of-right-most-colored-patch + addend ) ) ]
[ set pcolor temp-color ]
end
to finish-up
if sum the-short-list = target-total
[
set partition-series-list lput #addends-so-far-in-this-series partition-series-list
set-current-plot"#Addends Per Total"
set-plot-x-range 1 ( 1 + max partition-series-list )
histogram-list partition-series-list
set #addends-so-far-in-this-series 0
set previous-short-list the-short-list
set the-short-list []
set addend "-"
set successes-so-far successes-so-far + 1
if wait-at-full? [ wait .5 ]
cp ct
]
end
; *** NetLogo Model Copyright Notice ***
;
; This model was created as part of the project:
; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN
; CLASSROOMS. The project gratefully acknowledges the support of the
; National Science Foundation (REPP program) -- grant number REC #9814682.
;
; Copyright 2002 by Uri Wilensky. Updated 2002. All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
; from Uri Wilensky.
; Contact Uri Wilensky for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in academic publications, please use:
; Wilensky, U. (2002). NetLogo Partition Perms Distrib model.
; http://ccl.northwestern.edu/netlogo/models/PartitionPermsDistrib.
; Center for Connected Learning and Computer-Based Modeling,
; Northwestern University, Evanston, IL.
;
; In other publications, please use:
; Copyright 1998 by Uri Wilensky. All rights reserved. See
; http://ccl.northwestern.edu/netlogo/models/PartitionPermsDistrib
; for terms of use.
;
; *** End of NetLogo Model Copyright Notice ***
@#$#@#$#@
GRAPHICS-WINDOW
6
47
824
102
50
1
8.0
1
10
1
1
1
0
1
1
1
CC-WINDOW
5
335
834
430
Command Center
0
BUTTON
6
10
96
43
NIL
Setup
NIL
1
T
OBSERVER
T
NIL
BUTTON
102
10
191
43
NIL
Go
T
1
T
OBSERVER
T
NIL
PLOT
179
192
399
319
Addends
NIL
NIL
0.0
100.0
0.0
10.0
true
false
PENS
"list" 1.0 1 -16777216 true
MONITOR
72
192
173
241
Running Total
sum the-short-list
0
1
MONITOR
7
272
127
321
Successes So Far
successes-so-far
0
1
SLIDER
21
102
821
135
target-total
target-total
2
100
20
1
1
NIL
SLIDER
290
10
470
43
num-successes
num-successes
0
10000
10000
100
1
NIL
MONITOR
7
192
69
241
NIL
Addend
3
1
BUTTON
195
10
285
44
Add Once
go
NIL
1
T
OBSERVER
T
NIL
SWITCH
695
11
824
44
wait-at-full?
wait-at-full?
0
1
-1000
MONITOR
439
271
598
320
Mean #Addends Per Total
(length the-long-list) / (successes-so-far)
3
1
SWITCH
475
11
691
44
diminishing-sample-space?
diminishing-sample-space?
0
1
-1000
PLOT
604
191
825
320
#Addends Per Total
NIL
NIL
1.0
10.0
0.0
10.0
true
false
PENS
"default" 1.0 1 -16777216 true
MONITOR
7
140
825
189
Previous List of Included Addends
previous-short-list
3
1
@#$#@#$#@
WHAT IS IT?
-----------
This model is a part of the ProbLab curriculum. The ProbLab Curriculum is currently under development at the CCL. For more information about the ProbLab Curriculum please refer to http://ccl.northwestern.edu/curriculum/ProbLab/.
Partition Permutation Distribution is a model built around the idea of a partition function. This function relates between an integer, e.g., 4, and the number of different ways you can break this integer up into groups of integers, where order does not matter. For instance, 4 can be broken up in 5 ways:
(1) 4;
(2) 3 + 1;
(3) 2 + 2;
(4) 2 + 1 + 1; and
(5) 1 + 1 + 1 + 1.
Notice that in the above example, the number '1' appeared more often than the number '4.' Why is that? To address this question, this model allows you to repeatedly find partitions of a number and look at the distribution of integers in the partitions.
HOW IT WORKS
------------
In this model, you choose a target-total, for instance 20, and the code randomly generates addends of the total and adds them to the running-total. These addends are represented in the graphics window, too, as colorful lines that each are as long as the addend it represents. For instance, an addend of 13 will be 13 "patches" long ("patches" are the NetLogo square areas that make up the grid of the graphics window). When the running-total reaches the total, there's been a 'success.' Unlike the actual partition function, this model will not return a value. Moreover, in this model, there is no explicit attempt to exhaust all the partitions. Instead, the randomized procedure keeps adding up the totals randomly. Over many such brute-force addings, a graph shape emerges, and the same shape emerges both for constant totals over many runs and for different totals. The question is why this shape emerges and what this shape means in terms of partitions. So this model uses partitions as an engaging riddle to explore the idea of distribution.
Note:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -