def test_changing_name_of_an_employee(): db = MemoryDB() empId = AddHourlyEmployee("wrong", "Home", 1, db).execute() cnt = ChangeName(empId, "Bill", db) cnt.execute() e = db.get_employee(empId) assert e.name == "Bill"
def test_changing_an_address_of_an_employee(): db = MemoryDB() empId = AddHourlyEmployee("Bohn", "wrong", 1, db).execute() cat = ChangeAddress(empId, "Home", db) cat.execute() e = db.get_employee(empId) assert e is not None assert e.address == "Home"
def test_adding_a_service_charge_to_a_member(): db = MemoryDB() empId = AddSalariedEmployee("Bill", "Home", 1000, db).execute() e = db.get_employee(empId) ChangeUnionMember(empId, 10.0, db).execute() sct = AddServiceCharge(e.memberId, date.today(), 12.95, db) sct.execute() sc = e.affiliation.get_service_charge(date.today()) assert sc.charge == 12.95 assert sc.date == date.today()
def test_changing_an_employee_to_be_in_a_union_affiliation(): db = MemoryDB() empId = AddHourlyEmployee("Jim", "Home", 12.25, db).execute() cut = ChangeUnionMember(empId, 99.42, db) cut.execute() e = db.get_employee(empId) af = e.affiliation assert af is not None assert af.dues == 99.42 member = db.get_union_member(e.memberId) assert member is e
def test_changing_an_address_of_an_employee(): db = MemoryDB() empId = AddSalariedEmployee("Bohn", "wrong", 1500, db).execute() cs = ChangeHourly(empId, 12.25, db) cs.execute() e = db.get_employee(empId) pc = e.classification assert isinstance(pc, HourlyClassification) assert pc.hourly_rate == 12.25 ps = e.schedule assert isinstance(ps, WeeklySchedule)
def test_deducting_service_charges(): db = MemoryDB() empId = AddCommissionedEmployee("Bob", "Home", 10, 1000, db).execute() ChangeUnionMember(empId, 99.42, db).execute() mId = db.get_employee(empId).memberId pay_date = datetime.date(2001, 11, 16) AddServiceCharge(mId, pay_date, 10, db).execute() pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.gross_pay == 1000 print pc.deductions assert pc.deductions == 109.42 assert pc.net_pay == 1000 - 109.42
def test_removing_affiliations_from_employee(): db = MemoryDB() empId = AddHourlyEmployee("Jim", "Home", 12.25, db).execute() ChangeUnionMember(empId, 99.42, db).execute() cut = ChangeUnaffiliated(empId, db) cut.execute() e = db.get_employee(empId) assert isinstance(e.affiliation, NoAffiliation) try: db.get_union_member(e.memberId) raise "member not deleted" except KeyError: pass
def test_changing_an_address_of_an_employee(): db = MemoryDB() empId = AddHourlyEmployee("Bohn", "wrong", 1, db).execute() cc = ChangeCommissioned(empId, 2.5, 1000, db) cc.execute() e = db.get_employee(empId) pc = e.classification assert isinstance(pc, CommissionedClassification) assert pc.baseRate == 1000 assert pc.commission == 2.5 ps = e.schedule assert isinstance(ps, BiWeeklySchedule)
def setUp(): global view view = MockAddEmployeeView() container = TransactionContainer() db = MemoryDB() global presenter presenter = AddEmployeePresenter(view, container, db)
def test_adding_a_time_card(): db = MemoryDB() t = AddHourlyEmployee("Bill", "Home", 15.25, db) empId = t.execute() date = datetime.date(2005, 3, 30) tct = AddTimeCard(empId, date, 8.0, db) tct.execute() e = db.get_employee(empId) assert e is not None pc = e.classification assert isinstance(pc, HourlyClassification) tc = pc.get_time_card(date) assert tc.hours == 8.0 assert tc.date == date
def test_adding_an_hourly_paid_employee(): db = MemoryDB() at = AddHourlyEmployee("John", "Home", 15.75, db) empId = at.execute() e = db.get_employee(empId) assert e is not None assert e.id == empId assert e.name == "John" assert e.address == "Home" pc = e.classification assert isinstance(pc, HourlyClassification) assert pc.hourly_rate == 15.75 ps = e.schedule assert isinstance(ps, WeeklySchedule) pm = e.method assert isinstance(pm, HoldMethod)
def test_adding_a_sales_receipt(): db = MemoryDB() t = AddCommissionedEmployee("John", "Home", 5.0, 1500, db) empId = t.execute() date = datetime.date(2005, 3, 30) srt = AddSalesReceipt(empId, date, 500, db) srt.execute() e = db.get_employee(empId) assert e is not None pc = e.classification assert isinstance(pc, CommissionedClassification) srs = pc.get_sales_receipts() assert srs[0].amount == 500 assert srs[0].date == date
def test_adding_an_employee_paid_by_salary(): db = MemoryDB() t = AddSalariedEmployee("Jim", "Work", 2000.0, db) empId = t.execute() e = db.get_employee(empId) assert e.name, "Jim" assert e.address, "Work" pc = e.classification assert isinstance(pc, SalariedClassification) assert pc.salary == 2000.0 ps = e.schedule assert isinstance(ps, MonthlySchedule) pm = e.method assert isinstance(pm, HoldMethod)
def test_adding_an_employee_paid_by_commission(): db = MemoryDB() t = AddCommissionedEmployee("Sorro", "Firm", 5.0, 1000, db) empId = t.execute() e = db.get_employee(empId) assert e.name == "Sorro" assert e.address == "Firm" pc = e.classification assert isinstance(pc, CommissionedClassification) assert pc.commission == 5.0 assert pc.baseRate == 1000 ps = e.schedule assert isinstance(ps, BiWeeklySchedule) pm = e.method assert isinstance(pm, HoldMethod)
def main(): esplison = 0.6 memorydb_instance = MemoryDB('localhost', 'mario-ai', 'replay-memory') agent_instance = Agent((50, 75, 4), False) i = 1 while True: print(f"experiences size: {memorydb_instance.get_experiences_size()}") sampled_experiences, b_idx, b_ISWeights = memorydb_instance.sample( sample_size) print("sampled_experiences: ", len(sampled_experiences)) errors = train_with_experience(agent_instance, sampled_experiences, b_ISWeights, sample_size, epoch, discount) agent_instance.save_model() memorydb_instance.update_batch(b_idx, errors, sampled_experiences) if i % training_before_update_target == 0: agent_instance.sync_target() i += 1
def test_paying_nothing_to_an_hourly_employee_with_no_timecards(): db = MemoryDB() empId = AddHourlyEmployee("Bob", "Home", 15.25, db).execute() pay_date = datetime.date(2001, 11, 9) pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.pay_date == datetime.date(2001, 11, 9) assert pc.gross_pay == 0 assert pc.deductions == 0 assert pc.net_pay == 0
def test_not_paying_an_hourly_employee_on_wrong_date(): db = MemoryDB() empId = AddHourlyEmployee("Bob", "Home", 15.25, db).execute() pay_date = datetime.date(2001, 11, 29) pt = Payday(pay_date, db) pt.execute() try: pt.get_paycheck(empId) raise "pay on wrong date" except KeyError: pass
def test_not_paying_a_commissioned_employee_on_wrong_date(): db = MemoryDB() empId = AddCommissionedEmployee("Bob", "Home", 10, 1000, db).execute() pay_date = datetime.date(2001, 11, 23) pt = Payday(pay_date, db) pt.execute() try: pt.get_paycheck(empId) raise "pay on wrong date" except KeyError: pass
def test_paying_a_commissioned_employee_with_no_sales(): db = MemoryDB() empId = AddCommissionedEmployee("Bob", "Home", 10, 1000, db).execute() pay_date = datetime.date(2001, 11, 16) pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.pay_date == datetime.date(2001, 11, 16) assert pc.gross_pay == 1000 assert pc.deductions == 0 assert pc.net_pay == 1000
def test_paying_an_hourly_employee_only_in_one_period(): db = MemoryDB() empId = AddHourlyEmployee("Bob", "Home", 15.25, db).execute() pay_date = datetime.date(2001, 11, 9) AddTimeCard(empId, pay_date, 2.0, db).execute() AddTimeCard(empId, datetime.date(2001, 11, 1), 2.0, db).execute() pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.pay_date == datetime.date(2001, 11, 9) assert pc.gross_pay == 30.5 assert pc.deductions == 0 assert pc.net_pay == 30.5
def test_not_paying_a_salaried_employee_on_wrong_date(): db = MemoryDB() t = AddSalariedEmployee("Bill", "Home", 1000.0, db) empId = t.execute() pay_date = datetime.date(2001, 11, 29) pt = Payday(pay_date, db) pt.execute() try: pt.get_paycheck(empId) raise "paycheck found on wrong date" except KeyError: pass
def test_paying_an_hourly_employee_for_over_time(): db = MemoryDB() empId = AddHourlyEmployee("Bob", "Home", 10.0, db).execute() pay_date = datetime.date(2001, 11, 9) AddTimeCard(empId, pay_date, 10.0, db).execute() pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.pay_date == datetime.date(2001, 11, 9) print pc.gross_pay assert pc.gross_pay == 110 assert pc.deductions == 0 assert pc.net_pay == 110
def test_paying_a_single_salaried_employee(): db = MemoryDB() t = AddSalariedEmployee("Bill", "Home", 1000.0, db) empId = t.execute() pay_date = datetime.date(2001, 11, 30) pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.pay_date == datetime.date(2001, 11, 30) assert pc.gross_pay == 1000.0 assert pc.deductions == 0 assert pc.net_pay == 1000.0 assert pc.disposition == "Hold"
def test_paying_a_commissioned_employee_only_for_one_period(): db = MemoryDB() empId = AddCommissionedEmployee("Bob", "Home", 10, 1000, db).execute() pay_date = datetime.date(2001, 11, 16) AddSalesReceipt(empId, pay_date, 500, db).execute() AddSalesReceipt(empId, datetime.date(2001, 11, 16 - 14), 500, db).execute() AddSalesReceipt(empId, datetime.date(2001, 11, 16 + 1), 500, db).execute() pt = Payday(pay_date, db) pt.execute() pc = pt.get_paycheck(empId) assert pc.pay_date == datetime.date(2001, 11, 16) print pc.gross_pay assert pc.gross_pay == 1050 assert pc.deductions == 0 assert pc.net_pay == 1050
def test_deleting_an_employee(): db = MemoryDB() empId = AddHourlyEmployee("Jim", "Home", 32.25, db).execute() db.get_employee(empId) dt = DeleteEmployee(empId, db) dt.execute() try: db.get_employee(empId) raise "employee not deleted" except KeyError: pass
import numpy as np import pylab from dnq_agent import DQNAgent from memory_db import MemoryDB from state_generator import StateGenerator from training_parameters import esplison, esplison_decay, gamma, input_size, frame_size, stack_size, max_steps, render, max_episodes, sample_size, epoch, esplison, esplison_decay, experiences_before_training, training_before_update_target, e, a, beta, beta_increment_per_sampling, capacity, max_priority if __name__ == "__main__": env = gym_super_mario_bros.make('SuperMarioBros-v0') env = BinarySpaceToDiscreteSpaceEnv(env, SIMPLE_MOVEMENT) print(SIMPLE_MOVEMENT, env.action_space.n) # get size of state and action from environment state_size = env.observation_space.shape action_size = env.action_space.n memorydb_instance = MemoryDB(e, a, beta, beta_increment_per_sampling, capacity, max_priority) agent_instance = DQNAgent(input_size, action_size, esplison, esplison_decay, True) state_generator_instance = StateGenerator(frame_size, stack_size) scores, episodes = [], [] for e in range(max_episodes): done = False score = 0 raw_state = env.reset() state = state_generator_instance.get_stacked_frames(raw_state, True) steps = 0 # up to 500 while not done and steps < max_steps: if render: # if True
def main(): esplison = 0.6 io_instance = IO("FCEUX 2.2.3: mario") memorydb_instance = MemoryDB('localhost', 'mario-ai', 'replay-memory') agent_instance = Agent((50, 75, 4), False) i = 1 while True: print(f"experiences size: {memorydb_instance.get_experiences_size()}") experience_bacth = [] io_instance.focus_window() io_instance.reset() is_termnial = False previous_screenshot = io_instance.get_screenshot() previous_device_state = io_instance.get_device_state() previous_image_state = io_instance.get_stacked_frames( previous_screenshot, True) while is_termnial != True: experience = {} experience["terminal"] = False experience["screenshot"] = previous_screenshot experience["image_state"] = previous_image_state experience["device_state"] = previous_device_state dice = random.uniform(0.0, 1.0) action_index = 0 if dice >= esplison: #if True: reward = agent_instance.model_predict([ experience["image_state"].reshape(1, 50, 75, 4), experience["device_state"].reshape(1, 4) ]) # reward = output[0] # print("###value: ", output[2]) # print("###advantage: ", output[1]) print("###reward: ", reward) action_index = np.argmax(reward).item() print("Model selected action and rewards:", action_index, io_instance.action_mapping_name[action_index]) else: action_index = random.randint(0, 3) print("Random selected action:", action_index, io_instance.action_mapping_name[action_index]) io_instance.action(action_index) experience["action_index"] = action_index experience[ "next_screenshot"] = previous_screenshot = io_instance.get_screenshot( ) experience[ "next_image_state"] = previous_image_state = io_instance.get_stacked_frames( previous_screenshot, False) experience[ "next_device_state"] = previous_device_state = io_instance.get_device_state( ) experience_bacth.append(experience) if io_instance.is_termnial(experience["next_screenshot"]): experience["terminal"] = True is_termnial = True calculate_rewards(experience_bacth) memorydb_instance.add_batch(experience_bacth) print("Experiences updated") sampled_experiences, b_idx, b_ISWeights = memorydb_instance.sample( sample_size) print("sampled_experiences: ", len(sampled_experiences)) errors = train_with_experience(agent_instance, sampled_experiences, b_ISWeights, sample_size, epoch, discount) agent_instance.save_model() memorydb_instance.update_batch(b_idx, errors, sampled_experiences) esplison -= esplison_decay if i % training_before_update_target == 0: agent_instance.sync_target() i += 1