Skip to content
Snippets Groups Projects
Commit 881ce532 authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

parallel_runner implemented

closes #41
parent 2f9a6f58
No related branches found
No related tags found
1 merge request!94Handle disabled mpi
...@@ -115,7 +115,10 @@ class TopFarmProblem(Problem): ...@@ -115,7 +115,10 @@ class TopFarmProblem(Problem):
def get_DOE_list(self): def get_DOE_list(self):
assert isinstance(self.driver, DOEDriver), 'get_DOE_list only applies to DOEDrivers, and the current driver is: %s' % type(self.driver) assert isinstance(self.driver, DOEDriver), 'get_DOE_list only applies to DOEDrivers, and the current driver is: %s' % type(self.driver)
case_gen = self.driver.options['generator'] case_gen = self.driver.options['generator']
return np.array([[var[1] for var in c] for c in case_gen(self.model.get_design_vars(recurse=True), self.model)]) return [c for c in case_gen(self.model.get_design_vars(recurse=True), self.model)]
def get_DOE_array(self):
return np.array([[v for k,v in c] for c in self.get_DOE_list()])
class TurbineTypeOptimizationProblem(TopFarmProblem): class TurbineTypeOptimizationProblem(TopFarmProblem):
......
# import os
# import numpy as np
# import multiprocessing
#
#
# def run(N, single_starter, processes=None):
# """Run N single starts in parallel
#
# Parameters
# ----------
# N : int
# Number of single starts
# single_starter : function
# function for single start. Interface must be:
# def f(id):
# tf = Topfarm(...)
# return tf.optimize
# processes : int or None, optional
# Number of processes passed to multiprocessing.Pool
#
# Returns
# -------
# best : (cost, turbine_positions)
# best result
# results : [(cost1, turbine_positions1),...]
# all results
# """
# pool = multiprocessing.Pool(processes)
# results = pool.map(single_starter, range(N))
# best = results[np.argmin([r[0] for r in results])]
# return best, results
#
#
# def single_start_example(id, maxiter=5, plot_comp=None):
# from topfarm._topfarm import TopFarm
# from topfarm.cost_models.fused_wake_wrappers import FusedWakeNOJWakeModel
# from topfarm.cost_models.utils.aep_calculator import AEPCalculator
# from topfarm.cost_models.utils.wind_resource import WindResource
# from topfarm.easy_drivers import EasyScipyOptimizeDriver
# from topfarm.tests.test_files import tfp
#
# D = 80.0
# D2 = 2 * D
# init_pos = np.array([(0, D2), (0, 0), (0, -D2)])
# boundary = [(-D2, D2), (D2, D2), (D2, -D2), (-D2, -D2)]
# minSpacing = 2.0
# f, A, k = [1, 0, 0, 0], [10, 10, 10, 10], [2, 2, 2, 2]
# wr = WindResource(f, A, k, ti=np.zeros_like(f) + .1)
# wm = FusedWakeNOJWakeModel(tfp + "wind_farms/3tb.yml")
# aep_calc = AEPCalculator(wr, wm)
# driver = EasyScipyOptimizeDriver(maxiter=maxiter, disp=False)
# tf = TopFarm(init_pos, aep_calc.get_TopFarm_cost_component(), minSpacing * D,
# boundary=boundary, plot_comp=plot_comp, driver=driver, record_id=None)
# tf.shuffle_positions('abs')
# return tf.optimize()
#
#
# def try_me():
# if __name__ == '__main__':
# print(run(4, single_start_example)[0], 2)
# print(single_start_example(0, 10))
#
#
# try_me()
import os
import numpy as np
import multiprocessing
from topfarm._topfarm import InitialXYZOptimizationProblem
from topfarm.constraint_components.boundary_component import BoundaryComp
from topfarm.cost_models.dummy import DummyCost
from openmdao.drivers.doe_generators import UniformGenerator
class ParallelRunner():
def __init__(self, processes=None):
"""
Parameters
----------
processes : int or None, optional
Number of processes passed to multiprocessing.Pool
"""
self.pool = multiprocessing.Pool(processes)
def __call__(self, state_lst, seq_runner):
"""Run in parallel
Parameters
----------
state_lst : list
List of states (as returned by TopFarmProblem.get_DOE_list()
seq_runner : function
function for sequential run. Interface must be:
def f(lst):
tf = TopfarmProblem(...)
return tf.optimize()
Returns
-------
best : (cost, state, recorder)
best result
results : [(cost1, state1, recorder1),...]
all results
"""
indexes = np.round(np.linspace(0, len(state_lst), self.pool._processes)).astype(np.int)
seq_lst = [state_lst[i1:i2] for i1, i2 in zip(indexes[:-1], indexes[1:])]
results = self.pool.map(seq_runner, seq_lst)
best = results[np.argmin([r[0] for r in results])]
return best, results
def get_InitialXYZOptimizationProblem(driver):
return InitialXYZOptimizationProblem(
cost_comp=DummyCost([(1, 0, 4),
(0, 1, 3)]),
min_spacing=None,
turbineXYZ=[[0, 0, 0],
[2, 2, 2]],
boundary_comp=BoundaryComp(n_wt=2,
xy_boundary=[(10, 6), (11, 8)],
xy_boundary_type='rectangle',
z_boundary=[3, 4]),
driver=driver)
def seq_runner_example(lst):
return get_InitialXYZOptimizationProblem(lst).optimize()
def try_me():
if __name__ == '__main__':
lst = get_InitialXYZOptimizationProblem(driver=UniformGenerator(200)).get_DOE_list()
# run in parallel
par_runner = ParallelRunner()
(cost, state, recorder), results = par_runner(lst, seq_runner_example)
print(cost)
# run sequential
cost, state, recorder = seq_runner_example(lst)
print(cost)
try_me()
from openmdao.drivers.doe_driver import DOEDriver
from openmdao.drivers.doe_generators import FullFactorialGenerator,\
ListGenerator, UniformGenerator
import pytest
from topfarm._topfarm import InitialXYZOptimizationProblem
import numpy as np
from topfarm.cost_models.dummy import DummyCost
from topfarm.tests import npt, uta
from topfarm.constraint_components.constrained_generator import ConstrainedXYZGenerator
from topfarm.constraint_components.boundary_component import BoundaryComp
from topfarm.parallel_runner import ParallelRunner
@pytest.fixture("module")
def parallelRunner():
return ParallelRunner()
def get_InitialXYZOptimizationProblem(driver):
return InitialXYZOptimizationProblem(
cost_comp=DummyCost([(1, 0, 4),
(0, 1, 3)]),
min_spacing=None,
turbineXYZ=[[0, 0, 0],
[2, 2, 2]],
boundary_comp=BoundaryComp(n_wt=2,
xy_boundary=[(10, 6), (11, 8)],
xy_boundary_type='rectangle',
z_boundary=[3, 4]),
driver=driver)
@pytest.fixture
def lst():
return get_InitialXYZOptimizationProblem(driver=UniformGenerator(200)).get_DOE_list()
def seq_runner_example(lst):
return get_InitialXYZOptimizationProblem(lst).optimize()
def test_parallel_run(lst, parallelRunner):
# run sequential
s_cost, s_state, s_recorder = seq_runner_example(lst)
# run in parallel
(p_cost, p_state, p_recorder), results = parallelRunner(lst, seq_runner_example)
npt.assert_equal(s_cost, p_cost)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment