Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- random.seed()
- job_states = {
- 1: 'unemployed',
- 2: 'employed'
- }
- job_transitions = {
- 1: (1, 1, 2, 10, 'Found a job!'),
- 2: (2, 2, 1, -25, 'Fired from job.'),
- 3: (3, 2, 2, 2, 'Did some work.'),
- 4: (4, 1, 1, -2, 'Didn''t find a job.')
- }
- job_enums = {
- 'HIRED': 1,
- 'FIRED': 2,
- 'WORK': 3,
- 'NOJOB': 4
- }
- class Manager:
- def __init__(self, states, transitions):
- self.states = states
- self.transitions = transitions
- self.map = {}
- for (uid, initial, a, b, c) in self.transitions.values():
- if initial not in self.map:
- self.map[initial] = []
- self.map[initial].append(uid)
- self.modifiers = {
- job_enums['WORK']: lambda x: 4,
- job_enums['NOJOB']: lambda x: 2,
- job_enums['HIRED']: lambda x: 1,
- job_enums['FIRED']: lambda x: 1
- }
- def get_actions(self, age):
- actions = []
- for state in self.states:
- if state in self.map:
- for transition in self.map[state]:
- if transition in self.modifiers:
- actions.append([transition, self.modifiers[transition](age)])
- else:
- actions.append([transition, 1])
- return actions
- def process_action(self, transition):
- if transition in self.transitions:
- (uid, initial, final, a, b) = self.transitions[transition]
- if initial in self.states:
- self.states.remove(initial)
- self.states.append(final)
- def get_states_text(states, lookup):
- states_text = []
- for state in states:
- if state in lookup:
- states_text.append(lookup[state])
- return states_text
- def get_action_text(transition, lookup):
- if transition in lookup:
- return lookup[transition][4]
- return ''
- job_manager = Manager([1], job_transitions)
- managers = [job_manager]
- transitions = [job_transitions]
- master_transitions = {}
- for transition_dict in transitions:
- master_transitions.update(transition_dict)
- run = True
- while run:
- actions = []
- for manager in managers:
- actions.extend(manager.get_actions(0))
- counter = 0
- for action in actions:
- counter += action[1]
- random_counter = random.randint(1, counter)
- random_action = None
- for action in actions:
- random_counter -= action[1]
- if random_counter <= 0:
- random_action = action[0]
- break
- for manager in managers:
- manager.process_action(random_action)
- print(get_action_text(random_action, master_transitions))
- while True:
- user_input = input('Enter c to continue or q to quit:')
- if user_input == "c":
- break
- if user_input == "q":
- run = False
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement