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

rewrote RandomizeTurbinePosition functions

parent bdaac60f
No related branches found
No related tags found
1 merge request!94Handle disabled mpi
......@@ -131,7 +131,7 @@ class RandomSearchDriver(Driver):
n_iter = 0
desvar_info = [(abs2prom[name], *self._desvar_idx[name], meta['lower'], meta['upper']) for name, meta in iteritems(desvars)]
desvar_dict = {name: (x0[i:j], l, u) for (name, i, j, l, u) in desvar_info}
desvar_dict = {name: (x0[i:j], lower_bound[i:j], upper_bound[i:j]) for (name, i, j, l, u) in desvar_info}
while n_iter < max_iter:
for name, i, j, _, _ in desvar_info:
......@@ -223,29 +223,29 @@ class RandomSearchDriver(Driver):
return obj, success
class RandomizeTurbinePosition_DirStep():
def __init__(self, max_move_step):
self.max_move_step = max_move_step
class RandomizeTurbinePosition():
def __init__(self, max_step):
self.max_step = max_step
def __call__(self, desvar_dict):
i_wt = np.random.randint(len(desvar_dict['turbineX'][0]))
step = np.random.rand() * self.max_move_step
theta = np.random.rand() * np.pi * 2
for (xy, l, u), dxy in [(desvar_dict['turbineX'], step * np.cos(theta)),
(desvar_dict['turbineY'], step * np.sin(theta))]:
xy[i_wt] = np.maximum(np.minimum(xy[i_wt] + dxy, u), l)
max_step_xy = [self.max_step or (desvar_dict['turbine' + xy][2][i_wt] - desvar_dict['turbine' + xy][1][i_wt])
for xy in 'XY']
dxy = self._xy_step(max_step_xy)
for (xy, lbound, ubound), dxy_ in zip([desvar_dict['turbineX'], desvar_dict['turbineY']],
dxy):
xy[i_wt] = np.maximum(np.minimum(xy[i_wt] + dxy_, ubound[i_wt]), lbound[i_wt])
return desvar_dict
class RandomizeTurbinePosition_Uniform():
def __call__(self, desvar_dict):
i_wt = np.random.randint(len(desvar_dict['turbineX'][0]))
for xy, lbound, ubound in [(desvar_dict['turbineX']),
(desvar_dict['turbineY'])]:
if hasattr(lbound, 'len'):
lbound = lbound[i_wt]
if hasattr(ubound, 'len'):
ubound = ubound[i_wt]
v = np.random.rand() * (ubound - lbound) + lbound
xy[i_wt] = np.maximum(np.minimum(v, ubound), lbound)
return desvar_dict
class RandomizeTurbinePosition_Circle(RandomizeTurbinePosition):
def _xy_step(self, max_step_xy):
step = np.random.rand() * max(max_step_xy)
theta = np.random.rand() * np.pi * 2
return step * np.cos(theta), step * np.sin(theta)
class RandomizeTurbinePosition_Square(RandomizeTurbinePosition):
def _xy_step(self, max_step_xy):
return (np.random.rand()*2-1) * max_step_xy[0], (np.random.rand()*2-1) * max_step_xy[1]
......@@ -6,8 +6,8 @@ from topfarm.plotting import NoPlot
from topfarm.easy_drivers import EasyScipyOptimizeDriver, EasyPyOptSparseIPOPT,\
EasySimpleGADriver, EasyRandomSearchDriver
from topfarm.tests import uta
from topfarm.drivers.random_search_driver import RandomizeTurbinePosition_DirStep,\
RandomizeTurbinePosition_Uniform
from topfarm.drivers.random_search_driver import RandomizeTurbinePosition_Square,\
RandomizeTurbinePosition_Circle
initial = np.array([[6, 0], [6, -8], [1, 1]]) # initial turbine layouts
......@@ -22,7 +22,7 @@ def topfarm_generator():
from topfarm.cost_models.dummy import DummyCostPlotComp
plot_comp = DummyCostPlotComp(desired * xy_scale, plot_improvements_only=True)
plot_comp = NoPlot()
#plot_comp = NoPlot()
class DummyCostScaled(DummyCost):
def cost(self, **kwargs):
......@@ -114,11 +114,11 @@ def find_optimal_scaling(topfarm_generator):
plt.show()
@pytest.mark.parametrize('randomize_func', [RandomizeTurbinePosition_DirStep(1),
RandomizeTurbinePosition_Uniform()])
@pytest.mark.parametrize('randomize_func', [RandomizeTurbinePosition_Circle(1),
RandomizeTurbinePosition_Square(1)])
def test_random_search_driver(topfarm_generator, randomize_func):
driver = EasyRandomSearchDriver(randomize_func, max_iter=2000)
np.random.seed(1)
driver = EasyRandomSearchDriver(randomize_func, max_iter=1000)
tf = topfarm_generator(driver, spacing=1)
cost, _, recorder = tf.optimize()
tb_pos = tf.turbine_positions[:, :2]
......
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