HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Using the CMA-ES algorithm to optimize the position of wind turbines

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theturbinespositioncmaalgorithmusingoptimizewind

Problem

I have an implementation of an optimization algorithm called the Covariance Matrix Adaptation Evolutionary Strategy (CMA-ES). I am using this algorithm to optimize the position of wind turbines inside a wind farm, but if I place more than a certain number of turbines (equivalent to a high value of N at the beginning of the code) after around 50 iterations of the while loop, my RAM memory is filled. I have tried using deletion of certain variables after they are being used and using slots in my class declaration but to no noticeable effect.

Could you take a look at the code and tell me if there is something very memory consuming that I'm not seeing?

This is a stand-alone, coupled with the Rosenbrock function to optimize.

```
def frosenbrock(x):
x1=np.delete(x,len(x)-1);
x2=np.delete(x,0);
f = 100*sum(((x12) - (x2))2) + sum((x1-1)**2);
return f;

def optimise_CMA(self,initial_guess):
#--------------INITIALIZATION----------------------
N=20 #Number of variables, problem dimension
#Container for coordinates called xmean
xmean=[] #Objective variables (coordinates)initial point
for i in range(int(N)):
xmean.append([random.uniform(0,1)]);
xmean=np.asarray(xmean,dtype=np.float_)
sigma=0.5 #Coordinate wise standard deviation
stopfitness = 1e-10 #stop if fitness lambd/(c1+cmu)/N/10:
eigeneval=counteval #to achieve O(N^2)
C=np.triu(C)+np.triu(C,1).transpose() #enforce symmetry
B=np.linalg.eig(C)[1] #eigen decomposition, B==normalized eigenvectors
diag_D=np.linalg.eig(C)[0]
diag_D=diag_D.reshape([N,1])
D=diag_D**0.5 #D is a vector of standard deviations now
D_inv=D**(-1)
D_inv=np.diag(D_inv[:,0])
invsqrtC=np.dot(B,np.dot(D_inv,B.transpose()))
print orde

Solution

-
First off, style. Your code is... not very well styled. You have many PEP8 violations. Here's a list of those violations.

  • You have no space between operators/variable declarations. You should have one space between operators, like this: a + b, a == b, or var = 10.



  • Secondly, your variable names need to be more descriptive. Names like cont, arx, or l are not good. Names should be not too short, not too long, and as descriptive as possible.



  • You need two blank lines between functions.



-
I'm not quite sure on this, but I think you issue with memory stems from the fact that you're doing many intensive calculations. Python is not Mathematica or R, which means that it isn't built specifically to do calculations like this. You could use a mathematical library, like numpy, as well. See if you can shorten or simplify some of your expressions.

  • You're also doing a ton of looping, which can hang the program, especially with all those calculations. See where loops can be shortened.



  • Finally, it seems like you're allocating quite a bit of items to lists. If you can remove lists after they're needed, this may speed up your execution time.

Context

StackExchange Code Review Q#49759, answer score: 8

Revisions (0)

No revisions yet.