diff --git a/examples/bank_reserves/BankReservesModel_Data.csv b/examples/bank_reserves/BankReservesModel_Data.csv new file mode 100644 index 00000000..f9596ef9 --- /dev/null +++ b/examples/bank_reserves/BankReservesModel_Data.csv @@ -0,0 +1,251 @@ +,RunId,iteration,Step,init_people,rich_threshold,reserve_percent,Rich,Poor,Middle Class,Savings,Wallets,Money,Loans,AgentID,Wealth +0,0,0,1000,25,5,5,11,5,7,251,0,251,173,1,2 +1,0,0,1000,25,5,5,11,5,7,251,0,251,173,2,44 +2,0,0,1000,25,5,5,11,5,7,251,0,251,173,3,-22 +3,0,0,1000,25,5,5,11,5,7,251,0,251,173,4,-10 +4,0,0,1000,25,5,5,11,5,7,251,0,251,173,5,8 +5,0,0,1000,25,5,5,11,5,7,251,0,251,173,6,17 +6,0,0,1000,25,5,5,11,5,7,251,0,251,173,7,5 +7,0,0,1000,25,5,5,11,5,7,251,0,251,173,8,1 +8,0,0,1000,25,5,5,11,5,7,251,0,251,173,9,2 +9,0,0,1000,25,5,5,11,5,7,251,0,251,173,10,51 +10,0,0,1000,25,5,5,11,5,7,251,0,251,173,11,14 +11,0,0,1000,25,5,5,11,5,7,251,0,251,173,12,-22 +12,0,0,1000,25,5,5,11,5,7,251,0,251,173,13,10 +13,0,0,1000,25,5,5,11,5,7,251,0,251,173,14,57 +14,0,0,1000,25,5,5,11,5,7,251,0,251,173,15,7 +15,0,0,1000,25,5,5,11,5,7,251,0,251,173,16,3 +16,0,0,1000,25,5,5,11,5,7,251,0,251,173,17,12 +17,0,0,1000,25,5,5,11,5,7,251,0,251,173,18,-64 +18,0,0,1000,25,5,5,11,5,7,251,0,251,173,19,-2 +19,0,0,1000,25,5,5,11,5,7,251,0,251,173,20,9 +20,0,0,1000,25,5,5,11,5,7,251,0,251,173,21,7 +21,0,0,1000,25,5,5,11,5,7,251,0,251,173,22,-1 +22,0,0,1000,25,5,5,11,5,7,251,0,251,173,23,-23 +23,0,0,1000,25,5,5,11,5,7,251,0,251,173,24,2 +24,0,0,1000,25,5,5,11,5,7,251,0,251,173,25,-29 +25,1,0,1000,25,10,5,12,8,3,422,5,427,251,1,38 +26,1,0,1000,25,10,5,12,8,3,422,5,427,251,2,30 +27,1,0,1000,25,10,5,12,8,3,422,5,427,251,3,-21 +28,1,0,1000,25,10,5,12,8,3,422,5,427,251,4,65 +29,1,0,1000,25,10,5,12,8,3,422,5,427,251,5,40 +30,1,0,1000,25,10,5,12,8,3,422,5,427,251,6,10 +31,1,0,1000,25,10,5,12,8,3,422,5,427,251,7,-12 +32,1,0,1000,25,10,5,12,8,3,422,5,427,251,8,8 +33,1,0,1000,25,10,5,12,8,3,422,5,427,251,9,30 +34,1,0,1000,25,10,5,12,8,3,422,5,427,251,10,20 +35,1,0,1000,25,10,5,12,8,3,422,5,427,251,11,-5 +36,1,0,1000,25,10,5,12,8,3,422,5,427,251,12,-38 +37,1,0,1000,25,10,5,12,8,3,422,5,427,251,13,-12 +38,1,0,1000,25,10,5,12,8,3,422,5,427,251,14,27 +39,1,0,1000,25,10,5,12,8,3,422,5,427,251,15,-29 +40,1,0,1000,25,10,5,12,8,3,422,5,427,251,16,6 +41,1,0,1000,25,10,5,12,8,3,422,5,427,251,17,-37 +42,1,0,1000,25,10,5,12,8,3,422,5,427,251,18,27 +43,1,0,1000,25,10,5,12,8,3,422,5,427,251,19,-38 +44,1,0,1000,25,10,5,12,8,3,422,5,427,251,20,-10 +45,1,0,1000,25,10,5,12,8,3,422,5,427,251,21,-49 +46,1,0,1000,25,10,5,12,8,3,422,5,427,251,22,37 +47,1,0,1000,25,10,5,12,8,3,422,5,427,251,23,35 +48,1,0,1000,25,10,5,12,8,3,422,5,427,251,24,37 +49,1,0,1000,25,10,5,12,8,3,422,5,427,251,25,12 +50,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,1,-81 +51,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,2,27 +52,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,3,-56 +53,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,4,53 +54,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,5,86 +55,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,6,77 +56,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,7,20 +57,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,8,-28 +58,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,9,51 +59,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,10,-72 +60,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,11,-96 +61,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,12,81 +62,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,13,-43 +63,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,14,66 +64,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,15,70 +65,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,16,7 +66,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,17,32 +67,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,18,21 +68,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,19,64 +69,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,20,-54 +70,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,21,11 +71,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,22,-30 +72,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,23,-21 +73,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,24,24 +74,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,25,-4 +75,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,26,-12 +76,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,27,-67 +77,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,28,53 +78,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,29,79 +79,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,30,-9 +80,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,31,-6 +81,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,32,-2 +82,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,33,45 +83,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,34,85 +84,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,35,23 +85,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,36,-32 +86,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,37,-36 +87,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,38,-75 +88,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,39,-67 +89,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,40,-44 +90,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,41,-13 +91,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,42,-53 +92,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,43,105 +93,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,44,20 +94,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,45,-77 +95,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,46,36 +96,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,47,-35 +97,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,48,-87 +98,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,49,-70 +99,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,50,98 +100,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,51,109 +101,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,52,-93 +102,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,53,-18 +103,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,54,72 +104,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,55,38 +105,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,56,-33 +106,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,57,43 +107,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,58,87 +108,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,59,-2 +109,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,60,29 +110,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,61,123 +111,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,62,-22 +112,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,63,47 +113,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,64,108 +114,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,65,-53 +115,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,66,-3 +116,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,67,-58 +117,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,68,-16 +118,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,69,-28 +119,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,70,41 +120,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,71,-60 +121,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,72,-69 +122,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,73,93 +123,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,74,9 +124,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,75,37 +125,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,76,-108 +126,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,77,-69 +127,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,78,75 +128,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,79,-11 +129,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,80,-18 +130,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,81,16 +131,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,82,-32 +132,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,83,12 +133,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,84,8 +134,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,85,25 +135,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,86,16 +136,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,87,-99 +137,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,88,-15 +138,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,89,33 +139,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,90,12 +140,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,91,43 +141,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,92,2 +142,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,93,-139 +143,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,94,25 +144,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,95,101 +145,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,96,78 +146,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,97,44 +147,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,98,3 +148,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,99,-44 +149,2,0,1000,100,5,5,50,42,8,2563,7,2570,2219,100,-59 +150,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,1,-22 +151,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,2,27 +152,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,3,60 +153,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,4,89 +154,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,5,11 +155,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,6,12 +156,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,7,-47 +157,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,8,60 +158,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,9,34 +159,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,10,2 +160,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,11,5 +161,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,12,-27 +162,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,13,-13 +163,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,14,105 +164,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,15,-63 +165,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,16,138 +166,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,17,65 +167,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,18,40 +168,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,19,-68 +169,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,20,39 +170,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,21,-27 +171,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,22,64 +172,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,23,50 +173,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,24,-86 +174,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,25,21 +175,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,26,42 +176,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,27,-2 +177,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,28,-124 +178,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,29,90 +179,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,30,-39 +180,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,31,-40 +181,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,32,21 +182,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,33,55 +183,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,34,60 +184,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,35,71 +185,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,36,-27 +186,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,37,66 +187,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,38,48 +188,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,39,-63 +189,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,40,74 +190,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,41,3 +191,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,42,13 +192,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,43,-20 +193,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,44,0 +194,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,45,-51 +195,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,46,45 +196,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,47,44 +197,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,48,-110 +198,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,49,-95 +199,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,50,-21 +200,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,51,-46 +201,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,52,-34 +202,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,53,31 +203,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,54,-42 +204,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,55,52 +205,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,56,39 +206,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,57,112 +207,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,58,39 +208,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,59,-57 +209,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,60,108 +210,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,61,-33 +211,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,62,28 +212,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,63,7 +213,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,64,33 +214,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,65,-22 +215,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,66,69 +216,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,67,-77 +217,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,68,-58 +218,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,69,19 +219,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,70,27 +220,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,71,-41 +221,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,72,63 +222,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,73,97 +223,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,74,60 +224,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,75,32 +225,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,76,36 +226,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,77,-48 +227,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,78,-47 +228,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,79,45 +229,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,80,-5 +230,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,81,-27 +231,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,82,13 +232,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,83,48 +233,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,84,-69 +234,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,85,-55 +235,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,86,-41 +236,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,87,-31 +237,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,88,-14 +238,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,89,-103 +239,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,90,-29 +240,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,91,64 +241,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,92,-74 +242,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,93,1 +243,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,94,-72 +244,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,95,57 +245,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,96,6 +246,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,97,-15 +247,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,98,-15 +248,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,99,-46 +249,3,0,1000,100,10,5,49,42,9,2598,33,2631,2046,100,58 diff --git a/examples/bank_reserves/batch_run.py b/examples/bank_reserves/batch_run.py index dfbbefb1..11189f90 100644 --- a/examples/bank_reserves/batch_run.py +++ b/examples/bank_reserves/batch_run.py @@ -36,97 +36,17 @@ get_total_money, get_total_savings, get_total_wallets, + BankReservesModel ) - -def track_params(model): - return (model.init_people, model.rich_threshold, model.reserve_percent) - - -def track_run(model): - return model.uid - - -class BankReservesModel(mesa.Model): - # id generator to track run number in batch run data - - # grid height - grid_h = 20 - # grid width - grid_w = 20 - - """init parameters "init_people", "rich_threshold", and "reserve_percent" - are all set via Slider""" - - def __init__( - self, - height=grid_h, - width=grid_w, - init_people=2, - rich_threshold=10, - reserve_percent=50, - ): - super().__init__() - self.uid = next(self.id_gen) - self.height = height - self.width = width - self.init_people = init_people - - self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True) - # rich_threshold is the amount of savings a person needs to be considered "rich" - self.rich_threshold = rich_threshold - self.reserve_percent = reserve_percent - # see datacollector functions above - self.datacollector = mesa.DataCollector( - model_reporters={ - "Rich": get_num_rich_agents, - "Poor": get_num_poor_agents, - "Middle Class": get_num_mid_agents, - "Savings": get_total_savings, - "Wallets": get_total_wallets, - "Money": get_total_money, - "Loans": get_total_loans, - "Model Params": track_params, - "Run": track_run, - }, - agent_reporters={"Wealth": "wealth"}, - ) - - # create a single bank object for the model - self.bank = Bank(self, self.reserve_percent) - - # create people for the model according to number of people set by user - for i in range(self.init_people): - # set x coordinate as a random number within the width of the grid - x = self.random.randrange(self.width) - # set y coordinate as a random number within the height of the grid - y = self.random.randrange(self.height) - p = Person(i, (x, y), self, True, self.bank, self.rich_threshold) - # place the Person object on the grid at coordinates (x, y) - self.grid.place_agent(p, (x, y)) - - self.running = True - - def step(self): - # collect data - self.datacollector.collect(self) - # tell all the agents in the model to run their step function - self.agents.shuffle_do("step") - - def run_model(self): - for i in range(self.run_time): - self.step() - - -# parameter lists for each parameter to be tested in batch run -br_params = { - "init_people": [25, 100], - "rich_threshold": [5, 10], - "reserve_percent": 5, -} - - def main(): + # parameter lists for each parameter to be tested in batch run + br_params = { + "init_people": [25, 100], + "rich_threshold": [5, 10], + "reserve_percent": 5, + } + # The existing batch run logic here data = mesa.batch_run( BankReservesModel, diff --git a/examples/caching_and_replay/model.py b/examples/caching_and_replay/model.py index e4bc23c7..ede621c2 100644 --- a/examples/caching_and_replay/model.py +++ b/examples/caching_and_replay/model.py @@ -2,8 +2,9 @@ import mesa +from mesa.experimental.cell_space import CellAgent, OrthogonalMooreGrid -class SchellingAgent(mesa.Agent): +class SchellingAgent(CellAgent): """ Schelling segregation agent """ @@ -21,15 +22,13 @@ def __init__(self, model, agent_type): def step(self): similar = 0 - for neighbor in self.model.grid.iter_neighbors( - self.pos, moore=True, radius=self.model.radius - ): - if neighbor.type == self.type: + for agent in self.cell.get_neighborhood(radius=self.model.radius).agents: + if agent.type == self.type: similar += 1 # If unhappy, move: if similar < self.model.homophily: - self.model.grid.move_to_empty(self) + self.cell = self.model.grid.select_random_empty_cell() else: self.model.happy += 1 @@ -69,7 +68,7 @@ def __init__( self.homophily = homophily self.radius = radius - self.grid = mesa.space.SingleGrid(width, height, torus=True) + self.grid = OrthogonalMooreGrid((width, height), torus=True) self.happy = 0 self.datacollector = mesa.DataCollector( @@ -80,11 +79,11 @@ def __init__( # We use a grid iterator that returns # the coordinates of a cell as well as # its contents. (coord_iter) - for _, pos in self.grid.coord_iter(): + for cell in self.grid.all_cells: if self.random.random() < self.density: agent_type = 1 if self.random.random() < self.minority_pc else 0 agent = SchellingAgent(self, agent_type) - self.grid.place_agent(agent, pos) + agent.cell = cell self.datacollector.collect(self) diff --git a/examples/forest_fire/Forest Fire Model.ipynb b/examples/forest_fire/Forest Fire Model.ipynb index 189672ed..90722338 100644 --- a/examples/forest_fire/Forest Fire Model.ipynb +++ b/examples/forest_fire/Forest Fire Model.ipynb @@ -23,20 +23,36 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-10-12T18:20:45.476090Z", + "start_time": "2024-10-12T18:20:45.461936Z" + } + }, "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "%matplotlib inline\n", "\n", - "from mesa import Agent, Model\n", + "from mesa import Model\n", "from mesa.batchrunner import BatchRunner\n", - "from mesa.datacollection import DataCollector\n", - "from mesa.space import Grid" - ] + "from mesa.datacollection import DataCollector" + ], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'BatchRunner' from 'mesa.batchrunner' (/Users/jhkwakkel/Documents/GitHub/mesa/mesa/batchrunner.py)", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mImportError\u001B[0m Traceback (most recent call last)", + "Cell \u001B[0;32mIn[2], line 7\u001B[0m\n\u001B[1;32m 4\u001B[0m get_ipython()\u001B[38;5;241m.\u001B[39mrun_line_magic(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mmatplotlib\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124minline\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m 6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mmesa\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Model\n\u001B[0;32m----> 7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mmesa\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mbatchrunner\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m BatchRunner\n\u001B[1;32m 8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mmesa\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mdatacollection\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m DataCollector\n", + "\u001B[0;31mImportError\u001B[0m: cannot import name 'BatchRunner' from 'mesa.batchrunner' (/Users/jhkwakkel/Documents/GitHub/mesa/mesa/batchrunner.py)" + ] + } + ], + "execution_count": 2 }, { "cell_type": "markdown", @@ -46,54 +62,60 @@ "\n", "Most models consist of basically two things: agents, and an world for the agents to be in. The Forest Fire model has only one kind of agent: a tree. A tree can either be unburned, on fire, or already burned. The environment is a grid, where each cell can either be empty or contain a tree.\n", "\n", - "First, let's define our tree agent. The agent needs to be assigned **x** and **y** coordinates on the grid, and that's about it. We could assign agents a condition to be in, but for now let's have them all start as being 'Fine'. Since the agent doesn't move, and there is only at most one tree per cell, we can use a tuple of its coordinates as a unique identifier.\n", + "First, let's define our tree agent. The agent needs to be assigned a cell on the grid, and that's about it. We could assign agents a condition to be in, but for now let's have them all start as being 'Fine'. Since the agent doesn't move, we use `FixedAgent` as the parent class.\n", "\n", - "Next, we define the agent's **step** method. This gets called whenever the agent needs to act in the world and takes the *model* object to which it belongs as an input. The tree's behavior is simple: If it is currently on fire, it spreads the fire to any trees above, below, to the left and the right of it that are not themselves burned out or on fire; then it burns itself out. " + "Next, we define the agent's **step** method. This gets called whenever the agent needs to act in the world. The tree's behavior is simple: If it is currently on fire, it spreads the fire to any neighboring trees that are not burning or have not burned down." ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-10-12T18:22:50.333734Z", + "start_time": "2024-10-12T18:22:50.046775Z" + } + }, "source": [ - "class TreeCell(Agent):\n", + "from mesa.experimental.cell_space import FixedAgent\n", + "\n", + "\n", + "class TreeCell(FixedAgent):\n", " \"\"\"\n", " A tree cell.\n", "\n", " Attributes:\n", - " x, y: Grid coordinates\n", " condition: Can be \"Fine\", \"On Fire\", or \"Burned Out\"\n", - " unique_id: int\n", + "\n", " \"\"\"\n", "\n", - " def __init__(self, model, pos):\n", + " def __init__(self, model, cell):\n", " \"\"\"\n", " Create a new tree.\n", " Args:\n", - " pos: The tree's coordinates on the grid.\n", + " model: standard model reference for agent.\n", " \"\"\"\n", " super().__init__(model)\n", - " self.pos = pos\n", " self.condition = \"Fine\"\n", + " self.cell = cell\n", "\n", " def step(self):\n", " \"\"\"\n", " If the tree is on fire, spread it to fine trees nearby.\n", " \"\"\"\n", " if self.condition == \"On Fire\":\n", - " neighbors = self.model.grid.get_neighbors(self.pos, moore=False)\n", - " for neighbor in neighbors:\n", + " for neighbor in self.cell.neighborhood.agents:\n", " if neighbor.condition == \"Fine\":\n", " neighbor.condition = \"On Fire\"\n", - " self.condition = \"Burned Out\"" - ] + " self.condition = \"Burned Out\"\n" + ], + "outputs": [], + "execution_count": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now we need to define the model object itself. The main thing the model needs is the grid, which the trees are placed on. But since the model is dynamic, it also needs to include time -- it needs a schedule, to manage the trees activation as they spread the fire from one to the other.\n", + "Now we need to define the model object itself. The main thing the model needs is the grid, which the trees are placed on. We can choose different kinds of grids such as a von Neumann grid where any cell has 4 neighbors (left, right, top and bottom) or Moore grid where a cell has eigh neighbors.\n", "\n", "The model also needs a few parameters: how large the grid is and what the density of trees on it will be. Density will be the key parameter we'll explore below.\n", "\n", @@ -110,12 +132,15 @@ "metadata": {}, "outputs": [], "source": [ + "from mesa.experimental.cell_space import OrthogonalMooreGrid\n", + "\n", + "\n", "class ForestFire(Model):\n", " \"\"\"\n", " Simple Forest Fire model.\n", " \"\"\"\n", "\n", - " def __init__(self, width, height, density):\n", + " def __init__(self, width=100, height=100, density=0.65, seed=None):\n", " \"\"\"\n", " Create a new forest fire model.\n", "\n", @@ -123,9 +148,12 @@ " width, height: The size of the grid to model\n", " density: What fraction of grid cells have a tree in them.\n", " \"\"\"\n", + " super().__init__(seed=seed)\n", + "\n", " # Set up model objects\n", - " self.grid = Grid(width, height, torus=False)\n", - " self.dc = DataCollector(\n", + "\n", + " self.grid = OrthogonalMooreGrid((width, height), capacity=1)\n", + " self.datacollector = DataCollector(\n", " {\n", " \"Fine\": lambda m: self.count_type(m, \"Fine\"),\n", " \"On Fire\": lambda m: self.count_type(m, \"On Fire\"),\n", @@ -134,23 +162,25 @@ " )\n", "\n", " # Place a tree in each cell with Prob = density\n", - " for x in range(self.width):\n", - " for y in range(self.height):\n", - " if self.random.random() < density:\n", - " # Create a tree\n", - " new_tree = TreeCell(self, (x, y))\n", - " # Set all trees in the first column on fire.\n", - " if x == 0:\n", - " new_tree.condition = \"On Fire\"\n", - " self.grid[x][y] = new_tree\n", + " for cell in self.grid.all_cells:\n", + " if self.random.random() < density:\n", + " # Create a tree\n", + " new_tree = TreeCell(self, cell)\n", + " # Set all trees in the first column on fire.\n", + " if cell.coordinate[0] == 0:\n", + " new_tree.condition = \"On Fire\"\n", + "\n", " self.running = True\n", + " self.datacollector.collect(self)\n", "\n", " def step(self):\n", " \"\"\"\n", " Advance the model by one step.\n", " \"\"\"\n", " self.agents.shuffle_do(\"step\")\n", - " self.dc.collect(self)\n", + " # collect data\n", + " self.datacollector.collect(self)\n", + "\n", " # Halt if no more fire\n", " if self.count_type(self, \"On Fire\") == 0:\n", " self.running = False\n", @@ -160,11 +190,7 @@ " \"\"\"\n", " Helper method to count trees in a given condition in a given model.\n", " \"\"\"\n", - " count = 0\n", - " for tree in model.agents:\n", - " if tree.condition == tree_condition:\n", - " count += 1\n", - " return count" + " return len(model.agents.select(lambda x: x.condition == tree_condition))\n" ] }, { diff --git a/examples/pd_grid/pd_grid/model.py b/examples/pd_grid/pd_grid/model.py index 5874fed7..38ef5f5b 100644 --- a/examples/pd_grid/pd_grid/model.py +++ b/examples/pd_grid/pd_grid/model.py @@ -28,7 +28,6 @@ def __init__( """ super().__init__(seed=seed) self.activation_order = activation_order - # self.grid = mesa.space.SingleGrid(width, height, torus=True) self.grid = OrthogonalMooreGrid((width, height), torus=True) if payoffs is not None: diff --git a/examples/sugarscape_cg/sugarscape_cg/model.py b/examples/sugarscape_cg/sugarscape_cg/model.py index ddb4b3d3..8569320b 100644 --- a/examples/sugarscape_cg/sugarscape_cg/model.py +++ b/examples/sugarscape_cg/sugarscape_cg/model.py @@ -42,7 +42,6 @@ def __init__(self, width=50, height=50, initial_population=100, seed=None): self.height = height self.initial_population = initial_population - # self.grid = mesa.space.MultiGrid(self.width, self.height, torus=False) self.grid = OrthogonalVonNeumannGrid((self.width, self.height), torus=True) self.datacollector = mesa.DataCollector( {"SsAgent": lambda m: len(m.agents_by_type[SsAgent])}