<jupyter_start><jupyter_text>Check objective function: supply-demand ratio<jupyter_code>net = network.WaterDistributionNetwork('ctown_pd.inp')
net.set_time_params(duration=3600*24, hydraulic_step=300)
net.run()
df_links = net.df_links_report
df_nodes = net.df_nodes_report
df_links
objFunction.supply_demand_ratio(net)
objFunction.step_supply_demand_ratio(net, ['PU1','PU2'])
total_demand = 0
for junc_id in net.junctions.uid:
    total_demand += df_nodes['junctions', junc_id, 'demand'].sum()
total_demand
net2 = network.WaterDistributionNetwork('ky3_pd.inp')
net2.set_time_params(duration=3600*24, hydraulic_step=300)
net2.run()
objFunction.supply_demand_ratio(net2, ['X_Pump_1', 'X_Pump_2', 'X_Pump_3', 'X_Pump_4', 'X_Pump_5'])
objFunction.step_supply_demand_ratio(net2, ['X_Pump_1', 'X_Pump_2', 'X_Pump_3', 'X_Pump_4', 'X_Pump_5'])<jupyter_output>flows    : [4558.258935054655, 4558.14922816539, 4558.039119296168, 4557.928561067098, 4557.817495232736, 4557.705853762235, 4557.593559031735, 4557.480521806461, 4557.366639665892, 4557.251794213514, 4557.135848724178, 4557.018639058392, 4556.899962587757, 4556.779554918544, 4556.657023463388, 4556.531885031193, 4556.405156133135, 4556.276270345428, 4556.144776253951, 4556.0098661977145, 4555.870646182326, 4555.725029497335, 4555.5715200894865, 4555.4180215517645, 4547.43932054596, 4547.168062848779, 4546.889636226453, 4546.609993608323, 4546.329202422242, 4546.074257982989, 4545.840664230475, 4545.622262366722, 4545.41554644532, 4545.218202357434, 4545.02861556228, 4544.845583246262, 4544.668156633587, 4544.4955690388415, 4544.32715242831, 4544.163112698954, 4544.00323553378, 4543.8469559892565, 4543.693852308777, 4543.54358763287, 4543.3958841274925, 4543.250508149339, 4543.107260275045, 4542.965968282893, 4542.826481845567, 4542.688668832477, 4542.552412169654, 4542.417607514767, 4[...]<jupyter_text>New method: sum of the ratios at each timestamp<jupyter_code>tot_r = 0
tot_d = 0 
tot_ratio = 0

for time in df_links.index:
    demand = sum([df_nodes.loc[time]['junctions', junc_id, 'demand'] for junc_id in net.junctions.uid])
    flow = sum([df_links.loc[time]['pumps', pump_id, 'flow'] for pump_id in net.pumps.uid])
    ratio = flow/demand
    print(ratio)
    tot_ratio += ratio
tot_ratio<jupyter_output>0.47321270587247943
0.4732270868317517
0.9999996777331489
0.9999996782999285
0.9999996770153096
0.9999996778224257
0.9999996777497266
0.999999677803064
0.9999996779088709
0.9999996776148276
0.9999996769747629
0.9999996769589696
0.9999996779063294
0.9999996780084267
0.9999996783057898<jupyter_text>Differential Evolution<jupyter_code>objfunc_params = {
    'target_nodes': ['1', '2', '3'],
    'nodes_band': {'1': [50, 100],
                   '2': [60, 90],
                   '3': [40, 60]
                   }
    }
objfunc_params = None 


DE_params = {
        'vars_list': ['78', '79'],
        #'vars_list': ['PU1', 'PU2', 'PU3', 'PU4', 'PU5', 'PU6', 'PU7', 'PU8', 'PU9', 'PU10', 'PU11'],
        'n_pop': 5,
        'n_generations': 1,
        'CR': 0.7,
        'F': 0.5,
    }
net = network.WaterDistributionNetwork("anytown_pd.inp")
de = differentialEvolution.DifferentialEvolution(net, objFunction.supply_demand_ratio, objfunc_params, **DE_params)
duration = 86400           # 1 day
hyd_step = 600             # 10 min
update_interval = 14400    # 4 hours<jupyter_output><empty_output><jupyter_text>One day simulation<jupyter_code>de.run(duration, hyd_step, update_interval)<jupyter_output>> STARTING GENERATION 1: ...
[0.9616308444402765, 1.8239669409855008e-07, 1.8282544810124467e-07, 1.8260284375009857e-07, 1.8264720523266643e-07, 1.82875090584716e-07, 1.8293097807143943e-07, 1.8301723881547617e-07, 1.8354684713117025e-07, 1.81856810885574e-07, 1.8259399606606427e-07, 1.840828101415248e-07, 1.832596457598583e-07, 1.826376704123528e-07, 1.8371418834239983e-07, 1.8309355476985528e-07, 1.8331333833311638e-07, 1.8314357017504568e-07, 1.8344118997665088e-07, 1.8334727413532328e-07, 1.8312009468200873e-07, 1.8336335135602746e-07, 1.8208450969774887e-07, 1.8307086223639487e-07, 1.8216363804088819e-07, 0.5965816752185327, 0.5965816760527625, 0.5965816768475194, 0.5965816745595063, 0.5965816755223343, 0.5965816761612742, 0.5965816756938946, 0.5965816758938082, 0.5965816756443139, 0.5965816760546719, 0.5965816753280561, 0.5965816760314682, 0.5965816757949914, 0.5965816750393301, 0.5965816755599598, 0.5965816754722986, 0.5965816772810789, 0.5965816769397339, 0.5965816757487248, 0[...]<jupyter_text>One week simulation<jupyter_code>objfunc_params = {
    'target_nodes': ['1', '2', '3'],
    'nodes_band': {'1': [50, 100],
                   '2': [60, 90],
                   '3': [40, 60]
                   }
    }
objfunc_params = None 


DE_params = {
    'vars_list': ['78', '79'],
    #'vars_list': ['PU1', 'PU2', 'PU3', 'PU4', 'PU5', 'PU6', 'PU7', 'PU8', 'PU9', 'PU10', 'PU11'],
    'n_pop': 20,
    'n_generations': 5,
    'CR': 0.7,
    'F': 0.5,
}
net = network.WaterDistributionNetwork("anytown_pd.inp")
de = differentialEvolution.DifferentialEvolution(net, objFunction.supply_demand_ratio, objfunc_params, **DE_params)
pattern_csv = "../demand_patterns/demands_anytown.csv"
junc_demands = pd.read_csv(pattern_csv, names=['junc_demand'])
pattern_dict = {col_name: junc_demands[col_name].values for col_name in junc_demands.columns}
net.set_demand_pattern('junc_demand', junc_demands.values, net.junctions)
days = 7

duration = 24*3600          # 7 days
hyd_step = 600              # 10 min
update_interval = 3600*4    # 4 hours
de.run(duration, hyd_step, update_interval, days, keep_same_pop=False, save_results=True, info='week_new_objfunc')<jupyter_output>DAY 1:
> STARTING GENERATION 1: ...
>>> Parent: candidate = {'78': array([1, 0, 1, 1, 1, 1]), '79': array([1, 1, 1, 1, 0, 0])}, value = 0.8790743677467173
>>> Child: candidate = {'78': array([1, 1, 0, 1, 1, 1]), '79': array([0, 0, 1, 1, 0, 1])}, value = 0.874620312462142
    -> NEW BEST CANDIDATE FOUND!
    --> Substitution: False
 ----- Elapsed time [0:00:24.583015] -----
>>> Parent: candidate = {'78': array([1, 1, 0, 0, 0, 1]), '79': array([1, 1, 0, 1, 1, 1])}, value = 0.7512338609809404
>>> Child: candidate = {'78': array([1, 1, 1, 0, 0, 1]), '79': array([0, 1, 0, 1, 0, 1])}, value = 0.743745698940399
    --> Substitution: False
 ----- Elapsed time [0:00:50.626726] -----
>>> Parent: candidate = {'78': array([0, 0, 0, 0, 0, 1]), '79': array([0, 0, 0, 1, 0, 1])}, value = 0.30771092984035586
>>> Child: candidate = {'78': array([0, 1, 0, 0, 1, 0]), '79': array([0, 1, 0, 1, 0, 1])}, value = 0.5726784840779383
    --> Substitution: True
 ----- Elapsed time [0:01:13.013590] -----
>>> Paren[...]<jupyter_text>Testing with ctown<jupyter_code>objfunc_params = {
    'target_nodes': ['1', '2', '3'],
    'nodes_band': {'1': [50, 100],
                   '2': [60, 90],
                   '3': [40, 60]
                   }
    }
objfunc_params = None 


DE_params = {
    'vars_list': ['PU1', 'PU2'],
    'n_pop': 20,
    'n_generations': 5,
    'CR': 0.7,
    'F': 0.5,
}
net = network.WaterDistributionNetwork("ctown_pd.inp")
de = differentialEvolution.DifferentialEvolution(net, objFunction.supply_demand_ratio, objfunc_params **DE_params)
days = 1

duration = 24*3600          # 7 days
hyd_step = 600              # 10 min
update_interval = 3600*4    # 4 hours
demand_patterns_path = '../demand_patterns/three_year_demands_ctown.csv'
starting_demand_path = '../demand_patterns/starting_demand_points.csv'
week_index = 0

limit = (days * duration) - 1
total_demands = pd.read_csv(demand_patterns_path, index_col=0)
demand_starting_points = pd.read_csv(starting_demand_path, index_col=0)

week_start = demand_starting_points.iloc[week_index][0]
week_demands = total_demands.loc[week_start:week_start + limit, :]

for pattern_id in net.patterns.uid:
    net.set_demand_pattern(pattern_id, week_demands[pattern_id].values.tolist())
de.run(duration, hyd_step, update_interval, days, keep_same_pop=False, save_results=False, info='week_new_objfunc')<jupyter_output><empty_output><jupyter_text>Testing town KY3<jupyter_code>objfunc_params = {
    'target_nodes': ['1', '2', '3'],
    'nodes_band': {'1': [50, 100],
                   '2': [60, 90],
                   '3': [40, 60]
                   }
}
objfunc_params = None 

DE_params = {
        #'vars_list': ['78', '79'],
        #'vars_list': ['PU1', 'PU2', 'PU3', 'PU4', 'PU5', 'PU6', 'PU7', 'PU8', 'PU9', 'PU10', 'PU11'],
        'vars_list': ['X_Pump_1', 'X_Pump_2', 'X_Pump_3', 'X_Pump_4', 'X_Pump_5'],
        'n_pop': 5,
        'n_generations': 1,
        'CR': 0.7,
        'F': 0.5,
    }
net = network.WaterDistributionNetwork("ky3_pd.inp")
de = differentialEvolution.DifferentialEvolution(net, objFunction.supply_demand_ratio, objfunc_params, **DE_params)
duration = 86400           # 1 day
hyd_step = 600             # 10 min
update_interval = 14400    # 4 hours
de.run(duration, hyd_step, update_interval)<jupyter_output>> STARTING GENERATION 1: ...
{'fraction_non_alphanumeric': 0.15719844357976653, 'fraction_numerical': 0.26570316842690384, 'mean_word_length': 4.465370595382746, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '135548', 'license': 'MIT', 'n_tokens_mistral': 5202, 'n_tokens_neox': 3684, 'n_words': 638, 'path': 'notebooks/DE_objfunc.ipynb', 'repo_name': 'Daveonwave/msc_thesis', 'size': '8995'}