📄 ---model.nlogo
字号:
globals [clock halfedge current_area shape_list color_list expired t agent-id rnd crnd orders ordervalues costs inventory relationship trade-q msg-clr del-clr requests color-incr personality weight prod_limit bias shape_need money c_c c_t t_c t_t c_inv t_inv current_round run_c_c run_c_t run_t_c run_t_t run_c_inv run_t_inv ; these values are the running totals for the different statistics travel-condition ; this variable is needed to shift the conditions to travelers double-condition ; this variable is needed to shif thte conditions to double number_nodes ; initially a slider variable, has now been fixed at 10 distracted_col ; this is an agentset of all the collocators get distracted stat-flag ; set stat-flag to true when there is a statistically significant result between the conditions cc-ct-stat cc-tc-stat cc-tt-stat ct-tc-stat ct-tt-stat tc-tt-stat col-tel-stat id_list]breeds [agents officers edges nodes delayed-requests round-data]round-data-own [col-col col-tel tel-col tel-tel col_inventory tel_inventory cc-list ct-list tc-list tt-list cinv-list tinv-list] nodes-own [out-edges in-edges grabbed? shape_inventory production_limit biases person_type my_need inbox delay id sell-to]edges-own [a b turtle_traffic]turtles-own []delayed-requests-own [delay messages]patches-own [ areas ];-------------- beginning of setup -------------------------------;purpose: set up the initial configuration of the experiment.;when called: before an experimental play;called by: the "setup standare" "setup travel" and "setup double" buttons;receives: none;returns: none;remarks: it initiates the configuration of the experiment by calling the procedure "initiate-round."to setup ca ;clear everything on the screen set number_nodes 10 set environ "in-out" set travel-condition false set double-condition false set current_round 1 ;set the first round as round 1 initiate-first-round ;go to the "initiate-round" procedure to set up the initial configuration of the experiment.endto setup-travel ca ;clear everything on the screen set number_nodes 10 set environ "in-out" set double-condition false set travel-condition true set current_round 1 ;set the first round as round 1 initiate-first-round ;go to the "initiate-round" procedure to set up the initial configuration of the experiment.endto setup-double ca ;clear everything on the screen set number_nodes 10 set double-condition true set travel-condition false set environ "doubles" set current_round 1 ;set the first round as round 1 initiate-first-round ;go to the "initiate-round" procedure to set up the initial configuration of the experiment.end;--------------- end of setup -----------------------------;----------------beginning of setup-nodes----------------------------;purpose: set up the initial configuration of the agents (subjects of the experiment);when called: during the play, in the beginning of each round;called by: the "initiate-round" procedure;receives: none;returns: none;remarks:to setup-nodes locals [i] cct-nodes number_nodes [ set out-edges [] set in-edges [] ;out-edges and in-edges are for the lines connecting each pair of agents set shape_inventory [ 0 0 0 0 0 ] ;the shapes bought by each agent will be recorded by this variable set inbox [] ;all the incoming requests will be stored by this variable set delay 1 ;a variable reflecting the time delay set sell-to [] ;a variable recording how many shapes each agent sold to others set i 0 while [i < number_nodes][ set sell-to lput 0 sell-to set i i + 1 ] set production_limit prod_limit ;set the production limit of each agent. set id first id_list set id_list butfirst id_list set shape first shape_list set shape_list lput shape shape_list set shape_list butfirst shape_list ;assign a shape to an agent set color first color_list set color color set color_list lput color butfirst color_list ;assign a color to an agent set my_need first shape_need set shape_need lput my_need butfirst shape_need ;my_need is the variable reflecting what shapes ;and how many each agent need to fill their orders. set grabbed? false set size 8; if environ = "doubles" [ set heading heading + 12 ] forward 50 ;define the size and location of each agent on the screen.; show "who" + who; show "id" + id ]end;--------------end of setup-nodes;--------------beginning of go----------------------;purpose: start the experiment and keep it running;when called: in the beginning of an experiment;called by: the "go" button;receives: none;returns: none;remarks: to go turtle-trading if (current_round = rounds)[stop]endto go-traveler set travel-condition true turtle-trading if (current_round = rounds)[stop]endto go-double set double-condition true turtle-trading if (current_round = rounds)[stop]end;--------------end of go---------------------------- ;--------------beginning of turtle-trading----------;purpose: implement the trading behaviors among the subjects;when called: during the play, in the beginning of each time period;called by: the "go" procedure;receives: none;returns: none;remarks: this procedure is also called by itself to iterate through all the agents and called by; "initiate-round" at the beginning of each round; Note: there is a bug in printing log file. The data of the last round of an experiment ; will be written the total-number-of-rounds times. For example, if we run 2 rounds in an ; experiment, the data for the second round will be written twice in the log file. to turtle-trading locals [condition]; condition reflects what treatments the agents are in. if (travel-condition) [ if (current_round >= 3) [set environ "travelers"] ] ; sets up the travelers condition if (double-condition) [set environ "doubles"] ; sets up the doubles condition set expired count nodes with [production_limit <= 0] ; count how many agents have sold out all the shapes if (expired = number_nodes)[ ;when all the agents are sold out, record their inventories in the log file. ask nodes [show sell-to] if logfile? [ do-logfile ] if (current_round mod 5 = 0 and statistics?) [ run-exhaustive-t-tests ] ; run t-test on every condition ifelse on-screen-data? [ log-round-data ] [update-data-summary] update-totals do_plots_rt ifelse (current_round = rounds) [stop] ;if all the rounds have been run through, stop the experiment. [ set current_round current_round + 1 initiate-round ] ;if the experiment has not gone through all the rounds, go to a new round. ] set t t + 1 ;increment the time period if (any? delayed-requests with [delay = t]) [send-delayed-requests delayed-requests with [delay = t]]; if there are delayed requests scheduled to be sent in this time period, send them out. while [any? nodes with [delay = t]][ set agent-id random-one-of nodes with [delay = t] ;find out all the agents that can act in this period, and randomize their order to act. ifelse (empty? inbox-of agent-id or production_limit-of agent-id <= 0) [turtle-buy agent-id] ;if an agent's inbox is empty or has sold out all the shapes, it can send out a buying request. [turtle-sell agent-id] ;if an agent has requests in its inbox and it still has shapes to sell, fulfill one of the requests. ; Note: an agent can only buy OR sell, not buy and sell, in each time period. ifelse (empty? areas-of agent-id) ; decide in which future period an agent will act. [set delay-of agent-id t + 1 + cost_of_telecommuting] [ ifelse (member? who-of agent-id distracted_col) [set delay-of agent-id t + 1 + time_of_distraction] [set delay-of agent-id t + 1] ]];closing the "while" functionturtle-trading ;repeat this procedure till all the agents have sold out.end;-------------------end of turtle-trading---------------------------------------;-------------------beginning of turtle-buy----------------------------------;purpose: implement the buying behavior of agents (subjects in an experiment);when called: during the play;called by: the "turtle-trading" procedure;receives: the ID of the agent who is in action;returns: none;remarks: to turtle-buy [buyer_id] locals [ shape_to_request quantity potential_seller i j buyer_need long ] set i 3 set j 0 ;i & j are used to control the iteration through an agent's shape need (the my_need variable) set shape_list [ "circle" "box" "x" "diamond" "triangle" ] set potential_seller nodes with [shape = "cross"];"cross" is an nonexistant shape. It is used to make the potential_seller set empty. ask buyer_id [set buyer_need my_need] set quantity 0 set shape_to_request ""while [not empty? buyer_need][ set long length buyer_need set i random long ;position of the shape set j item i buyer_need ;quantity if (j > 0)[set shape_to_request item i shape_list] set potential_seller nodes with [shape = shape_to_request and production_limit > 0] set quantity j ifelse (count potential_seller > 0) [ set buyer_need [] ] [set buyer_need remove-item i buyer_need]] if (count potential_seller = 0) [stop];if no agent can sell the shape, stop the turtle-buy procedure, and go back to turtle-trading. send-request buyer_id potential_seller shape_to_request quantity ;if some agents can sell the shape, send a request by calling the "send-request" procedure. end;-------------------end of turtle-buy------------------------------- ;-------------------beginning of send-request----------------------;purpose: send a request from the buyer to a seller;when called: during the play;called by: the "turtle-buy" procedure;receives: the ID of the buyer, the set of the potential sellers (those who can sell the shape), the shape to request, and the amount to request.;returns: none;remarks: the favoritism in requesting is reflected by the algorithm to pick a seller out of the potential seller set to send-request [agent-from potential_seller shape_to_request quantity] locals [condition collocator agent-to request buyer] set shape_list [ "circle" "box" "x" "diamond" "triangle" ] set condition areas-of agent-from ;find out whether the buyer is a collocator or a telecommuter set collocator nobody ask agent-from[ set buyer who ] ifelse favoritism [ ; start the favoritism rule if not (empty? condition) [ set collocator random-one-of nodes with [shape = shape_to_request and areas = condition and production_limit > 0] ] ; if the buyer is a collocator, pick a seller from the collocating potential sellers ifelse (collocator = nobody) [set agent-to random-one-of potential_seller] ;if no collocators can sell, randomly pick a seller among all the potential sellers [set agent-to collocator] ] ;end the favoritism rule [set agent-to random-one-of potential_seller] ; else, the first-come-first-serve rule if (agent-to = nobody) [stop] ; if no agent in the experiment can sell, stop the turtle-request procedure, and go back to turtle-trading set request word quantity buyer ; compose the request. Each request is a string of two numbers. The first number is the amount of ; shapes to request, and the second number is the ID of the buyer. ifelse (cost_of_telecommuting = 0) [ask agent-to[ set inbox lput request inbox ] ; if there is no time delay, send out the request immediately. ] [;else start if (not empty? areas-of agent-from and not empty? areas-of agent-to)[ ask agent-to[ set inbox lput request inbox ] ; if there is time delay, but both the seller and the buyer are collocators, send out the request immediately. ] if (not empty? areas-of agent-from and empty? areas-of agent-to)[ ; if the buyer is a collocator but the seller is a telecommuter, ; schedule the request to be sent in a later time period save-delayed-requests request agent-to ] if (empty? areas-of agent-from)[ ; if the buyer is a telecommuter, schedule the request to be sent in a later period. save-delayed-requests request agent-to ] ];else end end;----------------end of send-request-----------------------------;----------------beginning of save-delayed-request---------------;purpose: send requested delayed from previous periods;when called: during the play;called by: the "turtle-trading" procedure;receives: the set of requests scheduled to be sent in the current time period;returns: none;remarks:to save-delayed-requests [request agent-to] locals [temp i j my_message] set i id-of agent-to ifelse (any? delayed-requests with [delay = t + 1 + cost_of_telecommuting]) [set temp random-one-of delayed-requests with [delay = t + 1 + cost_of_telecommuting] ask temp [ set my_message item i messages set my_message lput request my_message set messages replace-item i messages my_message ] ] [cct-delayed-requests 1 [ set delay t + 1 + cost_of_telecommuting set messages [] while [j < number_nodes][ set messages lput [] messages set j j + 1 ] set my_message item i messages set my_message lput request my_message set messages replace-item i messages my_message ] ] end;----------------end of save-delayed-request---------------------;----------------beginning of send-delayed-requests--------------;purpose: send requested delayed from previous periods;when called: during the play;called by: the "turtle-trading" procedure;receives: the set of requests scheduled to be sent in the current time period;returns: none;remarks:to send-delayed-requests [agent-requests] locals [temp i] ask random-one-of agent-requests[ set temp messages ; retrieve all the requests scheduled to be sent in the current period die ] ask nodes [ set i id set inbox sentence inbox item i temp ]end;-------------------end of send-delayed-requests-------------------------;------------------beginning of turtle-sell----------------------------;purpose: implement the selling behavior of the agents;when called: during the play;called by: the "turtle-trading" procedure;receives: the ID of the seller.;returns: none;remarks: the favoritism is reflected by how the seller pick a request to fulfillto turtle-sell [seller_id] locals [request quantity buyer shape_to_deliver my_inbox my_area n demand endx endy move_turtle ] set shape_list [ "circle" "box" "x" "diamond" "triangle" ] set request [] ask seller_id[ set my_inbox reverse inbox set shape_to_deliver position shape shape_list ] set my_area areas-of seller_id ifelse (favoritism) [ ; start the favoritism selling rule foreach my_inbox [ set buyer read-from-string but-first ?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -