I'm trying to do optimization using random numbers (with specified range of values). My code is below:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
True_Y = df_3 ['Flows (m3/s)']
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
for i in range(1,len(df_3)):
if 1 < NSE:
print (NSE)
if NSE > 0.5:
print (NSE)
if 1 < IA:
print (IA)
if IA > 0.5:
print (IA)
if 1 < LMI:
print (LMI)
if LMI > 0.5:
print (LMI)
else:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
observed_flow = rainfall
True_Y = rainfall
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
else:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
observed_flow = rainfall
True_Y = rainfall
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
else:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
observed_flow = rainfall
True_Y = rainfall
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
else:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
observed_flow = rainfall
True_Y = rainfall
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
else:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
observed_flow = rainfall
True_Y = rainfall
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
else:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
observed_flow = rainfall
True_Y = rainfall
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
What i'm trying to do here is, NSE, LMI and IA have to be from 0.5 to 1...if not, another random values of x_1, x_2, x_3 and x_4 should be generated and NSE, LMI and IA should be recomputed until it reaches 0.5 to 1 (for all NSE, LMI and IA).
I noticed that x_1, x_2, x_3 and x_4 or NSE, LMI and IA do not change (or probably not doing the loops). Anyone who would like to help me revise this code?
Apologies for using a long code, I'm still not used to functions or classes.
CodePudding user response:
Just changing your loop structure, I think this does what you are asking for:
while True:
x_1 = np.random.uniform(100,1200)
x_2 = np.random.uniform(-5,3)
x_3 = np.random.uniform(20,300)
x_4 = np.random.uniform(1.1,2.9)
params = { 'X1': x_1, 'X2': x_2, 'X3': x_3 , 'X4': x_4 }
states = { 'production_store': 0.60 * params['X1'], 'routing_store': 0.70 * params['X3'] } # Production store = S; Routing Store = R
rainfall = dataframe['RAINFALL']
potential_evap = dataframe['PE or ETP or Evapotrans (mm)']
simulated_flow = gr4j(rainfall, potential_evap, params, states)
True_Y = df_3 ['Flows (m3/s)']
Simulated_Y = simulated_flow
NSE = 1 - np.sum((Simulated_Y-True_Y)**2)/np.sum((True_Y-np.mean(True_Y))**2)
IA = 1 -(np.sum((True_Y-Simulated_Y)**2))/(np.sum((np.abs(Simulated_Y-np.mean(True_Y)) np.abs(True_Y-np.mean(True_Y)))**2))
LMI = 1-(np.sum(np.abs(Simulated_Y-True_Y))/(np.sum(np.abs(True_Y--np.mean(True_Y)))))
if 0.5 < NSE < 1 and 0.5 < LMI < 1 and 0.5 < AI < 1:
# Conditions are met
print(NSE, LMI, AI)
break