-
Notifications
You must be signed in to change notification settings - Fork 0
/
ENMultiObjEvaluator.cpp
1595 lines (1356 loc) · 49.5 KB
/
ENMultiObjEvaluator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by a1091793 on 2/3/17.
//
#include <dlfcn.h>
#include "ENMultiObjEvaluator.h"
#include <boost/lexical_cast.hpp>
#include <iomanip>
int EN_ELEVATION;
int EN_BASEDEMAND;
int EN_PATTERN;
int EN_EMITTER;
int EN_INITQUAL;
int EN_SOURCEQUAL;
int EN_SOURCEPAT;
int EN_SOURCETYPE;
int EN_TANKLEVEL;
int EN_DEMAND;
int EN_HEAD;
int EN_PRESSURE;
int EN_QUALITY;
int EN_SOURCEMASS;
int EN_INITVOLUME;
int EN_MIXMODEL;
int EN_MIXZONEVOL;
int EN_TANKDIAM;
int EN_MINVOLUME;
int EN_VOLCURVE;
int EN_MINLEVEL;
int EN_MAXLEVEL;
int EN_MIXFRACTION;
int EN_TANK_KBULK;
int EN_TANKVOLUME;
int EN_MAXVOLUME;
int EN_DIAMETER;
int EN_LENGTH;
int EN_ROUGHNESS;
int EN_MINORLOSS;
int EN_INITSTATUS;
int EN_INITSETTING;
int EN_KBULK;
int EN_KWALL;
int EN_FLOW;
int EN_VELOCITY;
int EN_HEADLOSS;
int EN_STATUS;
int EN_SETTING;
int EN_ENERGY;
int EN_LINKQUAL;
int EN_LINKPATTERN;
int EN_DURATION;
int EN_HYDSTEP;
int EN_QUALSTEP;
int EN_PATTERNSTEP;
int EN_PATTERNSTART;
int EN_REPORTSTEP;
int EN_REPORTSTART;
int EN_RULESTEP;
int EN_STATISTIC;
int EN_PERIODS;
int EN_STARTTIME;
int EN_HTIME;
int EN_QTIME;
int EN_HALTFLAG;
int EN_NEXTEVENT;
int EN_ITERATIONS;
int EN_RELATIVEERROR;
int EN_NODECOUNT;
int EN_TANKCOUNT;
int EN_LINKCOUNT;
int EN_PATCOUNT;
int EN_CURVECOUNT;
int EN_CONTROLCOUNT;
int EN_JUNCTION;
int EN_RESERVOIR;
int EN_TANK;
int EN_CVPIPE;
int EN_PIPE;
int EN_PUMP;
int EN_PRV;
int EN_PSV;
int EN_PBV;
int EN_FCV;
int EN_TCV;
int EN_GPV;
int EN_NONE;
int EN_CHEM;
int EN_AGE;
int EN_TRACE;
int EN_CONCEN;
int EN_MASS;
int EN_SETPOINT;
int EN_FLOWPACED;
int EN_CFS;
int EN_GPM;
int EN_MGD;
int EN_IMGD;
int EN_AFD;
int EN_LPS;
int EN_LPM;
int EN_MLD;
int EN_CMH;
int EN_CMD;
int EN_TRIALS;
int EN_ACCURACY;
int EN_TOLERANCE;
int EN_EMITEXPON;
int EN_DEMANDMULT;
int EN_LOWLEVEL;
int EN_HILEVEL;
int EN_TIMER;
int EN_TIMEOFDAY;
int EN_AVERAGE;
int EN_MINIMUM;
int EN_MAXIMUM ;
int EN_RANGE ;
int EN_MIX1;
int EN_MIX2 ;
int EN_FIFO ;
int EN_LIFO ;
int EN_NOSAVE ;
int EN_SAVE;
int EN_INITFLOW ;
int EN_SAVE_AND_INIT;
int EN_CONST_HP;
int EN_POWER_FUNC;
int EN_CUSTOM;
//Additional variables in Ibanez's version of EN2
int EN_VOLUME;
int EN_UPATTERN;
int EN_INITVOL;
int EN_SCHEDULE;
int EN_CLOCKSTART;
int EN_PUMPCOUNT;
int EN_RESERVCOUNT;
int EN_JUNCSCOUNT;
//Additional variables in EN3
//int EN_BASEDEMAND;
int EN_EMITTERFLOW ;
int EN_FULLDEMAND ;
int EN_ACTUALDEMAND ;
int EN_OUTFLOW ;
int EN_LEAKCOEFF1 ;
int EN_LEAKCOEFF2 ;
int EN_LEAKAGE ;
int EN_STARTDATE ;
int EN_RULECOUNT ;
int EN_RESVCOUNT ;
int EN_QUALTOL ;
int EN_HYDTOL ;
int EN_MINPRESSURE ;
int EN_MAXPRESSURE ;
int EN_PRESSEXPON ;
int EN_NETLEAKCOEFF1;
int EN_NETLEAKCOEFF2;
int EN_BASEPATTERN;
int EN_NOINITFLOW;
int evaluator_id = 0;
#include "ENFunctFlt2Dbl.h"
#include "InitialiseIbanezEN2.h"
#include "InitialiseOwaEN2.h"
#include "InitialiseEN3.h"
// --- Define the EPANET toolkit constants
typedef struct _ENsimulation_t * ENsimulation_t;
ENMultiObjEvaluator::ENMultiObjEvaluator()
: logging(boost::none)
{
}
ENMultiObjEvaluator::~ENMultiObjEvaluator()
{
if (this->params->en_lib_version == OWA_EN2)
{
this->close_OWA_EN2();
}
else if(this->params->en_lib_version == OWA_EN3)
{
this->close_OWA_EN3();
}
else if(this->params->en_lib_version == Ibanev_EN2)
{
this->close_Ibanez_EN2();
}
else
{
std::cout << "Error: Epanet Library Version unspecified. Library path also probably not specified\n";
}
// Close Ratings library
if (dlclose(en_lib_handle) != 0) {
printf("[%s] Problem closing library: %s", __FILE__, dlerror());
}
if (do_remove_work_dir_on_exit)
{
boost::filesystem::remove_all(working_dir);
}
}
void
ENMultiObjEvaluator::initialise(boost::filesystem::path opt_cfg_path, boost::filesystem::path _working_dir, bool _do_remove_work_dir_on_exit)
{
namespace fs = boost::filesystem;
do_remove_work_dir_on_exit = _do_remove_work_dir_on_exit;
// if (initParams)
// {
//copy the params to the working directory.
// Transfer a copy of the opt cfg file to the working directory...
// fs::path working_opt_path = this->working_dir
// / orig_opt_file_path.filename();
// if (!fs::exists(working_opt_path))
// {
// fs::copy_file(orig_opt_file_path, working_opt_path);
// }
//open the params
params = getObjectiveInput(opt_cfg_path);
// Copy project directory into working directory
std::string temp_dir_template = "WDSOpt_worker" + std::to_string(evaluator_id++) + "_%%%%-%%%%";
working_dir = boost::filesystem::unique_path(_working_dir / temp_dir_template);
if (!fs::exists(this->working_dir))
{
if (!fs::create_directories(this->working_dir))
{
std::string err = "Unable to create EN working directory: "
+ this->working_dir.string();
throw std::runtime_error(err);
}
}
boost::filesystem::path epanet_file_path(params->epanetFile);
if (!fs::exists(epanet_file_path))
{
std::string err = "Epanet input file does not exist: "
+ en_inp_path.string();
throw std::runtime_error(err);
}
en_inp_path = working_dir / epanet_file_path.filename();
boost::filesystem::copy_file(epanet_file_path, en_inp_path);
epanet_dylib_path = params->epanet_dylib_loc;
if (!fs::exists(epanet_dylib_path))
{
std::string err = "Epanet library does not exist at: "
+ epanet_dylib_path.string();
throw std::runtime_error(err);
}
++dlOpenCount;
//Work out the location for the report file....
std::string tmpReportName = reportFileName
+ boost::lexical_cast< std::string >(dlOpenCount) + ".rpt";
en_report_path = this->working_dir / tmpReportName;
//create the report and binary files...
reportFile_cstr.reset(new char[en_report_path.string().size() + 1]);
strcpy(reportFile_cstr.get(), en_report_path.string().c_str());
//Woprk out the location for the binary file...
std::string tmpBinName = binaryFileName
+ boost::lexical_cast< std::string >(dlOpenCount) + ".bin";
en_bin_path = this->working_dir / tmpBinName;
binaryFile_cstr.reset(new char[en_bin_path.string().size() + 1]);
strcpy(binaryFile_cstr.get(), en_bin_path.string().c_str());
if (this->params->en_lib_version == OWA_EN2)
{
this->initialise_OWA_EN2();
this->open_OWA_EN2();
}
else if(this->params->en_lib_version == OWA_EN3)
{
this->initialise_OWA_EN3();
this->open_OWA_EN3();
}
else if(this->params->en_lib_version == Ibanev_EN2)
{
this->initialise_Ibanez_EN2();
this->open_Ibanez_EN2();
}
else
{
std::cout << "Error: Epanet Library Version unspecified. Library path also probably not specified\n";
}
// Determine the pipe indices for the links we are optimising choice of pipe for.
this->setLinkIndices();
this->setNodeIndices();
this->getENInfo();
}
bool
ENMultiObjEvaluator::log()
{
if (!logging)
{
boost::filesystem::path log_path = working_dir.parent_path() / "enLink_calcs.log";
logging = boost::shared_ptr<std::ofstream>(new std::ofstream(log_path.string().c_str()));
if(!(logging.value()->is_open()))
{
logging = boost::none;
return (false);
}
// this->do_remove_work_dir_on_exit = false; /// NOw saved in directory above working dir.
}
return (true);
}
void
ENMultiObjEvaluator::getENInfo()
{
//GET infromation of network from EN.
errors(ENgetcount_f(EN_NODECOUNT, &(this->node_count)));
errors(ENgetcount_f(EN_LINKCOUNT, &(this->link_count)));
nodes.resize(node_count+1);
links.resize(link_count+1);
char id_buffer[16] = "";
int type_buffer = 0;
int j, end_j, k, end_k;
if (this->params->en_lib_version == OWA_EN2)
{
j = 1;
end_j = this->node_count;
k = 1;
end_k = this->link_count;
}
else if(this->params->en_lib_version == OWA_EN3)
{
j = 0;
end_j = this->node_count - 1;
k = 0;
end_k = this->link_count - 1;
}
else if(this->params->en_lib_version == Ibanev_EN2)
{
j = 1;
end_j = this->node_count;
k = 1;
end_k = this->link_count;
}
else
{
std::cout << "Error: Epanet Library Version unspecified. Library path also probably not specified\n";
}
for (; j <= end_j; ++j)
{
NodeInfo& node = nodes[j];
node.index = j;
errors(ENgetnodeid_f(j,id_buffer));
node.id = std::string(id_buffer);
errors(ENgetnodetype_f(j, &type_buffer));
switch (type_buffer)
{
case 0 :
node.type = ENLNK_JUNCTION;
break;
case 1 :
node.type = ENLNK_RESERVOIR;
break;
case 2:
node.type = ENLNK_TANK;
break;
}
}
for (; k <= end_k ; ++k)
{
LinkInfo& link = links[k];
link.index = k;
errors(ENgetlinkid_f(k,id_buffer));
link.id = std::string(id_buffer);
errors(ENgetlinktype_f(k,&type_buffer));
switch (type_buffer)
{
case 0:
link.type = ENLNK_CVPIPE;
break;
case 1:
link.type = ENLNK_PIPE;
break;
case 2:
link.type = ENLNK_PUMP;
break;
case 3:
link.type = ENLNK_PRV;
break;
case 4:
link.type = ENLNK_PSV;
break;
case 5:
link.type = ENLNK_PBV;
break;
case 6:
link.type = ENLNK_FCV;
break;
case 7:
link.type = ENLNK_TCV;
break;
case 8:
link.type = ENLNK_GPV;
break;
}
errors(ENgetlinknodes_f(k, &(link.fromNode), &(link.toNode)));
nodes[link.fromNode].connectedLinkIndices.push_back(k);
nodes[link.toNode].connectedLinkIndices.push_back(k);
}
}
void
ENMultiObjEvaluator::setLinkIndices()
{
linkIndices.clear();
int enIndex;
//Work out how many different types of pipe can be chosen for each group of epanet links.
LinkGroupsType::iterator pipeGroup =
params->link_groups_data.begin();
LinkGroupsType::iterator end = params->link_groups_data.end();
for (; pipeGroup != end; ++pipeGroup)
{
LinkGroups::iterator enLinkId =
LinkGroups(pipeGroup).epanetLinkIds.begin();
LinkGroups::iterator endId =
LinkGroups(pipeGroup).epanetLinkIds.end();
//std::cout << "\"" << PipeGroups(pipeGroup).id << "\"" << std::endl;
//Open the toolkit and the input file in epanet
// std::vector<char> writableLinkID;
for (; enLinkId != endId; ++enLinkId)
{
// writableLinkID.clear();
// writableLinkID.resize(enLinkId->size());
// std::copy(enLinkId->begin(), enLinkId->end(), writableLinkID.begin());
// writableLinkID.push_back('\0');
//delete[] linkID;
linkID.reset(new char[enLinkId->size() + 1]);strcpy(linkID.get(),enLinkId->c_str());
//std::cout << "\"" << linkID << "\"";
//char * trial = "P_0.0.0_1.0.0";
//int n;
//ENgetlinkindex(trial, &n);
//std::cout << " for " << trial << " index = " << n << std::endl;
errors(ENgetlinkindex_f(linkID.get(), &enIndex), *enLinkId);
//debug << *enLinkId << ": ";
//debug << " for " << linkID.get() << " index = " << enIndex << std::endl;
//errors(ENgetlinkindex(&writableLinkID[0], &enIndex);
linkIndices.insert(std::pair< std::string, int >(*enLinkId,
enIndex));
}
}
//Also check that the pipes in the velocitties constraint list are present in the map
VelocityConstraintsT::iterator vConstraintPipe =
params->velocity_constraints.begin();
VelocityConstraintsT::iterator end_vConst =
params->velocity_constraints.end();
for (; vConstraintPipe != end_vConst; ++vConstraintPipe)
{
//delete[] linkID;
linkID.reset(
new char[Velocities(vConstraintPipe).id.size() + 1]);strcpy(linkID.get(), Velocities(vConstraintPipe).id.c_str());
errors(ENgetlinkindex_f(linkID.get(), &enIndex), Velocities(
vConstraintPipe).id);
linkIndices.insert(std::pair< std::string, int >(Velocities(
vConstraintPipe).id, enIndex));
}
}
void
ENMultiObjEvaluator::setNodeIndices()
{
nodeIndices.clear();
int enIndex;
// std::string nodeId;
// std::vector<char> writableNodeID; //char for en funtion call;
//Work out how many different types of pipe can be chosen for each group of epanet links.
NodeConstraintsT::iterator enNode =
params->pressure_constraints.begin();
NodeConstraintsT::iterator end =
params->pressure_constraints.end();
for (; enNode != end; ++enNode)
{
// nodeId = Pressures(enNode).id;
// writableNodeID.clear();
// writableNodeID.resize(nodeId.size());
// std::copy(nodeId.begin(), nodeId.end(), writableNodeID.begin());
// writableNodeID.push_back('\0');
//delete[] nodeID;
nodeID.reset(new char[Pressures(enNode).id.size() + 1]);strcpy(nodeID.get(),Pressures(enNode).id.c_str());
//debug << "\"" << nodeID << "\"";
errors(ENgetnodeindex_f(nodeID.get(), &enIndex), Pressures(enNode).id);
nodeIndices.insert(std::pair< std::string, int >(nodeID.get(),
enIndex));
//debug << " for " << nodeID.get() << " index = " << enIndex << std::endl;
}
float head = 0;
NodeConstraintsT::iterator hConstraint =
params->head_constraints.begin();
NodeConstraintsT::iterator endH =
params->head_constraints.end();
for (; hConstraint != endH; ++hConstraint)
{
nodeID.reset(new char[Heads(hConstraint).id.size() + 1]);strcpy(nodeID.get(),Heads(hConstraint).id.c_str());
//debug << "\"" << nodeID << "\"";
errors(ENgetnodeindex_f(nodeID.get(), &enIndex),
Heads(hConstraint).id);
nodeIndices.insert(std::pair< std::string, int >(nodeID.get(),
enIndex));
}
}
bool
ENMultiObjEvaluator::errors(int err, std::string what)
{
if (err > 100)
{
std::cerr << "\n EPANET2 ERROR CODE: " << err << std::endl;
err_out << "\n EPANET2 ERROR CODE: " << err << std::endl;
int nchar = 160;
//char errorMsg[nchar + 1];
char errorMsg[161];
this->errors(ENgeterror_f(err, errorMsg, nchar));
std::cerr << "\n\t MEANING: " << errorMsg << std::endl;
err_out << "\n EPANET2 ERROR CODE: " << err << std::endl;
std::cerr << "RELATED TO: " << what << std::endl;
err_out << "RELATED TO: " << what << std::endl;
}
bool returnVal = true;
if (err != 0)
returnVal = false;
return (returnVal);
}
/**
* @brief Calculates the net present value of a series
* @param annualValue
* @param numYears
* @param rate
* @return
*/
inline double
net_present_value_series(double annualValue, int numYears, double rate)
{
return ((1.0 - std::pow(1.0 + rate, -1.0 * numYears)) / rate) * annualValue;
};
/**
* @brief Calculates the present value of an amount paid at some point in the future
* @param amount The amount paid in year numYears
* @param numYears The time in the future where the amount is paid
* @param rate The discount rate
*/
inline double
net_present_value(double amount, int numYears, double rate)
{
return amount / std::pow(1.0 + rate, numYears);
};
boost::shared_ptr<std::vector<int> >
ENMultiObjEvaluator::getPipeDVBounds()
{
int numberDVs = params->pipe_availOpt_data.size();
boost::shared_ptr<std::vector<int> > dv_bounds(new std::vector<int>);
typedef std::pair<const std::string, std::vector<std::string> > PipeAvailOptionsPair;
BOOST_FOREACH(PipeAvailOptionsPair & pipe_options, params->pipe_availOpt_data)
{
dv_bounds->push_back(pipe_options.second.size());
}
return (dv_bounds);
};
void
ENMultiObjEvaluator::setPipeChoices(const int* dvs)
{
PipeAvailOptionsType::iterator pipeGroup =
params->pipe_availOpt_data.begin();
PipeAvailOptionsType::iterator endGroup =
params->pipe_availOpt_data.end();
std::string * pipeChoiceID;
PipeDataProperties * pipeChoiceData;
const std::string * pipeGroupID;
double pipelength = 0;
double totalLength = 0;
double lengthAvgedDiameter = 0;
if (logging)
{
**logging << "Pipe option choices:\n";
**logging << std::setw(20) << std::right << "LinkID" << std::setw(20) << std::right << "dv#"
<< std::setw(20) << std::right << "group#" << std::setw(20) << std::right << "choice#"
<< std::setw(20) << std::right << "Status" << std::setw(20) << std::right << "Diameter"
<< std::setw(20) << std::right << "Length" << std::setw(20) << std::right << "roughnesscost\n";
}
int dv_number = 0;
while (pipeGroup != endGroup)
{
int dv_val = dvs[dv_number];
pipeChoiceID =
&(PipeAvailOptions(pipeGroup).pipeDataIds[dv_val]);
pipeGroupID = &(PipeAvailOptions(pipeGroup).groupId);
pipeChoiceData = &(params->pipe_data_map[*pipeChoiceID]);
//Get pointers to the links/pipes in the group
//std::cout << "SIZE: " << params->pipe_groups_data[pipeGroupID].size() << std::endl;
LinkGroups::iterator enLinkId =
params->link_groups_data[*pipeGroupID].begin();
LinkGroups::iterator endId =
params->link_groups_data[*pipeGroupID].end();
//Iterate through the epanet links in this group.
for (; enLinkId != endId; ++enLinkId)
{
//set diameter
//std::cout << " link indice: " << linkIndices[*enPipe] << std::endl;
if (pipeChoiceData->diameter > 0)
{
this->errors(
ENsetlinkvalue_f(linkIndices[*enLinkId], EN_INITSTATUS,
1), *enLinkId + ": Setting link to open"); // LINK IS OPEN
this->errors(
ENsetlinkvalue_f(linkIndices[*enLinkId], EN_DIAMETER,
pipeChoiceData->diameter),
*enLinkId + ": Setting link diameter to "
+ boost::lexical_cast< std::string >(
pipeChoiceData->diameter));
this->errors(
ENgetlinkvalue_f(linkIndices[*enLinkId], EN_LENGTH,
&pipelength),
*enLinkId + ": Getting link length ");
totalLength += pipelength;
lengthAvgedDiameter += pipelength
* pipeChoiceData->diameter;
this->errors(
ENsetlinkvalue_f(linkIndices[*enLinkId], EN_ROUGHNESS,
pipeChoiceData->roughnessHeight),
*enLinkId + ": Setting link roughness to "
+ boost::lexical_cast< std::string >(
pipeChoiceData->roughnessHeight));
if(logging)
{
**logging << *enLinkId << std::setw(20) << std::right << dv_number
<< std::setw(20) << std::right << pipeGroup->first
<< std::setw(20) << std::right << dv_val
<< std::setw(20) << std::right << "open"
<< std::setw(20) << std::right << pipeChoiceData->diameter
<< std::setw(20) << std::right << pipelength
<< std::setw(20) << std::right << pipeChoiceData->roughnessHeight
<< std::setw(20) << std::right << pipeChoiceData->cost << "\n";
}
}
else
{
this->errors(
ENsetlinkvalue_f(linkIndices[*enLinkId], EN_INITSTATUS,
0), *enLinkId); // LINK IS CLOSED
if(logging)
{
**logging << *enLinkId << "\t" << "closed" << "\n";
}
}
}
++pipeGroup;
++dv_number;
}
lengthAvgedDiameter /= totalLength;
results.lengthAvgDiameter = lengthAvgedDiameter;
if(logging)
{
**logging << "\n\n\n";
}
}
void
ENMultiObjEvaluator::evalPipes(const int* dvs)
{
results.pipeCapitalCost = 0;
results.pipeRepairCost = 0;
results.pipeEmbodiedEnergy = 0;
double length;
std::string * pipeChoiceID;
const std::string * pipeGroupID;
PipeDataProperties * pipeChoiceData;
PipeAvailOptionsType::iterator pipeGroup =
params->pipe_availOpt_data.begin();
PipeAvailOptionsType::iterator endGroup =
params->pipe_availOpt_data.end();
double t_pipeCapitalCost = 0;
double t_pipeRepairCost = 0;
double t_pipeRepairEE = 0;
double t_pipeEmbodiedEnergy = 0;
double t_trenchingCost = 0;
double t_trenchEE = 0;
int dv_number = 0;
if (logging)
{
**logging << "Pipe costings:\n";
**logging << "PipeID" << std::setw(20) << std::right << "CapitalCost"
<< std::setw(20) << std::right << "RepairCost"<< std::setw(20) << std::right << "RepairEE"
<< std::setw(20) << std::right << "EmbodiedEnergy"<< std::setw(20) << std::right << "trenchCost"
<< std::setw(20) << std::right << "trenchEE\n";
}
while (pipeGroup != endGroup)
{
int dv_val = dvs[dv_number];
pipeChoiceID =
&(PipeAvailOptions(pipeGroup).pipeDataIds[dv_val]);
pipeGroupID = &(PipeAvailOptions(pipeGroup).groupId);
pipeChoiceData = &(params->pipe_data_map[*pipeChoiceID]);
//Get pointers to the links/pipes in the group
//std::cout << "SIZE: " << params->pipe_groups_data[pipeGroupID].size() << std::endl;
LinkGroups::iterator enLinkId =
params->link_groups_data[*pipeGroupID].begin();
LinkGroups::iterator endId =
params->link_groups_data[*pipeGroupID].end();
//Iterate through the epanet links in this group.
for (; enLinkId != endId; ++enLinkId)
{
//std::cout << " link indice: " << linkIndices[*enPipe] << std::endl;
//find length
this->errors(ENgetlinkvalue_f(linkIndices[*enLinkId], EN_LENGTH, &length), *enLinkId);
//calculate cost
if (params->isCostObj)
{
t_pipeCapitalCost += ((params->FittingsCost > 0) ? (1 + params->FittingsCost) : 1)
* (length * pipeChoiceData->cost); //The pipe cost
results.pipeCapitalCost += t_pipeCapitalCost;
//calculate repair cost... for this pipe in one year
t_pipeRepairCost += (length * pipeChoiceData->repairCost); //The pipe cost
results.pipeRepairCost += t_pipeRepairCost;
t_pipeRepairEE += (length * pipeChoiceData->repairEE);
results.pipeRepairEE += t_pipeRepairEE;
}
if (params->isEnergyObj)
{
t_pipeEmbodiedEnergy = ((params->FittingsEE > 0) ? (1 + params->FittingsEE) : 1)
* (length * pipeChoiceData->embodiedEnergy); //The pipe cost
results.pipeEmbodiedEnergy += t_pipeEmbodiedEnergy;
}
if (params->TrenchingCost > 0)
{
double trenchVolume = ((pipeChoiceData->diameter+ 2 * pipeChoiceData->sideCover) / 1000) //trenching width - divide by 1000 to convert from mm to m
* ((pipeChoiceData->diameter + pipeChoiceData->topCover + pipeChoiceData->bottomCover) / 1000) //trenching depth - divide by 1000 to convert from mm to m
* length;
if (params->isCostObj)
{
t_trenchingCost = trenchVolume * params->TrenchingCost;
results.pipeCapitalCost += t_trenchingCost;
} // The trenching cost
if (params->isEnergyObj)
{
t_trenchEE = trenchVolume * params->trenchEE;
results.pipeEmbodiedEnergy += t_trenchEE;
}
}
if (logging)
{
**logging << *enLinkId << std::setw(20) << std::right << t_pipeCapitalCost
<< std::setw(20) << std::right << t_pipeRepairCost
<< std::setw(20) << std::right << t_pipeRepairEE
<< std::setw(20) << std::right << t_pipeEmbodiedEnergy
<< std::setw(20) << std::right << t_trenchingCost
<< std::setw(20) << std::right << t_trenchEE << "\n" ;
}
}
++pipeGroup;
++dv_number;
}
// Calculate net present value for this series
if (params->drateCost > 0 && params->planHorizon > 0)
{
if (params->isCostObj && params->isDistSystem) results.pipeRepairCost =
net_present_value_series(results.pipeRepairCost,
params->planHorizon, params->drateCost);
if (params->isEnergyObj && params->isDistSystem) results.pipeRepairEE =
net_present_value_series(results.pipeRepairEE,
params->planHorizon, params->drateEE);
}
if(logging)
{
**logging << "\n\n\n";
}
}
int
ENMultiObjEvaluator::runEN(const int* dvs)
{
// Set varaibles based on choice vector
this->setPipeChoices(dvs);
// Run simulations
this->evalHydraulicConstraints();
results.net_costPumpBreakdown.pumpCivilCost = 0;
results.net_costPumpBreakdown.pumpElectricalCost = 0;
results.net_costPumpBreakdown.pumpMechanicalCost = 0;
results.net_costPumpBreakdown.pumpRunningCost = 0;
results.net_costPumpBreakdown.pumpRunningEnergy = 0;
results.costPumps = 0;
if (params->isCostObj || params->isEnergyObj) this->evalPipes(
dvs);
return 0;
}
void
ENMultiObjEvaluator::evalPressurePenalty()
{
NodeConstraintsT::iterator pConstraint =
params->pressure_constraints.begin();
NodeConstraintsT::iterator end =
params->pressure_constraints.end();
double pressure = 0;
double deviationLow = 0;
double deviationHigh = 0;
double maxDeviationHigh = 0;
double maxDeviationLow = 0;
double sumDeviationHigh = 0;
double sumDeviationLow = 0;
double constraintLow = 0;
double constraintHigh = 0;
std::string nodeID;
if (logging)
{
**logging << "Pressure constraint evaluation\n";
**logging << "NodeID" << std::setw(20) << std::right << "Pressure" << std::setw(20) << std::right << "constraintLow" << std::setw(20) << std::right << "deviationLow" << std::setw(20) << std::right << "constraintHigh" << std::setw(20) << std::right << "deviationHigh\n";
}
for (; pConstraint != end; ++pConstraint)
{
//std::cout << "Junction: \"" << Pressures(pConstraint).id << "\"" << std::endl;
//std::cout << "ID: \"" << nodeIndices[Pressures(pConstraint).id] << "\"" << std::endl;
deviationLow = 0;
deviationHigh = 0;
nodeID = Pressures(pConstraint).id;
this->errors(
ENgetnodevalue_f(nodeIndices[nodeID],
EN_PRESSURE, &pressure), nodeID);
constraintLow = Pressures(pConstraint).minPressure;
if ((pressure) < constraintLow)
{
deviationLow = (constraintLow - pressure);
sumDeviationLow += deviationLow;
if (deviationLow < maxDeviationLow)
maxDeviationLow = deviationLow;
// results.minPressureTooLow +=
// (Pressures(pConstraint).minPressure - pressure);
}
constraintHigh = Pressures(pConstraint).maxPressure;
if ((pressure) > constraintHigh)
{
deviationHigh = pressure - constraintHigh;
sumDeviationHigh += deviationHigh;
if (deviationHigh > maxDeviationHigh)
maxDeviationHigh = deviationHigh;
// results.maxPessureTooHigh += (pressure
// - Pressures(pConstraint).maxPressure);
}
if (logging)
{
**logging << nodeID << std::setw(20) << std::right << pressure << std::setw(20) << std::right << constraintLow << std::setw(20) << std::right << deviationLow << std::setw(20) << std::right << constraintHigh << std::setw(20) << std::right << deviationHigh << "\n";
}
}
results.minPressureTooLow = maxDeviationLow;
results.maxPressureTooHigh = maxDeviationHigh;
results.sumPressureTooLow = sumDeviationLow;
results.sumPressureTooHigh = sumDeviationHigh;
if(logging)
{
**logging << "\n\n\n";
}
}
void
ENMultiObjEvaluator::evalHeadPenalty()
{
NodeConstraintsT::iterator hConstraint =
params->head_constraints.begin();
NodeConstraintsT::iterator end = params->head_constraints.end();
double head = 0;
double deviationLow = 0;
double deviationHigh = 0;