def write_swmm_for_saving(self, name, number, args, p): parameters = args["parameters"] linestring = args.get('parameters', None).linestring if self.parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_STAGE: for (i, ls) in enumerate( swmm_ea_controller.extractSWMMmultiplefiles(linestring)): swmmfile = (parameters.projectdirectory + os.sep + parameters.resultsdirectory + os.sep + (name + "_stage%(x)03i") % { "#": number, "x": i }) + ".inp" swmmWrite(scale(p, parameters), ls, swmmfile) elif self.parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_FLOOD: swmmfile = (parameters.projectdirectory + os.sep + parameters.resultsdirectory + os.sep + name % { "#": number }) + ".inp" swmmWrite(scale(p, parameters), linestring, swmmfile) elif self.parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_CALIB: swmmfile = (parameters.projectdirectory + os.sep + parameters.resultsdirectory + os.sep + name % { "#": number }) + ".inp" swmmWrite(scale(p, parameters), linestring, swmmfile) # if calibration write a small ini file with calibration data file name in it. shutil.copy2(self.parameters.calINITEMPLATE, swmmfile[:-3] + "ini") else: print "I don't know this type of analysis: ", self.parameters.swmmouttype, " !!!"
def getFitness(fillers, linestring,parameters): fitness=0.0 try: scaled=scale(fillers,parameters) filename = parameters.projectdirectory+os.sep+"tmp"+os.sep+("%07d" % (current_process().pid))+".inp" make_sure_path_exists(os.path.dirname(filename)) dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) #print "\tFlood : ", flood, sum(map(lambda fil: fil,scaled)) if parameters.swmmouttype[0]==swmm_ea_controller.SWMMREULTSTYPE_FLOOD or parameters.swmmouttype[0]==swmm_ea_controller.SWMMREULTSTYPE_STAGE : costf=pyratemp.Template("@!"+parameters.cost_function+"!@") pp=parse_parameters(scale(fillers,parameters)) pp['discount_rate']=parameters.discount_rate pp['stage_size']=parameters.stage_size pp['stages']=parameters.stages cost1=float(costf(**(pp))) costf=pyratemp.Template("@!"+parameters.swmmout_cost_function+"!@") if parameters.swmmouttype[0]==swmm_ea_controller.SWMMREULTSTYPE_STAGE: cost2=0.0 for (i,ls) in enumerate(swmm_ea_controller.extractSWMMmultiplefiles(linestring)): cost = swmmCost(scaled, ls, filename,parameters) pp={"f": cost} t1=float(costf(**(pp)))*(1-(1+parameters.discount_rate)**(-1*parameters.stage_size))/parameters.discount_rate # t1 is the NPV at the start of the ith stage. (http://www.investopedia.com/articles/03/101503.asp#axzz2KWaBIiKB) # now bring it to present value. cost2+=t1/(1+parameters.discount_rate)**(i*parameters.stage_size) #/(1+parameters.discount_rate)**(parameters.stage_size*i) #cost2*=parameters.stage_size else: cost = swmmCost(scaled, linestring, filename,parameters) pp={"f": cost} cost2=float(costf(**(pp))) if parameters.multiObjective: fitness=inspyred.ec.emo.Pareto([cost1,cost2]) else: fitness=cost1+cost2 elif parameters.swmmouttype[0]==swmm_ea_controller.SWMMREULTSTYPE_CALIB: fitness=swmmCost(scaled, linestring, filename,parameters) else: print "I don't know the calculation type!" raise except: print "\nError here: !!!!\n\n" try: tb = traceback.format_exc() print tb except: print "\grave error!!!\n\n" fitness=None finally: return fitness
def testDiff(self, verbose=False): with open(self.swmmfile,"r") as f1, open(self.slotfile,"r") as f2: before=f1.read().strip() after=f2.read().strip() #dmp.diff_main(before,after) if(self.multiple): l=swmm_ea_controller.extractSWMMmultiplefiles(after) slist=[self.testDiffStr(x, before, verbose) for x in l] if len(slist)!=self.multiple: print "Mismatch of number of input files in the compound file: %s!" % (self.slotfile) return False return reduce(lambda x, y: x and y, slist) else: return self.testDiffStr(after, before, verbose=verbose)
def write_swmm_for_saving(self, name, number, args, p): parameters=args["parameters"] linestring=args.get('parameters',None).linestring if self.parameters.swmmouttype[0]== swmm_ea_controller.SWMMREULTSTYPE_STAGE: for (i,ls) in enumerate(swmm_ea_controller.extractSWMMmultiplefiles(linestring)): swmmfile=(parameters.projectdirectory+os.sep+parameters.resultsdirectory+os.sep+(name+"_stage%(x)03i") % {"#":number,"x": i})+".inp" swmmWrite(scale(p,parameters),ls,swmmfile) elif self.parameters.swmmouttype[0]== swmm_ea_controller.SWMMREULTSTYPE_FLOOD: swmmfile=(parameters.projectdirectory+os.sep+parameters.resultsdirectory+os.sep+name % {"#":number})+".inp" swmmWrite(scale(p,parameters),linestring,swmmfile) elif self.parameters.swmmouttype[0]== swmm_ea_controller.SWMMREULTSTYPE_CALIB: swmmfile=(parameters.projectdirectory+os.sep+parameters.resultsdirectory+os.sep+name % {"#":number})+".inp" swmmWrite(scale(p,parameters),linestring,swmmfile) # if calibration write a small ini file with calibration data file name in it. shutil.copy2(self.parameters.calINITEMPLATE, swmmfile[:-3]+"ini") else: print "I don't know this type of analysis: ", self.parameters.swmmouttype, " !!!"
def getFitness(fillers, linestring, parameters): fitness = 0.0 try: scaled = scale(fillers, parameters) filename = parameters.projectdirectory + os.sep + "tmp" + os.sep + ( "%07d" % (current_process().pid)) + ".inp" make_sure_path_exists(os.path.dirname(filename)) dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) #print "\tFlood : ", flood, sum(map(lambda fil: fil,scaled)) if parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_FLOOD or parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_STAGE: costf = pyratemp.Template("@!" + parameters.cost_function + "!@") pp = parse_parameters(scale(fillers, parameters)) pp['discount_rate'] = parameters.discount_rate pp['stage_size'] = parameters.stage_size pp['stages'] = parameters.stages cost1 = float(costf(**(pp))) costf = pyratemp.Template("@!" + parameters.swmmout_cost_function + "!@") if parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_STAGE: cost2 = 0.0 for (i, ls) in enumerate( swmm_ea_controller.extractSWMMmultiplefiles( linestring)): cost = swmmCost(scaled, ls, filename, parameters) pp = {"f": cost} t1 = float(costf(**(pp))) * (1 - (1 + parameters.discount_rate) **(-1 * parameters.stage_size) ) / parameters.discount_rate # t1 is the NPV at the start of the ith stage. (http://www.investopedia.com/articles/03/101503.asp#axzz2KWaBIiKB) # now bring it to present value. cost2 += t1 / (1 + parameters.discount_rate)**( i * parameters.stage_size) #/(1+parameters.discount_rate)**(parameters.stage_size*i) #cost2*=parameters.stage_size else: cost = swmmCost(scaled, linestring, filename, parameters) pp = {"f": cost} cost2 = float(costf(**(pp))) if parameters.multiObjective: fitness = inspyred.ec.emo.Pareto([cost1, cost2]) else: fitness = cost1 + cost2 elif parameters.swmmouttype[ 0] == swmm_ea_controller.SWMMREULTSTYPE_CALIB: fitness = swmmCost(scaled, linestring, filename, parameters) else: print "I don't know the calculation type!" raise except: print "\nError here: !!!!\n\n" try: tb = traceback.format_exc() print tb except: print "\grave error!!!\n\n" fitness = None finally: return fitness