def test_if_then_else_basic(self): from pysd.py_backend.functions import if_then_else self.assertEqual(if_then_else(True, lambda: 1, lambda: 0), 1) self.assertEqual(if_then_else(False, lambda: 1, lambda: 0), 0) # Ensure lazzy evaluation self.assertEqual(if_then_else(True, lambda: 1, lambda: 1 / 0), 1) self.assertEqual(if_then_else(False, lambda: 1 / 0, lambda: 0), 0) with self.assertRaises(ZeroDivisionError): if_then_else(True, lambda: 1 / 0, lambda: 0) with self.assertRaises(ZeroDivisionError): if_then_else(False, lambda: 1, lambda: 1 / 0)
def still_employed(): """ Real Name: Still Employed Original Eqn: IF THEN ELSE(Motivation>Motivation Threshold, 1 , 0 ) Units: Dmnl Limits: (None, None) Type: component """ return functions.if_then_else(motivation() > motivation_threshold(), 1, 0)
def gpv_65(): """ Real Name: b'"GPV 65+"' Original Eqn: b'IF THEN ELSE(Extra Demand Flag=1,10+STEP(2,2020),10)' Units: b'1/Year' Limits: (None, None) Type: component b'' """ return functions.if_then_else(extra_demand_flag() == 1, 10 + functions.step(2, 2020), 10)
def still_employed(): """ Still Employed Dmnl component """ return functions.if_then_else(motivation() > motivation_threshold(), 1, 0)
def income(): """ Income Months/Month component The total income from commissions on sales to all tiers. """ return tier_1_income() + tier_2_income() + functions.if_then_else( time() < startup_subsidy_length(), startup_subsidy(), 0)
def still_employed(): """ Real Name: Still Employed Original Eqn: IF THEN ELSE(Months of Buffer < 0 , 0 , 1 ) Units: Dmnl Limits: (None, None) Type: component Flag for whether the agent is still with the firm. Goes to zero when the buffer becomes negative. """ return functions.if_then_else(months_of_buffer() < 0, 0, 1)
def income(): """ Real Name: Income Original Eqn: Tier 1 Income + Tier 2 Income + IF THEN ELSE(Time < Startup Subsidy Length, Startup Subsidy\ , 0 ) Units: Months/Month Limits: (None, None) Type: component The total income from commissions on sales to all tiers. """ return tier_1_income() + tier_2_income() + functions.if_then_else( time() < startup_subsidy_length(), startup_subsidy(), 0)
def still_employed(): """ Still Employed Dmnl component Flag for whether the agent is still with the firm. Goes to zero when the buffer becomes negative. """ return functions.if_then_else(months_of_buffer() < 0, 0, 1)
def sales_effort_available(): """ Sales Effort Available Hours/Month component """ return functions.if_then_else(still_employed() > 0, total_effort_available() * fraction_of_effort_for_sales(), 0)
def plant_death_rate(): """ Real Name: b'Plant Death Rate' Original Eqn: b'IF THEN ELSE(Marsh Elevation < 0.5 :OR: Marsh Elevation > 1.5, Base Growth Rate , 0 \\\\ )' Units: b'1/Year' Limits: (None, None) Type: component b'Imagine the plants live for two years and then die.\\t\\t\\t\\t(I am trying to mimic the rabbit population model in the tutorial where \\n \\t\\tthey call death rate the Avreage lifespan of rabbit. They said they live \\n \\t\\tfor 8 years and then die.' """ return functions.if_then_else(marsh_elevation() < 0.5 or marsh_elevation() > 1.5, base_growth_rate(), 0)
def plant_growth_rate(): """ Real Name: b'Plant Growth Rate' Original Eqn: b'IF THEN ELSE(Marsh Elevation > 0.5 :AND: Marsh Elevation < 1.5 , Base Growth Rate , \\\\ 0 )' Units: b'1/Year' Limits: (None, None) Type: component b'' """ return functions.if_then_else(marsh_elevation() > 0.5 and marsh_elevation() < 1.5, base_growth_rate(), 0)
def sales_effort_available(): """ Real Name: Sales Effort Available Original Eqn: IF THEN ELSE(Still Employed > 0, Total Effort Available * Fraction of Effort for Sales\ , 0 ) Units: Hours/Month Limits: (None, None) Type: component """ return functions.if_then_else(still_employed() > 0, total_effort_available() * fraction_of_effort_for_sales(), 0)
def income(): """ Income Dmnl component Technically in units of months of expenses earned per month """ return months_of_expenses_per_sale() * sales() + functions.if_then_else( time() < startup_subsidy_length(), startup_subsidy(), 0)
def income(): """ Real Name: Income Original Eqn: Months of Expenses per Sale * Sales + IF THEN ELSE(Time < Startup Subsidy Length, Startup Subsidy\ , 0 ) Units: Dmnl Limits: (None, None) Type: component Technically in units of months of expenses earned per month """ return months_of_expenses_per_sale() * sales() + functions.if_then_else( time() < startup_subsidy_length(), startup_subsidy(), 0)
def sales_effort_available(): """ Sales Effort Available Hours/Month component """ return functions.if_then_else( still_employed() > 0, total_effort_available() * fraction_of_effort_for_sales(), 0)
def flooding(): """ flooding 1 component % flooded """ return (np.maximum((1 - length_safety()), (functions.if_then_else( size_of_flood() > average_safety_of_old_levees(), (1 - official_current_safety()), 0)))) * pulse_if_flood() * 100
def workyear(): """ Real Name: b'Workyear' Original Eqn: b'IF THEN ELSE(Workyear Flag=1,Effect of System Pressure on Work Year*Standard Work Year\\\\ ,Standard Work Year)' Units: b'Day/Year' Limits: (None, None) Type: component b'' """ return functions.if_then_else(workyear_flag() == 1, effect_of_system_pressure_on_work_year() * standard_work_year(), standard_work_year())
def productivity(): """ Real Name: b'Productivity' Original Eqn: b'IF THEN ELSE(Productivity Flag=1,Effect of System Pressure on Productivity*Standard GP Productivity\\\\ ,Standard GP Productivity)' Units: b'Person/Person/Day' Limits: (None, None) Type: component b'' """ return functions.if_then_else( productivity_flag() == 1, effect_of_system_pressure_on_productivity() * standard_gp_productivity(), standard_gp_productivity())
def workyear(): """ Real Name: b'Workyear' Original Eqn: b'IF THEN ELSE(Workyear Flag=1,Effect of System Pressure on Work Year*Standard Work Year\\\\ ,Standard Work Year)' Units: b'Day/Year' Limits: (None, None) Type: component b'' """ return functions.if_then_else( workyear_flag() == 1, effect_of_system_pressure_on_work_year() * standard_work_year(), standard_work_year())
def test_if_then_else_with_subscripted(self): # this test only test the lazzy evaluation and basics # subscripted_if_then_else test all the possibilities from pysd.py_backend.functions import if_then_else coords = {'dim1': [0, 1], 'dim2': [0, 1]} dims = list(coords) xr_true = xr.DataArray([[True, True], [True, True]], coords, dims) xr_false = xr.DataArray([[False, False], [False, False]], coords, dims) xr_mixed = xr.DataArray([[True, False], [False, True]], coords, dims) out_mixed = xr.DataArray([[1, 0], [0, 1]], coords, dims) self.assertEqual(if_then_else(xr_true, lambda: 1, lambda: 0), 1) self.assertEqual(if_then_else(xr_false, lambda: 1, lambda: 0), 0) self.assertTrue( if_then_else(xr_mixed, lambda: 1, lambda: 0).equals(out_mixed)) # Ensure lazzy evaluation self.assertEqual(if_then_else(xr_true, lambda: 1, lambda: 1 / 0), 1) self.assertEqual(if_then_else(xr_false, lambda: 1 / 0, lambda: 0), 0) with self.assertRaises(ZeroDivisionError): if_then_else(xr_true, lambda: 1 / 0, lambda: 0) with self.assertRaises(ZeroDivisionError): if_then_else(xr_false, lambda: 1, lambda: 1 / 0) with self.assertRaises(ZeroDivisionError): if_then_else(xr_mixed, lambda: 1 / 0, lambda: 0) with self.assertRaises(ZeroDivisionError): if_then_else(xr_mixed, lambda: 1, lambda: 1 / 0)