def scores2(individual, stars, configurations): """ Used to score points :param individual: jmoo_individual :param stars: Star formation for STORM :param configurations: Configuration from jmoo_properties :return: list of N(determined by STORM_SPLIT) best poles (numbers), individual """ individual.anyscore = -1e30 nearest_contours = [] for count, star in enumerate(stars): try: a = euclidean_distance(individual.decisionValues, star.east.decisionValues) b = euclidean_distance(individual.decisionValues, star.west.decisionValues) c = euclidean_distance(star.east.decisionValues, star.west.decisionValues) x = (a**2 + c**2 - b**2) / (2 * c) y = (a**2 - x**2)**0.5 # Difference in objective values diff = euclidean_distance(star.east.fitness.fitness, star.west.fitness.fitness) temp_score = ((b / a) * diff / (y**2) * (1 / c)) except (ZeroDivisionError, ValueError), e: # Assumption for ZeroDivisionError: The cases where the code would be when a=0 (individual is east) # | y=0 (on the line) | c=0 east and west are the same point. If these things happen then we assign # a really high score to this individual. # Assumption for ValueError: The above formula works under the assumption that a, b < c, but it might not be # the case. For such cases we assign a high score. temp_score = 1e32 nearest_contours.append([count, temp_score])
def scores2(individual, stars, configurations): """ Used to score points :param individual: jmoo_individual :param stars: Star formation for STORM :param configurations: Configuration from jmoo_properties :return: list of N(determined by STORM_SPLIT) best poles (numbers), individual """ individual.anyscore = -1e30 nearest_contours = [] for count, star in enumerate(stars): try: a = euclidean_distance(individual.decisionValues, star.east.decisionValues) b = euclidean_distance(individual.decisionValues, star.west.decisionValues) c = euclidean_distance(star.east.decisionValues, star.west.decisionValues) x = (a ** 2 + c ** 2 - b ** 2) / (2 * c) y = (a ** 2 - x ** 2) ** 0.5 # Difference in objective values diff = euclidean_distance(star.east.fitness.fitness, star.west.fitness.fitness) temp_score = (b / a) * diff / (y ** 2) * (1 / c) except (ZeroDivisionError, ValueError), e: # Assumption for ZeroDivisionError: The cases where the code would be when a=0 (individual is east) # | y=0 (on the line) | c=0 east and west are the same point. If these things happen then we assign # a really high score to this individual. # Assumption for ValueError: The above formula works under the assumption that a, b < c, but it might not be # the case. For such cases we assign a high score. temp_score = 1e32 nearest_contours.append([count, temp_score])
def get_extreme_points(self, number_of_generations): """This method should be used to find the extreme points of particular generation across all the algorithms""" from Techniques.flatten_list import flatten points = [] points.extend(flatten([d.get_frontiers_collection(number_of_generations) for d in self.data])) print len(points) objectives = [point.objectives for point in points] maps_objectives = [[-1 for _ in objectives] for _ in objectives] from Techniques.euclidean_distance import euclidean_distance for i, ii in enumerate(objectives): for j, jj in enumerate(objectives): if maps_objectives[i][j] == -1: maps_objectives[i][j] = euclidean_distance(ii, jj) maps_objectives[j][i] = euclidean_distance(ii, jj) elif i == j: maps_objectives[i][j] = 0 print maps_objectives max_distance = max([max(maps_objective) for maps_objective in maps_objectives]) indexes = [[(i, j) for j, distance in enumerate(distances) if distance == max_distance] for i, distances in enumerate(maps_objectives)] index = [index for index in indexes if len(index) > 0][-1][-1] # Hack: To handle list of lists # indexes should always be a multiple of 2. And if there more than 2 entries in indexes just use any one. return objectives[index[0]], objectives[index[1]]
def scores(individual, stars): cols = len(stars[0].east.fitness.fitness) temp = -1e32 selected = -1 individual.anyscore = -1e30 for count, star in enumerate(stars): try: a = euclidean_distance(individual.decisionValues, star.east.decisionValues) b = euclidean_distance(individual.decisionValues, star.west.decisionValues) c = euclidean_distance(star.east.decisionValues, star.west.decisionValues) x = (a**2 + c**2 - b**2) / (2 * c) y = (a**2 - x**2)**0.5 r = len(stars) - 1 # Number of poles - midpoint diff = euclidean_distance(star.east.fitness.fitness, star.west.fitness.fitness) temp_score = ((b / a) * diff / (y**2 * c)) except: temp_score = 1e32 if temp < temp_score: temp = temp_score selected = count assert (selected > -1), "Something's wrong" individual.anyscore = temp return selected, individual
def scores(individual, stars): cols = len(stars[0].east.fitness.fitness) temp = -1e32 selected = -1 individual.anyscore = -1e30 for count, star in enumerate(stars): try: a = euclidean_distance(individual.decisionValues, star.east.decisionValues) b = euclidean_distance(individual.decisionValues, star.west.decisionValues) c = euclidean_distance(star.east.decisionValues, star.west.decisionValues) x = (a ** 2 + c ** 2 - b ** 2) / (2 * c) y = (a ** 2 - x ** 2) ** 0.5 r = len(stars) - 1 # Number of poles - midpoint diff = euclidean_distance(star.east.fitness.fitness, star.west.fitness.fitness) temp_score = (b / a) * diff / (y ** 2 * c) except: temp_score = 1e32 if temp < temp_score: temp = temp_score selected = count assert selected > -1, "Something's wrong" individual.anyscore = temp return selected, individual
def get_extreme_points(self, number_of_generations): """This method should be used to find the extreme points of particular generation across all the algorithms""" from Techniques.flatten_list import flatten points = [] points.extend( flatten([ d.get_frontiers_collection(number_of_generations) for d in self.data ])) print len(points) objectives = [point.objectives for point in points] maps_objectives = [[-1 for _ in objectives] for _ in objectives] from Techniques.euclidean_distance import euclidean_distance for i, ii in enumerate(objectives): for j, jj in enumerate(objectives): if maps_objectives[i][j] == -1: maps_objectives[i][j] = euclidean_distance(ii, jj) maps_objectives[j][i] = euclidean_distance(ii, jj) elif i == j: maps_objectives[i][j] = 0 print maps_objectives max_distance = max( [max(maps_objective) for maps_objective in maps_objectives]) indexes = [[(i, j) for j, distance in enumerate(distances) if distance == max_distance] for i, distances in enumerate(maps_objectives)] index = [index for index in indexes if len(index) > 0][-1][-1] # Hack: To handle list of lists # indexes should always be a multiple of 2. And if there more than 2 entries in indexes just use any one. return objectives[index[0]], objectives[index[1]]
def find_extreme_points(points): from Techniques.euclidean_distance import euclidean_distance random_point = choice(points) distances = [euclidean_distance(point, random_point) for point in points] first_point = points[distances.index(max(distances))] distances = [euclidean_distance(point, first_point) for point in points] second_point = points[distances.index(max(distances))] return [first_point, second_point]
def find_extreme(one, population): temp = [] for individual in population: temp_distance = euclidean_distance(one.decisionValues, individual.decisionValues), individual assert temp_distance > 1, "Something's wrong" temp.append([temp_distance, individual]) return sorted(temp, key=lambda x: x[0], reverse=True)[0][1]
def find_extreme(one, population): temp = [] for individual in population: temp_distance = euclidean_distance( one.decisionValues, individual.decisionValues), individual assert (temp_distance > 1), "Something's wrong" temp.append([temp_distance, individual]) return sorted(temp, key=lambda x: x[0], reverse=True)[0][1]
def generate_directions(problem): def gen(point): r = sum([pp**2 for pp in point])**0.5 return [round(p/r,3) for p in point] coordinates = [gen([random.uniform(0, 1) for _ in xrange(len(problem.decisions))]) for _ in xrange(jmoo_properties.ANYWHERE_POLES * 2)] for co in coordinates: assert(int(round(euclidean_distance(co, [0 for _ in xrange(len(problem.decisions))]))) == 1), "Something's wrong" return coordinates
def create_distance_matrix(population): weights = [pop.weight for pop in population] distance_matrix = [[[0, i] for i, _ in enumerate(xrange(len(weights)))] for _ in xrange(len(weights))] for i in xrange(len(weights)): for j in xrange(len(weights)): distance_matrix[i][j][0] = euclidean_distance(weights[i], weights[j]) assert(distance_matrix[i][i][0] == 0), "Diagonal of Distance matrix should be 0" return distance_matrix
def IGD(approximation_points, original_points): summ = 0 for o in original_points: min_distance = 1e32 for a in approximation_points: min_distance = min(min_distance, euclidean_distance(o, a)) summ += min_distance return summ / len(original_points)
def find_poles2(problem, population): def midpoint(population): def median(lst): import numpy return numpy.median(numpy.array(lst)) mdpnt = [] for dec in xrange(len(population[0].decisionValues)): mdpnt.append( median([pop.decisionValues[dec] for pop in population])) assert (len(mdpnt) == len( population[0].decisionValues)), "Something's wrong" # print mdpnt return jmoo_individual(problem, mdpnt, None) def generate_directions(problem): def gen(point): r = sum([pp**2 for pp in point])**0.5 return [round(p / r, 3) for p in point] coordinates = [ gen([random.uniform(0, 1) for _ in xrange(len(problem.decisions))]) for _ in xrange(jmoo_properties.ANYWHERE_POLES * 2) ] for co in coordinates: assert (int( round( euclidean_distance( co, [0 for _ in xrange(len(problem.decisions)) ]))) == 1), "Something's wrong" return coordinates # find midpoint mid_point = midpoint(population) # find directions directions = generate_directions(problem) # draw a star poles = [] for direction in directions: mine = -1e32 temp_pole = None for pop in population: transformed_dec = [ (p - m) for p, m in zip(pop.decisionValues, mid_point.decisionValues) ] y = perpendicular_distance(direction, transformed_dec) c = euclidean_distance(transformed_dec, [0 for _ in xrange(len(problem.decisions))]) # print c, y if mine < (c - y): mine = c - y temp_pole = pop poles.append(temp_pole) stars = rearrange(problem, mid_point, poles) return stars
def IGD(approximation_points, original_points): summ = 0 for o in original_points: min_distance = 1e32 for a in approximation_points: min_distance = min(min_distance, euclidean_distance(o, a)) summ += min_distance return summ/len(original_points)
def create_distance_matrix(population): weights = [pop.weight for pop in population] distance_matrix = [[[0, i] for i, _ in enumerate(xrange(len(weights)))] for _ in xrange(len(weights))] for i in xrange(len(weights)): for j in xrange(len(weights)): distance_matrix[i][j][0] = euclidean_distance( weights[i], weights[j]) assert (distance_matrix[i][i][0] == 0 ), "Diagonal of Distance matrix should be 0" return distance_matrix
def furthest(one, all_members): ret = None ret_distance = -1 * 1e10 for member in all_members: if equal_list(one, member) is True: continue else: temp = euclidean_distance(one, member) if temp > ret_distance: ret = member ret_distance = temp return ret
def furthest(one, all_members): """Find the distant point (from the population) from one (point)""" ret = None ret_distance = -1 * 1e10 for member in all_members: if equal_list(one, member) is True: continue else: temp = euclidean_distance(one, member) if temp > ret_distance: ret = member ret_distance = temp return ret
def find_poles2(problem, population): def midpoint(population): def median(lst): import numpy return numpy.median(numpy.array(lst)) mdpnt = [] for dec in xrange(len(population[0].decisionValues)): mdpnt.append(median([pop.decisionValues[dec] for pop in population])) assert len(mdpnt) == len(population[0].decisionValues), "Something's wrong" # print mdpnt return jmoo_individual(problem, mdpnt, None) def generate_directions(problem): def gen(point): r = sum([pp ** 2 for pp in point]) ** 0.5 return [round(p / r, 3) for p in point] coordinates = [ gen([random.uniform(0, 1) for _ in xrange(len(problem.decisions))]) for _ in xrange(jmoo_properties.ANYWHERE_POLES * 2) ] for co in coordinates: assert ( int(round(euclidean_distance(co, [0 for _ in xrange(len(problem.decisions))]))) == 1 ), "Something's wrong" return coordinates # find midpoint mid_point = midpoint(population) # find directions directions = generate_directions(problem) # draw a star poles = [] for direction in directions: mine = -1e32 temp_pole = None for pop in population: transformed_dec = [(p - m) for p, m in zip(pop.decisionValues, mid_point.decisionValues)] y = perpendicular_distance(direction, transformed_dec) c = euclidean_distance(transformed_dec, [0 for _ in xrange(len(problem.decisions))]) # print c, y if mine < (c - y): mine = c - y temp_pole = pop poles.append(temp_pole) stars = rearrange(problem, mid_point, poles) return stars
def generate_directions(problem): def gen(point): r = sum([pp**2 for pp in point])**0.5 return [round(p / r, 3) for p in point] coordinates = [ gen([random.uniform(0, 1) for _ in xrange(len(problem.decisions))]) for _ in xrange(jmoo_properties.ANYWHERE_POLES * 2) ] for co in coordinates: assert (int( round( euclidean_distance( co, [0 for _ in xrange(len(problem.decisions)) ]))) == 1), "Something's wrong" return coordinates
def find_extreme_points(problem, points): from Techniques.euclidean_distance import euclidean_distance distance_matrix = [[-1 for _ in xrange(len(points))] for _ in xrange(len(points))] for i in xrange(len(points)): for j in xrange(len(points)): if distance_matrix[i][j] == -1: temp_dist = euclidean_distance(points[i], points[j]) distance_matrix[i][j] = temp_dist distance_matrix[j][i] = temp_dist max_distance = max( [max(maps_objective) for maps_objective in distance_matrix]) indexes = [[(i, j) for j, distance in enumerate(distances) if distance == max_distance] for i, distances in enumerate(distance_matrix)] index = [index for index in indexes if len(index) > 0][-1][-1] return points[index[0]], points[index[1]]
def gamma_kernel(x, y, sigma=1): from Techniques.euclidean_distance import euclidean_distance from math import exp return exp(-1 * (euclidean_distance(x, y)** 2)/ (2 * (sigma ** 2)))
def generate_distance_matrix(points): from Techniques.euclidean_distance import euclidean_distance return [[euclidean_distance(points[i], points[j]) for j in xrange(len(points))] for i in xrange(len(points))]
def furthest(individual, population): from Techniques.euclidean_distance import euclidean_distance distances = sorted([[euclidean_distance(individual, pop), pop] for pop in population], key=lambda x: x[0], reverse=True) return distances[0][-1]
def new_fastmap(problem, true_population): """ Fastmap function that projects all the points on the principal component :param problem: Instance of the problem :param population: Set of points in the cluster population :return: """ def list_equality(lista, listb): for a, b in zip(lista, listb): if a != b: return False return True from random import choice from Techniques.euclidean_distance import euclidean_distance decision_population = [pop.decisionValues for pop in true_population] one = choice(decision_population) west = furthest(one, decision_population) east = furthest(west, decision_population) west_indi = jmoo_individual(problem,west, None) east_indi = jmoo_individual(problem,east, None) west_indi.evaluate() east_indi.evaluate() # Score the poles n = len(problem.decisions) weights = [] for obj in problem.objectives: # w is negative when we are maximizing that objective if obj.lismore: weights.append(+1) else: weights.append(-1) weightedWest = [c * w for c, w in zip(west_indi.fitness.fitness, weights)] weightedEast = [c * w for c, w in zip(east_indi.fitness.fitness, weights)] westLoss = loss(weightedWest, weightedEast, mins=[obj.low for obj in problem.objectives], maxs=[obj.up for obj in problem.objectives]) eastLoss = loss(weightedEast, weightedWest, mins=[obj.low for obj in problem.objectives], maxs=[obj.up for obj in problem.objectives]) # Determine better Pole if eastLoss < westLoss: SouthPole, NorthPole = east_indi, west_indi else: SouthPole, NorthPole = west_indi, east_indi east = SouthPole.decisionValues west = NorthPole.decisionValues c = euclidean_distance(east, west) tpopulation = [] for one in decision_population: a = euclidean_distance(one, west) b = euclidean_distance(one, east) tpopulation.append([one, projection(a, b, c)]) for tpop in tpopulation: for true_pop in true_population: if list_equality(tpop[0], true_pop.decisionValues): true_pop.x = tpop[-1] for i, true_pop in enumerate(true_population): if list_equality(west, true_pop.decisionValues): true_pop.fitness = west_indi.fitness elif list_equality(east, true_pop.decisionValues): true_pop.fitness = east_indi.fitness temp_list = sorted(true_population, key=lambda pop: pop.x) ranklist = [t.id for t in temp_list] return true_population, ranklist, temp_list[0], temp_list[-1]
def gamma_kernel(x, y, sigma=1): from Techniques.euclidean_distance import euclidean_distance from math import exp return exp(-1 * (euclidean_distance(x, y)**2) / (2 * (sigma**2)))
def generate_distance_matrix(points): from Techniques.euclidean_distance import euclidean_distance return [[ euclidean_distance(points[i], points[j]) for j in xrange(len(points)) ] for i in xrange(len(points))]