File: market.st

package info (click to toggle)
gnu-smalltalk 3.1-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,300 kB
  • ctags: 13,999
  • sloc: ansic: 88,106; sh: 23,223; asm: 7,889; perl: 4,493; cpp: 3,539; awk: 1,483; yacc: 1,355; xml: 1,272; makefile: 1,192; lex: 843; sed: 258; objc: 124
file content (398 lines) | stat: -rw-r--r-- 7,298 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"Market balancing price model"

"smalltalk version 1.9"

"6/1/95"

"copyright 1995, Richard E. Hawkins"

"all rights reserved"



Object subclass: Agent [
    | cash doneForDay inventory id |
    
    <category: nil>
    <comment: 'I am the root agent class for AL models!'>

    lastID := nil.
    hour := nil.
    date := nil.
    nHours := nil.
    World := nil.
    nCostToShop := nil.

    Agent class >> classInit: modelSpecs [
	"how boring"

	<category: 'instance creation'>
	
    ]

    Agent class >> new [
	<category: 'instance creation'>
	| r |
	r := super new.
	r init.
	^r
    ]

    Agent class >> newID [
	"this returns a new, unique, id number"

	<category: 'instance creation'>
	lastID := lastID + 1.
	^lastID
    ]

    init [
	<category: 'instance initialization'>
	id := Agent newID.
	cash := 0.
	inventory := 0
	"'Whee!  ive been inited' printNl"
    ]

    tellID [
	<category: 'methods for variable acces'>
	^id
    ]

    tellInv [
	<category: 'methods for variable acces'>
	^inventory
    ]

    tellCash [
	<category: 'methods for variable acces'>
	^cash
    ]

    setCash: newCash [
	<category: 'methods for variable acces'>
	cash := newCash
    ]

    perish [
	<category: 'methods for variable acces'>
	inventory := 0
    ]

    addCash: howMuch [
	<category: 'methods for variable acces'>
	cash := cash + howMuch
    ]

    newDay [
	<category: 'methods for variable acces'>
	self perish.
	doneForDay = false
    ]
]



"the person will be the dominant agent type"



Agent subclass: Person [
    
    <category: nil>
    <comment: 'I am a person, and I live in Hawkinsville.'>

    lastID := nil.
    nDailyCash := nil.
    initYet := nil.

    Person class >> new [
	<category: 'instance creation'>
	| r |
	initYet = nil 
	    ifTrue: 
		[nDailyCash := 20000.
		initYet := true].
	r := super new.
	r init.
	^r
    ]

    init [
	<category: 'instance initialization'>
	^super init
    ]
]



"!Person methodsFor: 'methods for daily initialization'!"

"nothing here, really"

"! !"

" *** end of class Person *** "



Person subclass: Merchant [
    | myPrices myTransactions mySales myLostTransactions myLostSales gHrTolerance gHrCh0 gHrCh1 gHrCh2 |
    
    <category: nil>
    <comment: 'I am a Merchant, and I live in Hawkinsville.'>

    nDailyInventory := nil.
    lastID := nil.
    hChanges := nil.
    dChanges := nil.

    Merchant class >> classInit: modelSpecs [
	<category: 'instance creation'>
	lastID := 0.
	nDailyInventory := modelSpecs tellDailyInventory
    ]

    Merchant class >> new [
	<category: 'instance creation'>
	| r |
	r := super new.
	r init.
	^r
    ]

    init: modelSpecs [
	<category: 'instance initialization'>
	super init.

	"an array to keep track of prices by hour"
	myPrices := Array new: 5	"nHours"
    ]

    newDay [
	<category: 'methods for daily initialization'>
	| temp |
	super NewDay.	"get general agent newday stuff"
	inventory = nDailyInventory.

	"put the adjusted closing price into a temporary spot"
	temp := myPrices at: nHours + 1.

	"flush the data"
	[:array | array atAllPut: 0] myPrices myTransactions mySales 
	    myLostTransactions myLostSale
    ]

    tellPrice [
	<category: 'methods for general behavior'>
	^myPrices at: hour
    ]

    endHour [
	<category: 'methods for general behavior'>
	self setPrice.
	super endHour
    ]

    endOfDay [
	"end of day merchant behavior"

	"compile the days information"

	"this will compile the data for each log"

	"additionally, it will compile to the master log, whose"

	"identity is known to the class"

	<category: 'methods for general behavior'>
	[:dayLog | dayLog sumUp] myPrices myTransactions mySales 
	    myLostTransactions myLostSale.
	self setPrice.
	super endOfDay
    ]

    setPrice [
	<category: 'methods for general behavior'>
	hour = 'endOfDay' ifTrue: [self setPriceDay] ifFalse: [self setPriceHour]
    ]

    setPriceHour [
	<category: 'methods for general behavior'>
	| dev change |
	dev := self myDev d
    ]

    gHrTolerance [
	<category: 'methods for general behavior'>
	gHrCh0 gHrCh1 gHrCh2
    ]

    myDev [
	"write this!!! ***"

	<category: 'methods for general behavior'>
	
    ]
]



