def test_optimisation_recovers_optimal_position(self): problem = self.default_problem() farm = problem.parameters.tidal_farm solver = DummySolver(problem) functional = PowerFunctional(problem) control = TurbineFarmControl(farm) rf_params = ReducedFunctionalParameters() rf_params.automatic_scaling = 5. rf = ReducedFunctional(functional, control, solver, rf_params) m0 = farm.control_array p = numpy.random.rand(len(m0)) minconv = helpers.test_gradient_array(rf.__call__, rf.derivative, m0, seed=0.005, perturbation_direction=p) assert minconv > 1.9 bounds = [[Constant(0), Constant(0)], [Constant(3000), Constant(1000)]] maximize(rf, bounds=bounds, method="SLSQP") m = farm._parameters["position"][0] log(INFO, "Solution of the primal variables: m=" + repr(m) + "\n") assert abs(m[0] - 1500) < 40 assert abs(m[1] - 500) < 0.4
def test_optimisation_recovers_optimal_position(self): problem = self.default_problem() farm = problem.parameters.tidal_farm solver = DummySolver(problem) functional = PowerFunctional(problem) control = TurbineFarmControl(farm) rf_params = ReducedFunctionalParameters() rf_params.automatic_scaling = 5. rf = ReducedFunctional(functional, control, solver, rf_params) m0 = farm.control_array p = numpy.random.rand(len(m0)) minconv = helpers.test_gradient_array(rf.__call__, rf.derivative, m0, seed=0.005, perturbation_direction=p) assert minconv > 1.9 bounds = [[Constant(0), Constant(0)], [Constant(3000), Constant(1000)]] maximize(rf, bounds=bounds, method="SLSQP") m = farm._parameters["position"][0] log(INFO, "Solution of the primal variables: m=" + repr(m) + "\n") assert abs(m[0]-1500) < 40 assert abs(m[1]-500) < 0.4
# Compute the derivatives with respect to the turbine position for n in range(len(config.params["turbine_pos"])): for var in ('turbine_pos_x', 'turbine_pos_y'): tfd = turbines(derivative_index_selector=n, derivative_var_selector=var) dj.append(2 * v.inner(tfd.vector())) dj = numpy.array(dj) return j, dj else: return j, None j = lambda m, forward_only=False: j_and_dj(m, forward_only)[0] dj = lambda m, forget: j_and_dj(m, forward_only=False)[1] # run the taylor remainder test config = default_config() m0 = ReducedFunctional(config).initial_control() # We set the perturbation_direction with a constant seed, so that it is consistent in a parallel environment. p = numpy.random.rand(len(m0)) # Run with a functional that does not depend on m directly minconv = test_gradient_array(j, dj, m0, 0.001, perturbation_direction=p) if minconv < 1.99: info_red("The turbine test failed") sys.exit(1) info_green("Test passed")
config.params["turbine_x"] = 800 config.params["turbine_y"] = 800 config.params["controls"] = ['turbine_pos'] config.params["initial_condition"] = BumpInitialCondition(config) config.params["automatic_scaling"] = True return config config = default_config() rf = ReducedFunctional(config, forward_model=mini_model_solve) m0 = rf.initial_control() config.info() p = numpy.random.rand(len(m0)) minconv = test_gradient_array(rf.j, rf.dj, m0, seed=0.005, perturbation_direction=p) if minconv < 1.9: info_red("The gradient taylor remainder test failed.") sys.exit(1) # If this option does not produce any ipopt outputs, delete the ipopt.opt file g = lambda m: [] dg = lambda m: [] bounds = [[Constant(0), Constant(0)], [Constant(3000), Constant(1000)]] m = maximize(rf, bounds = bounds, method = "SLSQP") info("Solution of the primal variables: m=" + repr(m) + "\n") exit_code = 1
# Compute the derivatives with respect to the turbine position for n in range(len(config.params["turbine_pos"])): for var in ('turbine_pos_x', 'turbine_pos_y'): tfd = turbines(derivative_index_selector=n, derivative_var_selector=var) dj.append( 2 * v.inner(tfd.vector()) ) dj = numpy.array(dj) return j, dj else: return j, None j = lambda m, forward_only = False: j_and_dj(m, forward_only)[0] dj = lambda m, forget: j_and_dj(m, forward_only = False)[1] # run the taylor remainder test config = default_config() m0 = ReducedFunctional(config).initial_control() # We set the perturbation_direction with a constant seed, so that it is consistent in a parallel environment. p = numpy.random.rand(len(m0)) # Run with a functional that does not depend on m directly minconv = test_gradient_array(j, dj, m0, 0.001, perturbation_direction=p) if minconv < 1.99: info_red("The turbine test failed") sys.exit(1) info_green("Test passed")