def test_to_robust_problem(self, mocked_platypus): mocked_model = Model('test', function=mock.Mock()) mocked_model.levers = [ RealParameter('a', 0, 1), RealParameter('b', 0, 1) ] mocked_model.uncertainties = [ RealParameter('c', 0, 1), RealParameter('d', 0, 1) ] mocked_model.outcomes = [ScalarOutcome('x'), ScalarOutcome('y')] scenarios = 5 robustness_functions = [ ScalarOutcome('mean x', variable_name='x', function=mock.Mock()), ScalarOutcome('mean y', variable_name='y', function=mock.Mock()) ] problem = to_robust_problem(mocked_model, scenarios, robustness_functions) self.assertEqual('robust', problem.searchover) for entry in problem.parameters: self.assertIn(entry.name, mocked_model.levers.keys()) self.assertEqual(['a', 'b'], problem.parameter_names) self.assertEqual(['mean x', 'mean y'], problem.outcome_names)
def test_to_problem(self, mocked_platypus): mocked_model = Model('test', function=mock.Mock()) mocked_model.levers = [ RealParameter('a', 0, 1), RealParameter('b', 0, 1) ] mocked_model.uncertainties = [ RealParameter('c', 0, 1), RealParameter('d', 0, 1) ] mocked_model.outcomes = [ScalarOutcome('x'), ScalarOutcome('y')] mocked_model.constraints = [] searchover = 'levers' problem = to_problem(mocked_model, searchover) self.assertEqual(searchover, problem.searchover) for entry in problem.parameters: self.assertIn(entry.name, mocked_model.levers.keys()) self.assertIn(entry, list(mocked_model.levers)) for entry in problem.outcome_names: self.assertIn(entry.name, mocked_model.outcomes.keys()) searchover = 'uncertainties' problem = to_problem(mocked_model, searchover) self.assertEqual(searchover, problem.searchover) for entry in problem.parameters: self.assertIn(entry.name, mocked_model.uncertainties.keys()) self.assertIn(entry, list(mocked_model.uncertainties)) for entry in problem.outcome_names: self.assertIn(entry.name, mocked_model.outcomes.keys())
def test_init(self): # let's add some uncertainties to this uncs = [RealParameter("a", 0, 1), RealParameter("b", 0, 1)] outcomes = [ ScalarOutcome("scalar"), ArrayOutcome("array", shape=(10, )), TimeSeriesOutcome("timeseries") ] callback = DefaultCallback(uncs, [], outcomes, nr_experiments=100) self.assertEqual(callback.i, 0) self.assertEqual(callback.nr_experiments, 100) self.assertEqual(callback.cases.shape[0], 100) self.assertEqual(callback.outcomes, [o.name for o in outcomes]) names = callback.cases.columns.values.tolist() names = set(names) self.assertEqual(names, {'a', 'b', 'policy', 'model', 'scenario'}) self.assertNotIn('scalar', callback.results) self.assertNotIn('timeseries', callback.results) self.assertIn('array', callback.results) a = np.all(np.isnan(callback.results['array'])) self.assertTrue(a) # with levers levers = [RealParameter('c', 0, 10)] callback = DefaultCallback(uncs, levers, outcomes, nr_experiments=100) self.assertEqual(callback.i, 0) self.assertEqual(callback.nr_experiments, 100) self.assertEqual(callback.cases.shape[0], 100) self.assertEqual(callback.outcomes, [o.name for o in outcomes]) names = callback.cases.columns.values.tolist() names = set(names) self.assertEqual(names, {'a', 'b', 'c', 'policy', 'model', 'scenario'}) self.assertNotIn('scalar', callback.results) self.assertNotIn('timeseries', callback.results) self.assertIn('array', callback.results) a = np.all(np.isnan(callback.results['array'])) self.assertTrue(a)
RealParameter('delta', 0.93, 0.99) ] # lake_model.levers = [RealParameter("c1", -2, 2), # RealParameter("c2", -2, 2), # RealParameter("r1", 0, 2), #[0,2] # RealParameter("r2", 0, 2), # # RealParameter("w1", 0, 1) # ] lake_model.levers = [ RealParameter(str(i), 0, 0.1) for i in range(lake_model.time_horizon) ] lake_model.outcomes = [ ScalarOutcome('max_P', kind=ScalarOutcome.MINIMIZE), ScalarOutcome('utility', kind=ScalarOutcome.MAXIMIZE), ScalarOutcome('inertia', kind=ScalarOutcome.MINIMIZE), ScalarOutcome('reliability', kind=ScalarOutcome.MAXIMIZE) ] lake_model.constants = [ Constant('alpha', 0.41), Constant('nsamples', 100), Constant('timehorizon', lake_model.time_horizon) ] scenarios = ['Ref', 77, 96, 130, 181] random_scenarios = [81, 289, 391, 257] policies = []