@@ -8,27 +8,27 @@ import flash.geom.Point;
88
99/**
1010 * An agent that performs the basic logic from the Doran & Parberry article
11- * @author
11+ * @author
1212 */
1313class Agent extends BasicAgent
1414{
1515 public static inline var SIGNIFICANT : Float = 0.25 ; // 25% more or less is "significant"
1616 public static inline var SIG_IMBALANCE : Float = 0.33 ;
1717 public static inline var LOW_INVENTORY : Float = 0.1 ; // 10% of ideal inventory = "LOW"
1818 public static inline var HIGH_INVENTORY : Float = 2.0 ; // 200% of ideal inventory = "HIGH"
19-
19+
2020 public static inline var MIN_PRICE : Float = 0.01 ; // lowest possible price
21-
22- public function new (id : Int , data : AgentData )
21+
22+ public function new (id : Int , data : AgentData )
2323 {
2424 super (id , data );
2525 }
26-
27- override public function createBid (bazaar : Market , good : String , limit : Float ): Offer
26+
27+ override public function createBid (bazaar : Market , good : String , limit : Float ): Offer
2828 {
2929 var bidPrice : Float = determinePriceOf (good );
3030 var ideal : Float = determinePurchaseQuantity (bazaar , good );
31-
31+
3232 // can't buy more than limit
3333 var quantityToBuy : Float = ideal > limit ? limit : ideal ;
3434 if (quantityToBuy > 0 )
@@ -37,12 +37,12 @@ class Agent extends BasicAgent
3737 }
3838 return null ;
3939 }
40-
41- override public function createAsk (bazaar : Market , commodity_ : String , limit_ : Float ): Offer
40+
41+ override public function createAsk (bazaar : Market , commodity_ : String , limit_ : Float ): Offer
4242 {
4343 var ask_price : Float = determinePriceOf (commodity_ );
4444 var ideal : Float = determineSaleQuantity (bazaar , commodity_ );
45-
45+
4646 // can't sell less than limit
4747 var quantity_to_sell : Float = ideal < limit_ ? limit_ : ideal ;
4848 if (quantity_to_sell > 0 )
@@ -51,8 +51,8 @@ class Agent extends BasicAgent
5151 }
5252 return null ;
5353 }
54-
55- override public function generateOffers (bazaar : Market , commodity : String ): Void
54+
55+ override public function generateOffers (bazaar : Market , commodity : String ): Void
5656 {
5757 var offer : Offer ;
5858 var surplus : Float = _inventory .surplus (commodity );
@@ -69,7 +69,7 @@ class Agent extends BasicAgent
6969 var shortage : Float = _inventory .shortage (commodity );
7070 var space : Float = _inventory .getEmptySpace ();
7171 var unit_size : Float = _inventory .getCapacityFor (commodity );
72-
72+
7373 if (shortage > 0 && space >= unit_size )
7474 {
7575 var limit : Float = 0 ;
@@ -81,7 +81,7 @@ class Agent extends BasicAgent
8181 {
8282 limit = Math .floor (space / shortage );
8383 }
84-
84+
8585 if (limit > 0 )
8686 {
8787 offer = createBid (bazaar , commodity , limit );
@@ -93,26 +93,26 @@ class Agent extends BasicAgent
9393 }
9494 }
9595 }
96-
97- override public function updatePriceModel (bazaar : Market , act : String , good : String , success : Bool , unitPrice : Float = 0 ): Void
96+
97+ override public function updatePriceModel (bazaar : Market , act : String , good : String , success : Bool , unitPrice : Float = 0 ): Void
9898 {
9999 var observed_trades : Array <Float >;
100-
100+
101101 if (success )
102102 {
103103 // Add this to my list of observed trades
104104 observed_trades = _observedTradingRange .get (good );
105105 observed_trades .push (unitPrice );
106106 }
107-
107+
108108 var public_mean_price : Float = bazaar .getAverageHistoricalPrice (good , 1 );
109-
109+
110110 var belief : Point = getPriceBelief (good );
111111 var mean : Float = (belief .x + belief .y ) / 2 ;
112112 var wobble : Float = 0.05 ;
113-
113+
114114 var delta_to_mean : Float = mean - public_mean_price ;
115-
115+
116116 if (success )
117117 {
118118 if (act == " buy" && delta_to_mean > SIGNIFICANT ) // overpaid
@@ -125,19 +125,19 @@ class Agent extends BasicAgent
125125 belief .x - = delta_to_mean / 2 ; // SHIFT towards mean
126126 belief .y - = delta_to_mean / 2 ;
127127 }
128-
128+
129129 belief .x + = wobble * mean ; // increase the belief's certainty
130130 belief .y - = wobble * mean ;
131131 }
132132 else
133133 {
134134 belief .x - = delta_to_mean / 2 ; // SHIFT towards the mean
135135 belief .y - = delta_to_mean / 2 ;
136-
136+
137137 var special_case : Bool = false ;
138138 var stocks : Float = queryInventory (good );
139139 var ideal : Float = _inventory .ideal (good );
140-
140+
141141 if (act == " buy" && stocks < LOW_INVENTORY * ideal )
142142 {
143143 // very low on inventory AND can't buy
@@ -150,34 +150,34 @@ class Agent extends BasicAgent
150150 wobble * = 2 ; // ask more liberally
151151 special_case = true ;
152152 }
153-
153+
154154 if (! special_case )
155155 {
156156 // Don't know what else to do? Check supply vs. demand
157157 var asks : Float = bazaar .history .asks .average (good ,1 );
158158 var bids : Float = bazaar .history .bids .average (good ,1 );
159-
159+
160160 // supply_vs_demand: 0=balance, 1=all supply, -1=all demand
161161 var supply_vs_demand : Float = (asks - bids ) / (asks + bids );
162-
162+
163163 // too much supply, or too much demand
164164 if (supply_vs_demand > SIG_IMBALANCE || supply_vs_demand < - SIG_IMBALANCE )
165165 {
166166 // too much supply: lower price
167167 // too much demand: raise price
168-
168+
169169 var new_mean = public_mean_price * (1 - supply_vs_demand );
170170 delta_to_mean = mean - new_mean ;
171-
171+
172172 belief .x - = delta_to_mean / 2 ; // SHIFT towards anticipated new mean
173173 belief .y - = delta_to_mean / 2 ;
174174 }
175175 }
176-
176+
177177 belief .x - = wobble * mean ; // decrease the belief's certainty
178178 belief .y + = wobble * mean ;
179179 }
180-
180+
181181 if (belief .x < MIN_PRICE )
182182 {
183183 belief .x = MIN_PRICE ;
@@ -187,4 +187,4 @@ class Agent extends BasicAgent
187187 belief .y = MIN_PRICE ;
188188 }
189189 }
190- }
190+ }
0 commit comments