" *** end of class Merchant *** "

"The world is of course populated with Consumers"



Person subclass: Consumer [
    | trysLeft |
    
    <category: nil>
    <comment: 'I am a Consumer, and I live in Hawkinsville.'>

    nDailyCash := nil.
    lastID := nil.
    sinboy := nil.
    nTotMerchants := nil.
    yestPrice := nil.

    Consumer class >> classInit: modelSpecs [
	<category: 'instance creation'>
	lastID := 0.
	nDailyCash := modelSpecs tellDailyCash
    ]

    Consumer class >> new [
	<category: 'instance creation'>
	| r |
	r := super new.
	r init.
	^r
    ]

    init [
	<category: 'instance initialization'>
	^super init
    ]

    at: key put: value [
	<category: 'general behavior'>
	
    ]

    buyStuff: howMany at: price from: myMerch [
	"make sure he can afford it"

	<category: 'general behavior'>
	| iGot quant |
	cash < (price * howMany) 
	    ifTrue: [quant := cash / price]
	    ifFalse: [quant := howMany copy].
	iGot := myMerch sell: quant.
	inventory := inventory + iGot.
	cash := cash - (myMerch getPaid: iGot * price).
	^iGot
    ]

    newDay [
	<category: 'general behavior'>
	super newDay.
	cash := nDailyCash.
	"give us this day. . ."
	World rShopSchedule: self
	"schedule me to shop at a random time"
    ]

    shop [
	"it is definitely time to shop"

	"note that the c++ version called him daily and he checked to see if"

	"it was time to shop"

	"and for that matter, this section could be probably halved in lenght;"

	"this is essentially the c++ code"

	<category: 'general behavior'>
	| myMerch price demand yestPrDemand |
	myMerch := World randMerch.
	price := myMerch tellPrice.
	demand := (self myDemand: myMerch price) - inventory.
	demand < 0 ifTrue: [demand := 0].

	"is it worth buying here?"
	yestPrDemand = self demand: yestPrice.
	price > ((yestPrice + nCostToShop) / yestPrDemand) & (hour < nHours) 
	    & (trysLeft > 0) & (demand > 0) 
	    ifTrue: 
		["this guy is too expensive, try next hour"

		self abortShop]
	    ifFalse: 
		[self 
		    buyStuff: demand
		    at: price
		    from: myMerch].
	inventory < demand ifTrue: [doneForDay := true]
    ]

    myDemand: price [
	"the consumer's demand at the given price"

	"perhaps this can inherit a code segment from modelSpecs"

	<category: 'general behavior'>
	| dem |
	date < 10 
	    ifTrue: [dem := 200 - price]
	    ifFalse: [dem := (200 - price + 25) * sinboy].
	^dem
    ]

    abortShop [
	<category: 'general behavior'>
	cash := cash - nCostToShop.
	trysLeft := trysLeft - 1.
	World shopReSchedule: self.
	^True
    ]
]



"the DayLog class is to keep track of & total hourly transactions"



Array subclass: DayLog [
    | carryOver total |
    
    <category: nil>
    <comment: 'DayLogs are extended arrays with also  endOfDay & total elements'>

    DayLog class >> new [
	<category: 'instance creation'>
	| r |
	r := super new.
	r init.
	^r
    ]

    init [
	<category: 'instance initialization'>
	^super init
    ]

    endOfDay [
	"[:array | array atAllPut: 0 ]
	 
	 myPrices myTransactions mySales myLostTransactions myLostSale"

	"sum the elements into Total"

	<category: 'general behavior'>
	
    ]
]