def __init__(self, capacity, items, duplicates=False): Benchmark.__init__(self, len(items)) self.capacity = capacity self.items = items self.components = [swarm.TrailComponent((item[0]), value=item[1]) for item in items] self.duplicates = duplicates self.bias = 0.5 if self.duplicates: max_count = [self.capacity // item[0] for item in self.items] self.bounder = ec.DiscreteBounder([i for i in range(max(max_count)+1)]) else: self.bounder = ec.DiscreteBounder([0, 1]) self.maximize = True self._use_ants = False
def __init__(self, seed): self.seed = seed # seed for random generator # self.bounder = ec.Bounder(0, 1) # Discrete bounder to boolean values self.bounder = ec.DiscreteBounder( [0, 1]) # Discrete bounder to boolean values self.maximize = False # Flag to define the problem nature self.genCount = 0 # generation count
def __init__(self, benchmark, dimension_bits): Benchmark.__init__(self, benchmark.dimensions, benchmark.objectives) self.benchmark = benchmark self.dimension_bits = dimension_bits self.bounder = ec.DiscreteBounder([0, 1]) self.maximize = self.benchmark.maximize self.__class__.__name__ = self.__class__.__name__ + ' ' + self.benchmark.__class__.__name__
def __init__(self, weights): Benchmark.__init__(self, len(weights)) self.weights = weights self.components = [swarm.TrailComponent((i, j), value=(1 / weights[i][j])) for i, j in itertools.permutations(range(len(weights)), 2)] self.bias = 0.5 self.bounder = ec.DiscreteBounder([i for i in range(len(weights))]) self.maximize = True self._use_ants = False
def __init__(self, number_of_sentences, coverage_matrix, sentences_similarity, ratio, objectives=1): self.dimensions = int(ratio * number_of_sentences) self.objectives = objectives self.bounder = None self.maximize = True self.coverage_matrix = coverage_matrix self.sentences_similarity = sentences_similarity self.ratio = ratio self.number_of_sentences = number_of_sentences self.bounder = ec.DiscreteBounder( [i for i in range(number_of_sentences)])
def __init__(self, dimensions=43278): # Column numbers of the Main_data_file: These are [the land type of various cell in 5 different scenarios: A-E A_LU=3 B_LU=4 C_LU=5 D_LU=6 E_LU=7 cSUIT=11 cNoChange=9 # It is Whether the land allocation changed w.r.t the base case: 0: implies change minA = {} # Minimum area for each land type (1-25) maxA = {} # Max area for each land type suit=[] olt = {} olt2nlt = {} # Original land type to number : Basically key value, key is the land type, value is id allotted to it adj_List = {} # Adjacency list for each cell dim=0 area = {} sumIndArea = [] # Sum of area for each type of land type (not cell), total 25 indxMap = {} areaMap = {} # area for each cell landType = [] generator_count = 0 for i in range(50000): suit.append(0) #olt.append(0) ''' 0: AIR: Airport, 1: CIV: Civic, 2:POS: Preserved Open Space 3:WF, Working Farm 3:RL, Rural Living 3:RCR, Rural Crossroads 4:ER, Estate Residential 4:LLRN, Large Lot Residential Neighborhood 5:SLRN, Small Lot Residential Neighborhood 5:MFRN, Multifamily Residential Neighborhood 5:MRN, Mixed Residential Neighborhood 5:MHP 6:HRR, Healthcare 7:NCC, Neighborhood Commercial Center 7:SCC, Suburban Commercial Corridor 7:SOC, Suburban Office Center 8:LIC, Light Industrial 8:HIC, Heavy Industrial 8:HCC, Highway Commercial Corridor 9:MUN, Mixed-Use Neighborhood 9:TC, Town Center 9:UN, Urban Neighborhood 9:TOD, Transit Oriented Development 9:MUC, Mixed Use Center 9:MC, Metropolitan Center 9:UC, Urban Center ''' olt2nlt["AIR"] = 1 olt2nlt["CIV"] = 2 olt2nlt["HIC"] = 3 olt2nlt["LIC"] = olt2nlt["HCC"] = 4 olt2nlt["LLRN"] = olt2nlt["ER"] = 5 olt2nlt["MFRN"] = 6 olt2nlt["MHP"] = 7 olt2nlt["MRN"] = 8 olt2nlt["MUN"] = 9 olt2nlt["NCC"] = 10 olt2nlt["POS"]= 11 olt2nlt["RCR"]= 12 olt2nlt["RL"] = 13 olt2nlt["SCC"]= 14 olt2nlt["SLRN"] = 15 olt2nlt["SOC"] = 16 olt2nlt["TC"] = 17 olt2nlt["UC"] = 18 olt2nlt["UN"] = 19 olt2nlt["VC"] = olt2nlt["HRR"] = 20 olt2nlt["WF"] = 21 olt2nlt["MUC"] = 22 olt2nlt["MC"] = 23 olt2nlt["TOD"] = 24 #Loading adj list for each cell f= open("Adj_List_file.gal","r") data = f.read().splitlines()[1:] for line in data: l = line.split(' ') if not l[0] is '': k = int(l[0]) for i in range(len(l)): #print i, k j = int(l[i]) if (i==0): # Adding the key, i.e. the cell for which adj_list is being obtained, adds for the first time, when it does not exist int he dictionary if not k in adj_List: adj_List[k]=[] else: adj_List[k].append(j) if not j in adj_List: adj_List[j] =[] if not k in adj_List[j]: adj_List[j].append(k) #print adj_List[20][1] #Loading areas, landtype for each cell g=open("Main_data_file.csv","r") # initiating the total sums for each landtype to zero for i in range(0,25): sumIndArea.append(0) datag = g.readlines()[1:] for line in datag: line = line.split('\t') pt = int(line[0]) ar = float(line[2]) #area tee = line[1] # landtype temp_lt = olt2nlt[tee] landType.append(temp_lt) # indxMap : contains decision (change/nochange), and four other preferred land types indxMap[pt] = (int(line[cNoChange]),olt2nlt[line[B_LU]] ,olt2nlt[line[C_LU]],olt2nlt[line[D_LU]],olt2nlt[line[E_LU]]) areaMap[pt] = ar sumIndArea[temp_lt] += ar copyLandType = landType copySumIndArea = sumIndArea area_sum=0.00 for i in range(43278): if not i in areaMap: areaMap[i]=0 for pt in areaMap.keys(): area_sum = area_sum + areaMap[pt] # Loading area targets gh = open("Tarets_File.csv","r") datagh = gh.readlines()[1:] for line in datagh: line = line.split('\t') #print line[0], line[1], line[2] minA[int(line[0])] = float(line[1]) * area_sum maxA[int(line[0])] = float(line[2]) * area_sum #print line[0], minA[int(line[0])], maxA[int(line[0])], area_sum #43151 self.dimensions = dim self.objectives = 25 Benchmark.__init__(self, dimensions, 25) #numobj=3, dimensions=numcells=6 self.bounder = ec.DiscreteBounder([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]) self.maximize = True # Maximizing criteria: i.e. maximizing the objectives self.adjList = adj_List self.candidatesArea = area self.landType = landType self.copyLandType = copyLandType self.areaMap = areaMap self.indxMap = indxMap self.sumIndArea = sumIndArea self.copySumIndArea = copySumIndArea self.total_area = area_sum self.minA = minA self.maxA = maxA self.suit = suit self.generator_count = generator_count
def __init__(self, dimensions=43278): Raster = True landtypes = {} invertedlandtypes = {} adj_List = {} indxMap = {} optimal = {} typeArea = defaultdict(float) roadDistance = {} attractiveness = {} devDistance = {} b = {} areaMap = {} compatibility = defaultdict(float) conversion = defaultdict(float) landUseAllowed = 3 minA = defaultdict(float) # Minimum area for each land type (1-25) maxA = defaultdict(float) # Max area for each land type currentTypes = [] currentline = 0 f = open("mdata.txt", "r") data = f.read().splitlines() # First line species the number and land use types l = data[currentline].split() currentline = +1 n = int(l[0]) # Mapping for LandTypes for i in range(1, n + 1): landtypes[l[i]] = i - 1 invertedlandtypes[i - 1] = l[i] # print landtypes # number of parcels parcels = int(data[currentline]) currentline = currentline + 1 # Adjacency List for i in range(0, parcels): line = data[currentline + i] neighbours = line.split() k = int(neighbours[0]) adj_List[k] = [] if (len(neighbours) > 1): for neighbour in range(1, len(neighbours)): j = int(neighbours[neighbour]) adj_List[k].append(j) currentline = currentline + parcels # print adj_List parcels = int(data[currentline]) currentline = currentline + 1 # Land Use type Index for i in range(0, parcels): line = data[currentline + i] allowedTypes = line.split() k = int(allowedTypes[0]) indxMap[k] = [] # print i currenttype = landtypes[allowedTypes[1]] currentTypes.append(currenttype) indxMap[k].append(currenttype) isallowed = int(allowedTypes[2]) indxMap[k].append(isallowed) if (len(allowedTypes) > 2): for allowedType in range(landUseAllowed, len(allowedTypes)): if allowedTypes[allowedType] in landtypes.keys(): j = landtypes[allowedTypes[allowedType]] else: j = landtypes["WF"] indxMap[k].append(j) currentline = currentline + parcels # print currentTypes n = int(data[currentline]) currentline = currentline + 1 # Land Use with min max areas and b for i in range(0, n): line = data[currentline + i] allowedTypes = line.split() currenttype = landtypes[allowedTypes[0]] typeArea[currenttype] = 0 minA[currenttype] = float(allowedTypes[1]) maxA[currenttype] = float(allowedTypes[2]) b[currenttype] = allowedTypes[2] currentline = currentline + n # print minA # print maxA parcels = int(data[currentline]) currentline = currentline + 1 #Current Area for i in range(0, parcels): line = data[currentline + i] allowedTypes = line.split() k = int(allowedTypes[0]) currenttype = landtypes[allowedTypes[1]] typeArea[currenttype] = typeArea[currenttype] + float( allowedTypes[2]) areaMap[i] = float(allowedTypes[2]) currentline = currentline + parcels # print 'Area*****' # print typeArea # parcels = int(data[currentline]); # currentline = currentline + 1 parcels = int(data[currentline]) currentline = currentline + 1 #Road Distance and Attractiveness for i in range(0, parcels): line = data[currentline + i] allowedTypes = line.split() pt = int(allowedTypes[0]) attractiveness[pt] = float(allowedTypes[1]) #Road Distance and Attractiveness # rasterFile = data[currentline].split()[2] ## print rasterFile # rows=0 # columns=0 # rasterInputs = '' # if data[currentline].split()[1] == 'Raster': # Raster =True # fr = open(rasterFile, "r") # datar = fr.read().splitlines() # rows = int(datar[0].split()[1]) # columns = int(datar[1].split()[1]) # for lines in range(0, 6): # rasterInputs = rasterInputs + datar[lines] +"\n" # print rasterInputs Raster = False # A_LU=3 # B_LU=4 # C_LU=5 # D_LU=6 # E_LU=7 # cSUIT=11 # cNoChange=9 # #id2idx = [] # #idx2id = [] # id2idx = {} # idx2id = {} # id2idx2 = {} # idx2id2 ={} # minA = {} # maxA = {} # suit=[] # olt = {} # for i in range(50000): # suit.append(0) #olt.append(0) # olt2nlt = {} # olt2nlt["AIR"] = 1 # olt2nlt["CIV"] = 2 # olt2nlt["HIC"] = 3 # olt2nlt["LIC"] = olt2nlt["HCC"] = 4 # olt2nlt["LLRN"] = olt2nlt["ER"] = 5 # olt2nlt["MFRN"] = 6 # olt2nlt["MHP"] = 7 # olt2nlt["MRN"] = 8 # olt2nlt["MUN"] = 9 # olt2nlt["NCC"] = 10 # olt2nlt["POS"]= 11 # olt2nlt["RCR"]= 12 # olt2nlt["RL"] = 13 # olt2nlt["SCC"]= 14 # olt2nlt["SLRN"] = 15 # olt2nlt["SOC"] = 16 # olt2nlt["TC"] = 17 # olt2nlt["UC"] = 18 # olt2nlt["UN"] = 19 # olt2nlt["VC"] = olt2nlt["HRR"] = 20 # olt2nlt["WF"] = 21 # olt2nlt["MUC"] = 22 # olt2nlt["MC"] = 23 # olt2nlt["TOD"] = 24 #Loading adj list for each cell # f= open("Adj_List_Final.gal","r") # data = f.read().splitlines()[1:] # adj_List = {} # for line in data: # l = line.split(' ') # if not l[0] is '': # k = int(l[0]) # for i in range(len(l)): # #print i, k # j = int(l[i]) # if (i==0): # if not k in adj_List: # adj_List[k]=[] # else: # adj_List[k].append(j) # # if not j in adj_List: # adj_List[j] =[] # if not k in adj_List[j]: # adj_List[j].append(k) #print adj_List[20][1] #Loading areas, landtype for each cell # g=open("Refined_Swas_E_Updated2.csv","r") # dim=0 # area = {} # for i in range(0,25): # sumIndArea.append(0) # #print sumIndArea[21] # datag = g.readlines()[1:] # indxMap = {} # areaMap = {} # landType = [] # for line in datag: # line = line.split('\t') # pt = int(line[0]) # ar = float(line[2]) #area # tee = line[1] # landtype # temp_lt = olt2nlt[tee] # landType.append(temp_lt) # # indxMap : contains decision (change/nochange), and four preferred land types # indxMap[pt] = (int(line[cNoChange]),olt2nlt[line[B_LU]] ,olt2nlt[line[C_LU]],olt2nlt[line[D_LU]],olt2nlt[line[E_LU]]) # areaMap[pt] = ar # #print temp_lt # sumIndArea[temp_lt] += ar #print indxMap[20] sumIndArea = defaultdict(float) sumIndArea = typeArea landType = currentTypes copyLandType = copy.deepcopy(currentTypes) copySumIndArea = copy.deepcopy(sumIndArea) area_sum = 0.00 for i in range(43278): if not i in areaMap: areaMap[i] = 0 for pt in areaMap.keys(): area_sum = area_sum + areaMap[pt] f = open("moptdata.txt", "r") optData = f.read().splitlines() currentline = 0 parcels = int(optData[currentline]) currentline = 1 # print optData[currentline]; # Land Use type Index for i in range(0, parcels): line = optData[currentline + i] # print line allowedTypes = line.split() k = int(allowedTypes[0]) optimal[k] = [] # print allowedTypes isallowed = int(allowedTypes[1]) optimal[k].append(isallowed) optimal[k].append(landtypes[allowedTypes[2]]) # print optimal[k] # currentline = currentline + parcels # Loading area targets # gh = open("SWAS_E_LU_PERCENT.csv","r") # datagh = gh.readlines() # for line in datagh: # line = line.split('\t') # #print line[0], line[1], line[2] # minA[int(line[0])] = float(line[1]) * area_sum # maxA[int(line[0])] = float(line[2]) * area_sum #print line[0], minA[int(line[0])], maxA[int(line[0])], area_sum rasterInputs = 1 rows = 1 columns = 1 #43151 self.dimensions = parcels self.objectives = 25 Benchmark.__init__(self, parcels, 25) #numobj=3, dimensions=numcells=6 #self.bounder = ec.Bounder([0] * self.dimensions, [3.0] * self.dimensions) self.bounder = ec.DiscreteBounder([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ]) self.maximize = True #self.candidatesArea = {0:10,1:10,2:10,3:10,4:10,5:10} #self.total_area = 60 #self.adjList = {0:[1,2],1:[0,2],2:[0,1,3],3:[2,4,5],4:[3,5],5:[3,4]} self.adjList = adj_List # self.candidatesArea = area self.landType = landType self.copyLandType = copyLandType self.areaMap = areaMap self.indxMap = indxMap self.optimal = optimal self.sumIndArea = sumIndArea self.copySumIndArea = copySumIndArea self.total_area = area_sum self.minA = minA self.maxA = maxA self.attractiveness = attractiveness self.devDistance = devDistance self.conversion = conversion # self.suit = suit self.Raster = Raster self.rasterVal = rasterInputs self.rows = rows self.columns = columns # http://people.hofstra.edu/geotrans/eng/ch6en/conc6en/ch6c2en.html for i in range(24): print 'Type********', i print self.minA[i] print self.maxA[i] print self.sumIndArea[i]
def __init__(self, dimensions=43278): A_LU = 3 B_LU = 4 C_LU = 5 D_LU = 6 E_LU = 7 cSUIT = 11 cNoChange = 9 #id2idx = [] #idx2id = [] id2idx = {} idx2id = {} id2idx2 = {} idx2id2 = {} minA = {} maxA = {} suit = [] olt = {} for i in range(50000): suit.append(0) #olt.append(0) ''' 0: AIR: Airport, 1: CIV: Civic, 2:POS: Preserved Open Space 3:WF, Working Farm 3:RL, Rural Living 3:RCR, Rural Crossroads 4:ER, Estate Residential 4:LLRN, Large Lot Residential Neighborhood 5:SLRN, Small Lot Residential Neighborhood 5:MFRN, Multifamily Residential Neighborhood 5:MRN, Mixed Residential Neighborhood 5:MHP 6:HRR, Healthcare 7:NCC, Neighborhood Commercial Center 7:SCC, Suburban Commercial Corridor 7:SOC, Suburban Office Center 8:LIC, Light Industrial 8:HIC, Heavy Industrial 8:HCC, Highway Commercial Corridor 9:MUN, Mixed-Use Neighborhood 9:TC, Town Center 9:UN, Urban Neighborhood 9:TOD, Transit Oriented Development 9:MUC, Mixed Use Center 9:MC, Metropolitan Center 9:UC, Urban Center ''' olt2nlt = {} olt2nlt["AIR"] = 1 olt2nlt["CIV"] = 2 olt2nlt["HIC"] = 3 olt2nlt["LIC"] = olt2nlt["HCC"] = 4 olt2nlt["LLRN"] = olt2nlt["ER"] = 5 olt2nlt["MFRN"] = 6 olt2nlt["MHP"] = 7 olt2nlt["MRN"] = 8 olt2nlt["MUN"] = 9 olt2nlt["NCC"] = 10 olt2nlt["POS"] = 11 olt2nlt["RCR"] = 12 olt2nlt["RL"] = 13 olt2nlt["SCC"] = 14 olt2nlt["SLRN"] = 15 olt2nlt["SOC"] = 16 olt2nlt["TC"] = 17 olt2nlt["UC"] = 18 olt2nlt["UN"] = 19 olt2nlt["VC"] = olt2nlt["HRR"] = 20 olt2nlt["WF"] = 21 olt2nlt["MUC"] = 22 olt2nlt["MC"] = 23 olt2nlt["TOD"] = 24 #Loading adj list for each cell f = open("Adj_List_Final.gal", "r") data = f.read().splitlines()[1:] adj_List = {} for line in data: l = line.split(' ') if not l[0] is '': k = int(l[0]) for i in range(len(l)): #print i, k j = int(l[i]) if (i == 0): if not k in adj_List: adj_List[k] = [] else: adj_List[k].append(j) if not j in adj_List: adj_List[j] = [] if not k in adj_List[j]: adj_List[j].append(k) #print adj_List[20][1] #Loading areas, landtype for each cell g = open("Refined_Swas_E_Updated2.csv", "r") dim = 0 area = {} sumIndArea = [] for i in range(0, 25): sumIndArea.append(0) #print sumIndArea[21] datag = g.readlines()[1:] indxMap = {} areaMap = {} landType = [] for line in datag: line = line.split('\t') pt = int(line[0]) ar = float(line[2]) #area tee = line[1] # landtype temp_lt = olt2nlt[tee] landType.append(temp_lt) # indxMap : contains decision (change/nochange), and four preferred land types indxMap[pt] = (int(line[cNoChange]), olt2nlt[line[B_LU]], olt2nlt[line[C_LU]], olt2nlt[line[D_LU]], olt2nlt[line[E_LU]]) areaMap[pt] = ar #print temp_lt sumIndArea[temp_lt] += ar #print indxMap[20] copyLandType = landType copySumIndArea = sumIndArea area_sum = 0.00 for i in range(43278): if not i in areaMap: areaMap[i] = 0 for pt in areaMap.keys(): area_sum = area_sum + areaMap[pt] # Loading area targets gh = open("SWAS_E_LU_PERCENT.csv", "r") datagh = gh.readlines() for line in datagh: line = line.split('\t') #print line[0], line[1], line[2] minA[int(line[0])] = float(line[1]) * area_sum maxA[int(line[0])] = float(line[2]) * area_sum #print line[0], minA[int(line[0])], maxA[int(line[0])], area_sum #43151 self.dimensions = dim self.objectives = 25 Benchmark.__init__(self, dimensions, 25) #numobj=3, dimensions=numcells=6 #self.bounder = ec.Bounder([0] * self.dimensions, [3.0] * self.dimensions) self.bounder = ec.DiscreteBounder([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ]) self.maximize = True #self.candidatesArea = {0:10,1:10,2:10,3:10,4:10,5:10} #self.total_area = 60 #self.adjList = {0:[1,2],1:[0,2],2:[0,1,3],3:[2,4,5],4:[3,5],5:[3,4]} self.adjList = adj_List self.candidatesArea = area self.landType = landType self.copyLandType = copyLandType self.areaMap = areaMap self.indxMap = indxMap self.sumIndArea = sumIndArea self.copySumIndArea = copySumIndArea self.total_area = area_sum self.minA = minA self.maxA = maxA self.suit = suit
def NSGA2(): """Starts the optimization with the NSGA-II algorithm.""" begin = time.time() # initialize random generator with system time rand = random.Random() rand.seed() # Generate the original start individual from input data # return it including the non static land use indices start_individual, nonstatic_elements = generate_genom(max_range, file_HRU,cfg.mapConfig.file_ASCII_map, cfg.mapConfig.file_transformation, cfg.mapConfig.file_ID_map, cfg.mapConfig.four_neighbours) if len(start_individual) == 0: msg = "Error: The generated start individual has no elements." WriteLogMsg(msg) raise SystemError("Error: The generated start individual has no elements.") close_window # determine that 'Bounder' conditions of candidates are equal to # the integer values of the non static land use indices bounder_discrete = nonstatic_elements # initialize inspyred log files stats_file,individ_file = fh.init_inspyred_logfiles() # initialize and run NSGA2 ea = ec.emo.NSGA2(rand) # public attributes # NSGA2 is predefined with tournament_selection if cfg.ea.selector != 'tournament_selection': exec ("%s%s" % ('ea.selector = ', fh.preparing_attribute('selector',cfg.ea.selector))) msg = 'Selector of the optimization algorithm changed to: %s' % cfg.ea.selector WriteLogMsg(msg) # NSGA2 is predefined with nsga_replacement if cfg.ea.replacer != 'nsga_replacement': exec ("%s%s" % ('ea.replacer = ', fh.preparing_attribute('replacer',cfg.ea.replacer))) msg = 'Replacer of the optimization algorithm changed to: %s' % cfg.ea.replacer WriteLogMsg(msg) # NSGA2 is predefined with best_archiver if cfg.ea.archiver != 'best_archiver': exec ("%s%s" % ('ea.archiver = ', fh.preparing_attribute('archiver',cfg.ea.archiver))) msg = 'Archiver of the optimization algorithm changed to: %s' % cfg.ea.archiver WriteLogMsg(msg) exec ("%s%s" % ('ea.migrator = ', fh.preparing_attribute('migrator',cfg.ea.migrator))) # file observer prints after each generation the best, worst, mean etc. values into the statistic and individual file exec ("%s%s" % ('ea.observer = ', fh.preparing_attribute('observer',cfg.ea.observer))) # specify how the new candidates should be varied exec ("%s%s" % ('ea.variator = ', fh.preparing_attribute('variator',cfg.ea.variator))) # specify when the optimization should terminate exec ("%s%s" % ('ea.terminator = ', fh.preparing_attribute('terminator',cfg.ea.terminator))) # NSGA2 is predefined with num_selected = pop_size if cfg.ea.num_selected != cfg.ea.pop_size: msg = 'Num_selected of the optimization algorithm changed to: %s' % cfg.ea.num_selected WriteLogMsg(msg) # NSGA2 is predefined with tournament_size = 2 if cfg.ea.tournament_size != 2: msg = 'Tournament_size of the optimization algorithm changed to: %s' % cfg.ea.tournament_size WriteLogMsg(msg) # run optimization, when finished final_pop holds the results final_pop = ea.evolve(generator = generate_parameter, # evaluate is the function to start external models # return results for the optimization algorithm evaluator = evaluate, # define population size pop_size = cfg.ea.pop_size, # maximize or Minimize the problem (default True) maximize = cfg.ea.maximize, # bound the parameters to an interval # DiscreteBounder: choose integer values between 1 and max_range bounder = ec.DiscreteBounder(bounder_discrete), # minimum population diversity allowed (when using diversity_termination default 0.001) min_diversity = cfg.ea.min_diversity, # maximum number of generations max_generations = cfg.ea.max_generations, # maximum number of evaluations (default pop_size) max_evaluations = cfg.ea.max_evaluations, # number of elites to consider (default 0) num_elites = cfg.ea.num_elites, # number of individuals to be selected (default NSGA2 pop_size) num_selected = cfg.ea.num_selected, # tournament size (default NSGA2 2) tournament_size = cfg.ea.tournament_size, # rate at which crossover is performed (default 1.0) crossover_rate = cfg.ea.crossover_rate, # rate at which mutation is performed (default 0.1) mutation_rate = cfg.ea.mutation_rate, # number of crossover points used (default 1) num_crossover_points = cfg.ea.num_crossover_points, # a positive integer representing the number of # closest solutions to consider as a “crowd” (default 2) crowding_distance = cfg.ea.crowding_distance, # statistic file statistics_file = stats_file, # individuals file individuals_file = individ_file) final_arc = ea.archive # for constrained_tournament_selection: # create a copy of final_arc only with feasible individuals (for csv file with best feasible solutions) if 'constrained_tournament_selection' in cfg.ea.selector: final_arc_feasible = [] end = time.time() msg = "The optimization process needed %d seconds." %(end-begin) fh.WriteLogMsg(msg) msg = 'Best Solutions: \n' WriteLogMsg(msg) f_count=1 for f in final_arc: # for constrained_tournament_selection: with information if individual is infeasible # and copy feasible solutions in final_arc_feasible if 'constrained_tournament_selection' in cfg.ea.selector: if individual_filter(f.candidate) == False: WriteLogMsg("(infeasible) %s" % f) # save the map as ascii file in output folder if file_HRU == 'None' or (file_HRU != 'None' and cfg.mapConfig.file_ID_map != 'None'): transform_individual_ascii_map(f.candidate,False,f_count,None,None,None,False) else: WriteLogMsg("%s" % f) # save the map as ascii file in output folder if file_HRU == 'None' or (file_HRU != 'None' and cfg.mapConfig.file_ID_map != 'None'): transform_individual_ascii_map(f.candidate,False,f_count) final_arc_feasible.append(f) else: msg = "%s" % f #msg = "%f" % f WriteLogMsg(msg) # save the map as ascii file in output folder if file_HRU == 'None' or (file_HRU != 'None' and cfg.mapConfig.file_ID_map != 'None'): transform_individual_ascii_map(f.candidate,False,f_count) if f_count == 1: len_fitness = len(f.fitness) f_count += 1 # plot the best solution in a 2, 3 or 4 dimensional plot # 2 dimensional plot if (cfg.ea.plot_results == True and len_fitness == 2): # log the best solutions in a csv file fh.save_best_solutions(final_arc,2) import pylab x = [] y = [] for f in final_arc: x.append(f.fitness[0]) y.append(f.fitness[1]) pylab.scatter(x, y, color='r') fh.savePlot_png(opt_algorithm) pylab.show() # for constrained_tournament_selection: create a second plot with feasible solutions if 'constrained_tournament_selection' in cfg.ea.selector: # log the best feasible solutions in a csv file fh.save_best_solutions(final_arc_feasible,2) x = [] y = [] for f in final_arc_feasible: x.append(f.fitness[0]) y.append(f.fitness[1]) pylab.scatter(x, y, color='r') fh.savePlot_png(opt_algorithm) pylab.show() # 3 and 4 dimensional plots if (cfg.ea.plot_results == True and (len_fitness == 3 or len_fitness == 4)): import warnings with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=FutureWarning) from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = [] y = [] if len_fitness == 3 or len_fitness == 4: z = [] if len_fitness == 4: c = [] for f in final_arc: x.append(f.fitness[0]) y.append(f.fitness[1]) if len(f.fitness) == 3 or len(f.fitness) == 4: z.append(f.fitness[2]) if len(f.fitness) == 4: c.append(f.fitness[3]) if len_fitness == 3: # log the best solutions in a csv file fh.save_best_solutions(final_arc,3) ax.scatter(x, y, z, c='r') if len_fitness == 4: # log the best solutions in a csv file fh.save_best_solutions(final_arc,4) ax.scatter(x, y, z, c=c, cmap=plt.hot()) fh.savePlot_png(opt_algorithm) plt.show() # for constrained_tournament_selection: create a second plot with feasible solutions if 'constrained_tournament_selection' in cfg.ea.selector: fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = [] y = [] if len_fitness == 3 or len_fitness == 4: z = [] if len_fitness == 4: c = [] for f in final_arc_feasible: x.append(f.fitness[0]) y.append(f.fitness[1]) if len(f.fitness) == 3 or len(f.fitness) == 4: z.append(f.fitness[2]) if len(f.fitness) == 4: c.append(f.fitness[3]) if len_fitness == 3: # log the best solutions in a csv file fh.save_best_solutions(final_arc_feasible,3) ax.scatter(x, y, z, c='r') if len_fitness == 4: # log the best solutions in a csv file fh.save_best_solutions(final_arc_feasible,4) ax.scatter(x, y, z, c=c, cmap=plt.hot()) fh.savePlot_png(opt_algorithm) plt.show() # print results without plotting (if plot_results was set to false) if cfg.ea.plot_results == False: if len_fitness == 2: # log the best solutions in a csv file fh.save_best_solutions(final_arc,2) if 'constrained_tournament_selection' in cfg.ea.selector: # log the best feasible solutions in a csv file fh.save_best_solutions(final_arc_feasible,2) if len_fitness == 3: # log the best solutions in a csv file fh.save_best_solutions(final_arc,3) if 'constrained_tournament_selection' in cfg.ea.selector: # log the best feasible solutions in a csv file fh.save_best_solutions(final_arc_feasible,3) if len_fitness == 4: # log the best solutions in a csv file fh.save_best_solutions(final_arc,4) if 'constrained_tournament_selection' in cfg.ea.selector: # log the best feasible solutions in a csv file fh.save_best_solutions(final_arc_feasible,4)
def GA(): """Starts the optimization with the GA algorithm.""" begin = time.time() # initialize random generator with system time rand = random.Random() rand.seed() # original start individual of the input data global start_individual # generate the original start individual from the input data # return it including the non static land use indices start_individual, nonstatic_elements = generate_genom(max_range, file_HRU,cfg.mapConfig.file_ASCII_map, cfg.mapConfig.file_transformation, cfg.mapConfig.file_ID_map, cfg.mapConfig.four_neighbours) if len(start_individual) == 0: msg = "Error: The generated start individual has no elements." WriteLogMsg(msg) raise SystemError("Error: The generated start individual has no elements.") close_window # determine that 'Bounder' conditions of candidates are equal to # the integer values of the non static land use indices bounder_discrete = nonstatic_elements # initialize inspyred log files stats_file,individ_file = fh.init_inspyred_logfiles() # initialize and run GA ea = ec.GA(rand) # public attributes # GA is predefined with rank_selection if cfg.ea.selector != 'rank_selection': exec ("%s%s" % ('ea.selector = ', fh.preparing_attribute('selector',cfg.ea.selector))) msg = 'Selector of the optimization algorithm changed to: %s' % cfg.ea.selector WriteLogMsg(msg) # GA is predefined with generational_replacement if cfg.ea.replacer != 'generational_replacement': exec ("%s%s" % ('ea.replacer = ', fh.preparing_attribute('replacer',cfg.ea.replacer))) msg = 'Replacer of the optimization algorithm changed to: %s' % cfg.ea.replacer WriteLogMsg(msg) # specify how the new candidates should be varied # GA is predefined with n_point_crossover,bit_flip_mutation as variators if cfg.ea.variator != 'n_point_crossover,bit_flip_mutation' and cfg.ea.variator != 'bit_flip_mutation,n_point_crossover': exec ("%s%s" % ('ea.variator = ', fh.preparing_attribute('variator',cfg.ea.variator))) msg = 'Variator of the optimization algorithm changed to: %s' % cfg.ea.variator WriteLogMsg(msg) # GA is predefined with num_selected = pop_size if cfg.ea.num_selected != cfg.ea.pop_size: msg = 'Num_selected of the optimization algorithm changed to: %s' % cfg.ea.num_selected WriteLogMsg(msg) exec ("%s%s" % ('ea.migrator = ', fh.preparing_attribute('migrator',cfg.ea.migrator))) exec ("%s%s" % ('ea.archiver = ', fh.preparing_attribute('archiver',cfg.ea.archiver))) if cfg.ea.archiver != 'best_archiver': msg = 'Archiver of the optimization algorithm changed to: %s' % cfg.ea.archiver WriteLogMsg(msg) exec ("%s%s" % ('ea.observer = ', fh.preparing_attribute('observer',cfg.ea.observer))) # specify when the optimization should terminate exec ("%s%s" % ('ea.terminator = ', fh.preparing_attribute('terminator',cfg.ea.terminator))) # run optimization, when finished final_pop holds the results final_pop = ea.evolve(generator = generate_parameter, # evaluate is the function to start external models # return results for the optimization algorithm evaluator = evaluate, # define population size pop_size = cfg.ea.pop_size, # maximize or minimize the problem maximize = cfg.ea.maximize, # bound the parameters to an interval # choose integer values between 1 and max_range in this case bounder = ec.DiscreteBounder(bounder_discrete), # minimum population diversity allowed (when using diversity_termination default 0.001) min_diversity = cfg.ea.min_diversity, # maximum number of evaluations (default pop_size) max_evaluations = cfg.ea.max_evaluations, # maximum number of generations max_generations = cfg.ea.max_generations, # number of elites to consider (default 0) num_elites = cfg.ea.num_elites, # number of individuals to be selected (default NSGA2 pop_size) num_selected = cfg.ea.num_selected, # tournament size (default NSGA2 2) tournament_size = cfg.ea.tournament_size, # the rate at which crossover is performed (default 1.0) crossover_rate = cfg.ea.crossover_rate, # mutation rate mutation_rate = cfg.ea.mutation_rate, # number of crossover points used (default 1) num_crossover_points = cfg.ea.num_crossover_points, # a positive integer representing the number of # closest solutions to consider as a “crowd” (default 2) crowding_distance = cfg.ea.crowding_distance, # statistic file statistics_file = stats_file, # individuals file individuals_file = individ_file) # read out the best individuals final_arc = ea.archive # for constrained_tournament_selection: # create a copy of final_arc only with feasible individuals (for csv file with best feasible solutions) if 'constrained_tournament_selection' in cfg.ea.selector: final_arc_feasible = [] end = time.time() WriteLogMsg("The optimization process needed %d seconds." %(end-begin)) msg = 'Best Solutions: \n' WriteLogMsg(msg) # save the map as ascii file in output folder f_count=1 for f in final_arc: # for constrained_tournament_selection: with information if individual is infeasible # and copy feasible solutions in final_arc_feasible if 'constrained_tournament_selection' in cfg.ea.selector: if individual_filter(f.candidate) == False: WriteLogMsg("(infeasible) %s" % f) # save the map as ascii file in output folder if file_HRU == 'None' or (file_HRU != 'None' and cfg.mapConfig.file_ID_map != 'None'): transform_individual_ascii_map(f.candidate,False,f_count,None,None,None,False) else: WriteLogMsg("%s" % f) # save the map as ascii file in output folder if file_HRU == 'None' or (file_HRU != 'None' and cfg.mapConfig.file_ID_map != 'None'): transform_individual_ascii_map(f.candidate,False,f_count) final_arc_feasible.append(f) else: WriteLogMsg("%s" % f) # save the map as ascii file in output folder if file_HRU == 'None' or (file_HRU != 'None' and cfg.mapConfig.file_ID_map != 'None'): transform_individual_ascii_map(f.candidate,False,f_count) f_count += 1 if cfg.ea.maximize == 'True': if 'constrained_tournament_selection' in cfg.ea.selector and individual_filter(f.candidate) == False: WriteLogMsg("\nFinal infeasible individual: %s, [%f]" % (max(final_pop).candidate,max(final_pop).fitness)) else: WriteLogMsg("\nFinal individual: %s, [%f]" % (max(final_pop).candidate,max(final_pop).fitness)) else: if 'constrained_tournament_selection' in cfg.ea.selector and individual_filter(f.candidate) == False: WriteLogMsg("\nFinal infeasible individual: %s, [%f]" % (min(final_pop).candidate,min(final_pop).fitness)) else: WriteLogMsg("\nFinal individual: %s, [%f]" % (min(final_pop).candidate,min(final_pop).fitness)) # save the map as ascii file in output folder # log the best solutions in a csv file fh.save_best_solutions(final_arc,1) # for constrained_tournament_selection: log the best feasible solutions in a csv file if 'constrained_tournament_selection' in cfg.ea.selector: fh.save_best_solutions(final_arc_feasible,1)
def __init___(self, dimensions=4): problem.__init__(self, dimensions, 2) self.bounder = ec.DiscreteBounder([0, 1]) self.maximze = True
def __init__(self, dimensions=43278): landtypes = {} invertedlandtypes = {} adj_List = {} indxMap = {} typeArea = defaultdict(float) roadDistance = {} attractiveness = {} b = {} areaMap = {} compatibility = defaultdict(float) conversion = defaultdict(float) landUseAllowed = 3 minA = defaultdict(float) # Minimum area for each land type (1-25) maxA = defaultdict(float) # Max area for each land type currentTypes = [] currentline = 0 f = open("sample", "r") data = f.read().splitlines() # First line species the number and land use types l = data[currentline].split() currentline = +1 n = int(l[0]) # Mapping for LandTypes for i in range(1, n + 1): landtypes[l[i]] = i - 1 invertedlandtypes[i - 1] = l[i] print landtypes # number of parcels parcels = int(data[currentline]) currentline = currentline + 1 # Adjacency List for i in range(0, parcels): line = data[currentline + i] neighbours = line.split() k = int(neighbours[0]) adj_List[k] = [] if (len(neighbours) > 1): for neighbour in range(1, len(neighbours)): j = int(neighbours[neighbour]) adj_List[k].append(j) currentline = currentline + parcels print adj_List parcels = int(data[currentline]) currentline = currentline + 1 # Land Use type Index for i in range(0, parcels): line = data[currentline + i] allowedTypes = line.split() k = int(allowedTypes[0]) indxMap[k] = [] currenttype = landtypes[allowedTypes[1]] currentTypes.append(currenttype) indxMap[k].append(currenttype) isallowed = int(allowedTypes[2]) indxMap[k].append(isallowed) if (len(allowedTypes) > 2): for allowedType in range(landUseAllowed, len(allowedTypes)): j = landtypes[allowedTypes[allowedType]] indxMap[k].append(j) currentline = currentline + parcels print indxMap n = int(data[currentline]) currentline = currentline + 1 # Land Use with min max areas and b for i in range(0, n): line = data[currentline + i] allowedTypes = line.split() currenttype = landtypes[allowedTypes[0]] typeArea[currenttype] = 0 minA[currenttype] = float(allowedTypes[1]) maxA[currenttype] = float(allowedTypes[2]) b[currenttype] = allowedTypes[2] currentline = currentline + n print minA print maxA parcels = int(data[currentline]) currentline = currentline + 1 #Current Area for i in range(0, parcels): line = data[currentline + i] allowedTypes = line.split() k = int(allowedTypes[0]) currenttype = landtypes[allowedTypes[1]] typeArea[currenttype] = typeArea[currenttype] + int( allowedTypes[2]) areaMap[i] = int(allowedTypes[2]) currentline = currentline + parcels print typeArea parcels = int(data[currentline]) currentline = currentline + 1 #Road Distance and Attractiveness for i in range(0, parcels): line = data[currentline + i] allowedTypes = line.split() pt = int(allowedTypes[0]) roadDistance[pt] = int(allowedTypes[1]) attractiveness[pt] = float(allowedTypes[2]) currentline = currentline + parcels #Compatibility Matrix for type1 in range(0, n): line = data[currentline + type1] compats = line.split() for type2 in range(0, n): compatibility[type1, type2] = compats[type2] currentline = currentline + n print compatibility #Conversion Matrix for type1 in range(0, n): line = data[currentline + type1] converts = line.split() for type2 in range(0, n): conversion[type1, type2] = converts[type2] currentline = currentline + n print conversion # A_LU=3 # B_LU=4 # C_LU=5 # D_LU=6 # E_LU=7 # cSUIT=11 # cNoChange=9 # #id2idx = [] # #idx2id = [] # id2idx = {} # idx2id = {} # id2idx2 = {} # idx2id2 ={} # minA = {} # maxA = {} # suit=[] # olt = {} # for i in range(50000): # suit.append(0) #olt.append(0) # olt2nlt = {} # olt2nlt["AIR"] = 1 # olt2nlt["CIV"] = 2 # olt2nlt["HIC"] = 3 # olt2nlt["LIC"] = olt2nlt["HCC"] = 4 # olt2nlt["LLRN"] = olt2nlt["ER"] = 5 # olt2nlt["MFRN"] = 6 # olt2nlt["MHP"] = 7 # olt2nlt["MRN"] = 8 # olt2nlt["MUN"] = 9 # olt2nlt["NCC"] = 10 # olt2nlt["POS"]= 11 # olt2nlt["RCR"]= 12 # olt2nlt["RL"] = 13 # olt2nlt["SCC"]= 14 # olt2nlt["SLRN"] = 15 # olt2nlt["SOC"] = 16 # olt2nlt["TC"] = 17 # olt2nlt["UC"] = 18 # olt2nlt["UN"] = 19 # olt2nlt["VC"] = olt2nlt["HRR"] = 20 # olt2nlt["WF"] = 21 # olt2nlt["MUC"] = 22 # olt2nlt["MC"] = 23 # olt2nlt["TOD"] = 24 #Loading adj list for each cell # f= open("Adj_List_Final.gal","r") # data = f.read().splitlines()[1:] # adj_List = {} # for line in data: # l = line.split(' ') # if not l[0] is '': # k = int(l[0]) # for i in range(len(l)): # #print i, k # j = int(l[i]) # if (i==0): # if not k in adj_List: # adj_List[k]=[] # else: # adj_List[k].append(j) # # if not j in adj_List: # adj_List[j] =[] # if not k in adj_List[j]: # adj_List[j].append(k) #print adj_List[20][1] #Loading areas, landtype for each cell # g=open("Refined_Swas_E_Updated2.csv","r") # dim=0 # area = {} # for i in range(0,25): # sumIndArea.append(0) # #print sumIndArea[21] # datag = g.readlines()[1:] # indxMap = {} # areaMap = {} # landType = [] # for line in datag: # line = line.split('\t') # pt = int(line[0]) # ar = float(line[2]) #area # tee = line[1] # landtype # temp_lt = olt2nlt[tee] # landType.append(temp_lt) # # indxMap : contains decision (change/nochange), and four preferred land types # indxMap[pt] = (int(line[cNoChange]),olt2nlt[line[B_LU]] ,olt2nlt[line[C_LU]],olt2nlt[line[D_LU]],olt2nlt[line[E_LU]]) # areaMap[pt] = ar # #print temp_lt # sumIndArea[temp_lt] += ar #print indxMap[20] sumIndArea = defaultdict(float) sumIndArea = typeArea landType = currentTypes copyLandType = landType copySumIndArea = sumIndArea area_sum = 0.00 for i in range(43278): if not i in areaMap: areaMap[i] = 0 for pt in areaMap.keys(): area_sum = area_sum + areaMap[pt] # Loading area targets # gh = open("SWAS_E_LU_PERCENT.csv","r") # datagh = gh.readlines() # for line in datagh: # line = line.split('\t') # #print line[0], line[1], line[2] # minA[int(line[0])] = float(line[1]) * area_sum # maxA[int(line[0])] = float(line[2]) * area_sum #print line[0], minA[int(line[0])], maxA[int(line[0])], area_sum #43151 self.dimensions = parcels self.objectives = 25 Benchmark.__init__(self, parcels, 25) #numobj=3, dimensions=numcells=6 #self.bounder = ec.Bounder([0] * self.dimensions, [3.0] * self.dimensions) self.bounder = ec.DiscreteBounder([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ]) self.maximize = True #self.candidatesArea = {0:10,1:10,2:10,3:10,4:10,5:10} #self.total_area = 60 #self.adjList = {0:[1,2],1:[0,2],2:[0,1,3],3:[2,4,5],4:[3,5],5:[3,4]} self.adjList = adj_List # self.candidatesArea = area self.landType = landType self.copyLandType = copyLandType self.areaMap = areaMap self.indxMap = indxMap self.sumIndArea = sumIndArea self.copySumIndArea = copySumIndArea self.total_area = area_sum self.minA = minA self.maxA = maxA
def __init___(self): problem.__init__(self, 2) self.bounder = ec.DiscreteBounder([0, 1]) self.maximze = True