NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.
NetLogo Version: NetLogo 4.0.4
globals [ product-count ;; the number of products company-count ;; the number of products max-quality ;; the highest possible quality (currently) max-demand ;; the highest possible demand (currently) demand-list ;; the global markets demand for each product ] breed [ companies company ] companies-own [ quality-list ;; the quality level of each product manufactured production-list ;; the production level of each product this period ] to setup clear-all clear-output simulate-demand satisfy-demand report-production end to report-production ; repeat 10 [output-print "" ] output-print "--------------------" output-type " " repeat length format demand-list - length "DEMAND: " [output-type " " ] output-type "DEMAND: " output-print format demand-list output-type "(COMPANY ##) " output-type "quality" repeat length format demand-list - length "quality" [output-type " " ] output-type " " output-print "production" foreach sort companies [ ask ? [ output-print (word self (ifelse-value ( [who] of self < 10 ) [ " " ] [ "" ] ) " " format quality-list " " format production-list ) ] ] output-print "--------------------" end to simulate-demand set product-count products-in-market set company-count companies-in-market set max-quality 3 set max-demand 5 ;; * 10 ;; randomly initialize the demand list set demand-list n-values product-count [ 10 * random ( max-demand + 1 ) ] ;; create companies create-companies company-count [ ;; randomly initialize product quality list set quality-list n-values product-count [ random ( max-quality + 1 ) ] ] ;; for display purposes only let c-size (pi * (world-width )) / (company-count ) * cos (180 / (company-count )) let rad .5 * (world-width - c-size) ask companies [ home set shape "circle" set size c-size set label who set heading who * ( 360 / company-count) set color 15 + 10 * who jump rad ] end to satisfy-demand ;; clear the companies productionlist for this persiod ask companies [ set production-list n-values product-count [ 0 ] ] ;; create a sorted list of the companies for use later let sort-comp sort companies ;; now we have a list of stores [ s0 s1 s2 s3...] ;; in order by who number ;; create a list that just contains index numbers for demandlist let indexes n-values product-count [ ? ] ;; for each product in the demand list (foreach demand-list indexes [ ;; move the ?x placeholders into temp variables, for the sake of sanity let demand ?1 ;; demand for the product let index ?2 ;; index number of the product ;; proceed only if there is demand for this product if demand > 0 [ ;; proceed only if some company makes this item if any? companies with [ item index quality-list > 0 ] [ ;; find the company(ies) that produce the highest quality for this product let best-comps companies with-max [ item index quality-list ] ;; note: best-comps is an AGENTSET ;; store the number of best-comps let best-comp-count count best-comps ;; NOW we know who the manufactures will be, ;; we just have to divide the production evenly among them ;; if the demand can be evenly divided among the companies ;; this is trivial, but if it can't, then some ;; means of even distribution is needed (or not?) ;; calc the minimum amount each will produce let min-per-comp-qty floor (demand / best-comp-count) ;; calculate the remaining demand after the minimum is done let rem-demand demand - (min-per-comp-qty * best-comp-count) let prod-qty-list  ;; build a list of the production quantities ;; the first rem-demand items will be min-per-comp-qty + 1 ;; the rest will be min-per-comp-qty ;; if there is no rem-demand, then all will naturally be min-per-comp-qty ;; in other words: map the index number to the production qty, ;; based on the remainder compared to the index number set prod-qty-list map [ min-per-comp-qty + ;; the base amount, plus ifelse-value ( ? < rem-demand ) ;; if the index number of this item ;; is within the quantity of the remainder to distribute [ 1 ] ;; add 1 [ 0 ] ;; otherwise, add nothing ] n-values best-comp-count [ ? ] ;; using a list of index numbers ;; now we have a list of quantities that the companies should produce ;; so we shuffle the list and let the companies take one of the production ;; quantities. (foreach ( sort best-comps ) prod-qty-list [ ;; put ?x in variables for sanity's sake let comp ?1 let prod-qty ?2 ;; remember index is the product index ask comp [ ;; replace the production quantity in product-list (a zero) ;; the the quantity from prod-list set production-list replace-item index production-list prod-qty ] ]) ] ] ]) end to-report format [ input ] ifelse is-list? input [ let cols 2 let spaces " " let line "[" foreach input [ let i (word spaces ?) set i substring i ( length i - 2 ) ( length i ) set line ( word line " " i ) ] set line ( word line " ]" ) report line ] [ report input ] end
View or download the complete model file (to download: right-click, save-link-as):
-- Download distribute-products-by-mfg-quality_2009 --