def test_design_var_list(turbineTypeOptimizationProblem, design_vars): tf = TopFarmProblem(design_vars=design_vars, cost_comp=DummyCost(np.array([[2, 0, 1]]).T, ['type']), driver=DOEDriver(FullFactorialGenerator(3))) cost, _, = tf.evaluate() npt.assert_equal(tf.cost, cost) assert tf.cost == 5
def test_state_array(turbineTypeOptimizationProblem): tf = turbineTypeOptimizationProblem arr = tf.state_array(['type', 'type']) npt.assert_equal(arr.shape, [3, 2]) npt.assert_array_equal(arr, [[0, 0], [0, 0], [0, 0]])
def test_parallel_run(lst, parallelRunner): # run sequential s_cost, s_state, s_recorder = seq_runner_example(lst)[0] # run in parallel (p_cost, p_state, p_recorder), results = parallelRunner(lst, seq_runner_example) npt.assert_equal(s_cost, p_cost)
def test_TopFarmProblemXYBoundaryConstraintPolygon(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, constraints=[XYBoundaryConstraint(xy3tb.boundary, 'polygon')]) # constraint violated tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1], -1) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [3, 6, 4]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1])
def test_update_state(turbineTypeOptimizationProblem, types, cost): tf = turbineTypeOptimizationProblem c, state = tf.evaluate({'type': types}) npt.assert_equal(c, cost) npt.assert_array_equal(state['type'], types) # wrong shape c, state = tf.evaluate({'type': [types]}) npt.assert_equal(c, cost) npt.assert_array_equal(state['type'], types) # missing key c, state = tf.evaluate({'missing': types})
def test_with_uniform_generator(get_tf): tf = get_tf(driver=DOEDriver(UniformGenerator(10))) arr = tf.get_DOE_array() uta.assertGreaterEqual(arr[:, 0].min(), 10) # x uta.assertLessEqual(arr[:, 0].max(), 11) # x uta.assertGreaterEqual(arr[:, 1].min(), 6) # y uta.assertLessEqual(arr[:, 1].max(), 8) # y uta.assertGreaterEqual(arr[:, 2].min(), 3) # z uta.assertLessEqual(arr[:, 2].max(), 4) # z cost, _, recorder = tf.optimize() npt.assert_equal(cost, np.min(recorder.get('cost')))
def test_TopFarmProblemXYBoundaryPenalty(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, driver=EasyRandomSearchDriver(RandomizeTurbinePosition(1), 10), constraints=[XYBoundaryConstraint(xy3tb.boundary)]) # spacing violated cost, _ = tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_array_less(1e10, cost) # spacing satisfied cost, _ = tf.evaluate({'x': xy3tb.optimal[:, 0], 'y': xy3tb.optimal[:, 1]}) npt.assert_equal(1.5, cost)
def test_TopFarmProblemLimits(): tf = xy3tb.get_tf(design_vars={'x': (xy3tb.initial[:, 0], -3, 3), 'y': (xy3tb.initial[:, 1], [-4, -3, -2], [2, 3, 4])}, driver=EasyRandomSearchDriver(RandomizeTurbinePosition(1), max_iter=100), constraints=[]) tf.evaluate() desvars = tf.driver._designvars npt.assert_equal(desvars['indeps.x']['lower'], -3) npt.assert_equal(desvars['indeps.x']['upper'], 3) npt.assert_array_equal(desvars['indeps.y']['lower'], [-4, -3, -2]) npt.assert_array_equal(desvars['indeps.y']['upper'], [2, 3, 4])
def test_TopFarmProblemXYBoundaryConstraint(): tf = xy3tb.get_tf(design_vars={'x': [3, 7, 4], 'y': [-3, -7, -3]}, constraints=[XYBoundaryConstraint(xy3tb.boundary)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1, 3], -1) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [3, 6, 4]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1]) desvars = tf.driver._designvars for xy in 'xy': for lu in ['lower', 'upper']: npt.assert_equal(desvars['indeps.' + xy][lu], np.nan)
def testCostModelComponentAdditionalOutput(): def aep_cost(x, y): opt_x, opt_y = optimal.T return -np.sum((x - opt_x)**2 + (y - opt_y)**2), {'add_out': sum(x)} tf = get_tf( AEPCostModelComponent(['x', 'y'], 4, aep_cost, aep_gradients, additional_output=[('add_out', 0)])) _, state, _ = tf.optimize() npt.assert_array_almost_equal(tf.turbine_positions[:, :2], optimal_with_constraints, 5) npt.assert_equal(sum(state['x']), state['add_out'])
def test_random_search_driver_randomize_all_uniform(): np.random.seed(1) class Cost(): i = 0 def __call__(self, *args, **kwargs): self.i += 1 return self.i cost_comp = CostModelComponent(input_keys=['x', 'y', 'type'], n_wt=2, cost_function=Cost(), income_model=True) tf = TopFarmProblem( { 'x': ([1, 6], [0, 1], [5, 6]), 'y': ([-1., 0], -6, 0), 'type': ([3, 3], 3, 8) }, cost_comp=cost_comp, constraints=[], driver=EasyRandomSearchDriver(randomize_func=RandomizeAllUniform( ['x', 'type']), max_iter=600, disp=False), ) _, state, recorder = tf.optimize() # check that integer design variables are somewhat evenly distributed x, y, t = recorder['x'], recorder['y'], recorder['type'] for arr, l, u in [(x[:, 0], 0, 5), (x[:, 1], 1, 6), (t[:, 0], 3, 8)]: count = [(arr == i).sum() for i in range(l, u + 1)] npt.assert_equal(601, sum(count)) npt.assert_array_less(600 / len(count) * .70, count) count, _ = np.histogram(y[:, 0], np.arange(-6, 1)) npt.assert_equal(y.shape[0], sum(count)) npt.assert_array_less(600 / len(count) * .70, count)
def test_TopFarmProblemXYBoundaryPenaltyAndLimits(): tf = xy3tb.get_tf(design_vars={'x': ([3, 7, 4], -1, 5), 'y': ([-3, -7, -3], -9, -1)}, driver=EasyRandomSearchDriver(RandomizeTurbinePosition(1), 10), constraints=[XYBoundaryConstraint(xy3tb.boundary)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1, 3], -1) desvars = tf.driver._designvars npt.assert_equal(desvars['indeps.x']['lower'], 0) npt.assert_equal(desvars['indeps.x']['upper'], 5) npt.assert_array_equal(desvars['indeps.y']['lower'], -9) npt.assert_array_equal(desvars['indeps.y']['upper'], -1)
def test_TopFarmProblemXYBoundaryConstraint(): tf = xy3tb.get_tf(design_vars={ 'x': [3, 7, 4], 'y': [-3, -7, -3] }, constraints=[XYBoundaryConstraint(xy3tb.boundary)]) tf.evaluate({'x': xy3tb.desired[:, 0], 'y': xy3tb.desired[:, 1]}) npt.assert_equal(tf['boundaryDistances'][1, 3], -1) _, state, _ = tf.optimize() npt.assert_array_almost_equal(state['x'], [3, 6, 4]) npt.assert_array_almost_equal(state['y'], xy3tb.optimal[:, 1]) desvars = tf.driver._designvars if tuple(map(int, scipy.__version__.split("."))) < (1, 5, 0): for xy in 'xy': for lu in ['lower', 'upper']: npt.assert_equal(desvars['indeps.' + xy][lu], np.nan) else: for i, xy in enumerate('xy'): for lu, func in zip(['lower', 'upper'], (np.min, np.max)): npt.assert_equal(desvars['indeps.' + xy][lu], func(xy3tb.boundary[:, i]))
def test_state(turbineTypeOptimizationProblem): tf = turbineTypeOptimizationProblem npt.assert_equal(tf.state, {'type': [0, 0, 0]})
def test_cost(turbineTypeOptimizationProblem): tf = turbineTypeOptimizationProblem cost, _, = tf.evaluate() npt.assert_equal(tf.cost, cost) assert tf.cost == 5
def testTopFarmProblem_as_component(turbineTypeOptimizationProblem): tf = turbineTypeOptimizationProblem c = tf.as_component() npt.assert_equal(c.__class__, ProblemComponent) assert c.problem == tf