-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathai.py
482 lines (333 loc) · 14.3 KB
/
ai.py
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import math
import random
import business as bu
import database as d
import jobs
import people as p
import unit as u
#singleton
class StartupAI(object):
def __init__(self):
None
#O(n^2). Not ideal, but n SHOULD be low. Can we make O(n)?
def whatToBuild(self, investor):
# knownStock = self.knownStock(investor)
#current dumb ai will build full product lines, so all are equally possible.
# possibilities = self.possibilities(investor, knownStock)
possibilities = self.tempPossibilities(investor)
optimalList = self.optimalList(investor)
bestPossible = self.bestPossible(possibilities, optimalList)
return bestPossible
def knownStock(self, investor):
requiredStock = d.getAllComponents()
manuProfiles = investor.getKnownManus()
knownStock = [False for i in requiredStock]
for manu in manuProfiles:
priceList = manu.getPrices()
for i in range(len(priceList)):
if priceList[i] != 0:
knownStock[i] = True
return knownStock
def tempPossibilities(self, investor):
possibilities = [True for i in d.getMaterials()]
for business in investor.getBusinesses():
for order in business.getCraftOrders():
possibilities[order.getProductIndex()] = False
return possibilities
#which products can the investor find stock for?
#O(n^2)- not exactly, but multivariate
def possibilities(self, investor, knownStock):
requiredStock = d.getAllComponents()
possibilities = [False for i in requiredStock]
for i in range(len(requiredStock)):
target = requiredStock[i]
hasStock = True
for componentRatio in target:
component = componentRatio[0]
if not knownStock[component]:
hasStock = False
if hasStock:
possibilities[i] = True
return possibilities
#people shop for the thing they want the most. So they want to open businesses with that in mind- what will bring in the most customers?
def optimalList(self, investor):
peopleProfiles = investor.getKnownPeople()
highestMU = [0 for i in d.getMaterials()]
for profile in peopleProfiles:
muList = profile.getMuList()[0]
maxMu = muList.index(max(muList))
if muList[maxMu] != 0:
highestMU[maxMu] += 1
return highestMU
def bestPossible(self, possibilities, optimalList):
notDone = True
while notDone:
bestOption = optimalList.index(max(optimalList))
if optimalList[bestOption] == 0:
bestOption = None
notDone = False
if bestOption is not None:
if possibilities[bestOption]:
notDone = False
else:
optimalList[bestOption] = 0
return bestOption
#factory for supply chains.
class Builder(object):
def __init__(self, model):
self.model = model
def initial_demand(self, unit):
for i in range(len(unit.can_make)):
if unit.can_make[i]:
unit.failSales[i] = 500
def jobMaker(self, unit):
unitJobLink = { "Farm" : jobs.Farmer,
"Brewery" : jobs.Brewer,
"Mill" : jobs.Miller,
"Bakery" : jobs.Baker,
"Lumberyard": jobs.Lumberjack,
"Joinery" : jobs.Carpenter}
slots = 10
salary = 6
newJob = unitJobLink[unit.getUnitType()](slots, unit.getBusiness(), unit, salary)
return newJob
#also makes transfer orders
def craftOrderMaker(self, job):
# final commas needed so they're all tuples
jobProductLink = {"Farmer" : (d.GRAIN_INDEX,),
"Miller" : (d.FLOUR_INDEX,),
"Baker" : (d.BREAD_INDEX,),
"Brewer" : (d.BEER_INDEX,),
"Lumberjack" : (d.LUMBER_INDEX,),
"Carpenter" : (d.CHAIR_INDEX, d.TABLE_INDEX)}
business = job.getBusiness()
unit = job.getUnit()
materialIndexList = jobProductLink[job.getJobType()]
for i in materialIndexList:
amount = 10
craftOrder = business.craftOrderManager(job, i)
craftOrder.setAmount(10)
transferOrder = business.transferOrderManager(unit, i)
transferOrder.setAmount(10)
def buildIt(self, business, locality, toBuild):
unitLocation = locality.find_property()
existingUnits = business.getUnits()
nonesuch = True
for unit in existingUnits:
unitType = unit.getUnitType()
if (unitType == toBuild.unitType):
nonesuch = False
if nonesuch:
unitName = business.owners[0].lastname + " " + toBuild.unitType
newUnit = toBuild(unitName, locality, unitLocation, business)
locality.claim_node(unitLocation, newUnit)
self.initial_demand(newUnit)
new_job = self.jobMaker(newUnit)
self.craftOrderMaker(new_job)
if newUnit.unitType == "Farm":
self.giveGrain(newUnit)
def giveGrain(self, unit):
unit.addStock(d.GRAIN_INDEX, 100000)
#for now, there is no startup cost
def buildChain(self, business, toBuild):
locality = business.getLocality()
chains = [
[u.Farm],
[u.Mill, u.Farm],
[u.Brewery, u.Farm],
[u.Bakery, u.Mill, u.Farm],
[],
[],
[u.Lumberyard],
[u.Joinery, u.Lumberyard],
[u.Joinery, u.Lumberyard]
]
if toBuild is not None:
for target in chains[toBuild]:
self.buildIt(business, locality, target)
def newBusiness(self, boss, busiName=None, capital=1000000):
newBus = None
if busiName is None:
busiName = boss.lastname + " " + random.choice(d.busiNameList)
if boss.capital >= capital:
boss.capital -= capital
newBus = bu.Business([boss], busiName, boss.locality, capital)
boss.businesses.append(newBus)
startupHQ = boss.getHome()
OwnerJob = jobs.Owner(newBus, startupHQ)
self.model.hirer.jobApplication(boss, OwnerJob)
if boss != self.model.char:
i_build = self.model.startupAI.whatToBuild(boss)
self.model.builder.buildChain(boss.businesses[0], i_build)
return newBus
class Character(p.People):
def __init__(self, model, firstname, lastname, theirGender, theirHometown, theirHome,theirReligion):
p.People.__init__(self, model, firstname, lastname, theirGender, theirHometown, theirHome, theirReligion)
def startBusiness(self, busiName, capital=3000):
newBusiness = self.model.builder.newBusiness(self, busiName, capital)
return newBusiness
def build(self, business, unit, unitName):
units = {"farm" : u.Farm, "bakery" : u.Bakery, "mill" : u.Mill, "warehouse" : u.Warehouse, "church" : u.Church}
if business in self.businesses:
newUnit = units[unit](unitName, business.getLocality(), business)
return newUnit
def run_day(self):
self.model.clock.runDay()
class ProductionAI(object):
def __init__(self, model):
self.model = model
def setProduction(self, business):
for job in business.getCraftingJobs():
jobUnit = job.getUnit()
demand = jobUnit.getTotalDemand()
for i in range(len(demand)):
if (jobUnit.can_make[i] and demand[i] > 0):
#crafted
if d.is_crafted(i):
self.setCraftOrder(business, job, demand[i], i)
#planted
elif d.is_planted(i):
self.setPlantOrder(business, job, i)
self.setHarvestOrder(business, job, i)
def setCraftOrder(self, business, job, demand, i):
jobUnit = job.getUnit()
order = business.craftOrderManager(job, i)
transfer = business.transferOrderManager(jobUnit, i)
sellDemand = jobUnit.sales[i] + jobUnit.failSales[i]
for component in d.getComponents(i):
transport = business.transportOrderManager(jobUnit, component[0])
if transport is not None:
needed = component[1] * demand
transport.setAmount(needed)
else:
#build unit?
pass
#they just flood the market if demand drops, which should raise demand. Nice and simple.
if order.getAmount() < demand:
order.setAmount(demand)
transfer.setAmount(sellDemand)
def setPlantOrder(self, business, job, i):
jobUnit = job.getUnit()
order = business.craftOrderManager(job, i)
transfer = business.transferOrderManager(jobUnit, i)
grow_days = jobUnit.incubator.getGrowDays(i)
avgDemand = jobUnit.bigdata.getAvgDemand(i)
ratio = jobUnit.incubator.getRatio(i)
amount = avgDemand / ratio
#get components- does nothing if none.
for component in d.getComponents(i):
transport = business.transportOrderManager(jobUnit, component[0])
if transport is not None:
needed = component[1] * amount
transport.setAmount(needed)
else:
#build unit?
pass
if order.getAmount() < (amount * grow_days):
order.setAmount(amount * grow_days)
transfer.setAmount(avgDemand)
def setHarvestOrder(self, business, job, i):
order = business.harvestOrderManager(job, i)
class JobPoster(object):
def __init__(self, model):
self.model = model
# don't fire anyone for now, just go bankrupt- that's fine. AI is loyal. (Otherwise they may not get them back.)
def managePositions(self, job):
slots = job.slots
employees = job.employees
totalWorkers = 0
for order in (x for x in job.business.craftOrders if x.job == job):
amount = order.amount
materialIndex = order.materialIndex
tech = job.unit.getTech(materialIndex)
if d.is_planted(materialIndex):
#divide by length of planting season, not grow_days- how?
grow_days = job.unit.incubator.getGrowDays(materialIndex)
workers = math.ceil(amount / (tech * grow_days))
elif d.is_crafted(materialIndex):
workers = math.ceil(amount / tech)
totalWorkers += workers
newSlots = totalWorkers - len(job.employees)
job.setSlots(newSlots if newSlots > 0 else 0)
class Hirer(object):
def __init__(self, model):
self.model = model
#later, be more selective
def jobApplication(self, nominee, job):
isHired = self.hire(nominee, job)
return isHired
def hire(self, hiree, job):
isHired = False
business = job.business
jobSlots = job.getSlots()
employeeList = job.getEmployees()
if (jobSlots >= 1):
if (hiree.getJob() == None):
isHired = True
hiree.setJob(job, job.salary)
job.decrementSlots()
employeeList.append(hiree)
business.m_employees.append(hiree)
business.incrementHired(job.salary)
#thoughts
if isHired:
hiree.think("I got hired by " + business.name + " as a " + job.jobType + ". I'll be working at " + job.unit.name + ".")
else:
hiree.think(job.business.name + " tried to hire me as a " + job.jobType + ", but it didn't work out.")
return isHired
#never used- no one is ever fired and that's okay.
class Firer(object):
def __init__(self, model):
self.model = model
def fire(self, firee, job):
isFired = False
fireeJob = firee.getJob()
business = fireeJob.getUnit().getBusiness()
if (fireeJob == job):
isFired = True
firee.unsetJob()
job.incrementSlots()
employeeList = job.getEmployees()
employeeList.remove(firee)
business.m_employees.remove(firee)
for employee in employeeList:
fireeNewJob = firee.getJob()
#update employee's profile of firee
fireeProfile = employee.peopleManager(firee)
dayNum = job.unit.locality.clock.getDayNum()
fireeProfile.updateJob(fireeNewJob, dayNum)
if isFired:
firee.think("I got fired from my job today.")
return isFired
class SalaryPayer(object):
def __init__(self, model):
self.model = model
def paySalaries(self):
for unit in d.getUnit():
laborCost = 0
for job in unit.getJobList():
totalSalary = 0
totalEmployees = 0
allWell = True
#calculate
for employee in job.employees:
# empBusiness = employee.getJob().getUnit().getBusiness()
salary = employee.getSalary()
# if (empBusiness == job.business):
totalSalary += salary
totalEmployees += 1
# else:
# allWell = False
#validate and pay
if job.business.canAfford(totalSalary):
job.business.pay(totalSalary)
for employee in job.employees:
employee.addCapital(salary)
employee.think("I got paid " + str(salary) + " ducats today by " + job.business.name + ".")
else:
allWell = False
#unit needs totalSalary to calculate labor costs- if not paid, no cost, right?
if allWell == False:
totalSalary = 0
laborCost += totalSalary
unit.setLaborTotal(laborCost)