Ejemplo n.º 1
0
    def __init__(self,
                 length=None,
                 fitness_fn=None,
                 maximize=True,
                 crossover=None,
                 mutator=None):

        if (fitness_fn is None) and (length is None):
            raise Exception("fitness_fn or length must be specified.")

        if length is None:
            length = len(fitness_fn.weights)

        self.length = length

        if fitness_fn is None:
            fitness_fn = FlipFlop()

        self.max_val = 2
        crossover = OnePointCrossOver(self) if crossover is None else crossover
        mutator = ChangeOneMutator(self) if mutator is None else mutator
        super().__init__(length, fitness_fn, maximize, 2, crossover, mutator)

        state = np.random.randint(2, size=self.length)
        self.set_state(state)
Ejemplo n.º 2
0
    def __init__(self, length=None, fitness_fn=None, maximize=True, max_val=2,
                 weights=None, values=None, max_weight_pct=0.35,
                 crossover=None, mutator=None,
                 multiply_by_max_item_count=False):

        if (fitness_fn is None) and (weights is None and values is None):
            raise Exception("""fitness_fn or both weights and"""
                            + """ values must be specified.""")

        if length is None:
            if weights is not None:
                length = len(weights)
            elif values is not None:
                length = len(values)
            elif fitness_fn is not None:
                length = len(fitness_fn.weights)

        self.length = length

        if fitness_fn is None:
            fitness_fn = Knapsack(weights=weights, values=values,
                                  max_weight_pct=max_weight_pct,
                                  max_item_count=max_val,
                                  multiply_by_max_item_count=multiply_by_max_item_count)

        self.max_val = max_val
        crossover = UniformCrossOver(self) if crossover is None else crossover
        mutator = ChangeOneMutator(self) if mutator is None else mutator
        super().__init__(length, fitness_fn, maximize, max_val, crossover, mutator)
Ejemplo n.º 3
0
    def __init__(self,
                 length=None,
                 fitness_fn=None,
                 maximize=False,
                 crossover=None,
                 mutator=None):

        if (fitness_fn is None) and (length is None):
            raise Exception("fitness_fn or length must be specified.")

        if length is None:
            length = len(fitness_fn.weights)

        self.length = length

        if fitness_fn is None:
            fitness_fn = Queens()

        self.max_val = length
        crossover = UniformCrossOver(self) if crossover is None else crossover
        mutator = ChangeOneMutator(self) if mutator is None else mutator
        super().__init__(length, fitness_fn, maximize, length, crossover,
                         mutator)

        state = np.random.randint(self.length, size=self.length)
        np.random.shuffle(state)
        self.set_state(state)
Ejemplo n.º 4
0
    def __init__(self,
                 edges=None,
                 length=None,
                 fitness_fn=None,
                 maximize=False,
                 max_colors=None,
                 crossover=None,
                 mutator=None):

        if (fitness_fn is None) and (edges is None):
            raise Exception("fitness_fn or edges must be specified.")

        if length is None:
            if fitness_fn is None:
                length = len(edges)
            else:
                length = len(fitness_fn.weights)

        self.length = length

        if fitness_fn is None:
            fitness_fn = MaxKColor(edges)

        # set up initial state (everything painted one color)
        g = nx.Graph()
        g.add_edges_from(edges)
        fitness_fn.set_graph(g)

        # if none is provided, make a reasonable starting guess.
        # the max val is going to be the one plus the maximum number of neighbors of any one node.
        if max_colors is None:
            max_colors = 1 + max(
                [len([*g.neighbors(n)]) for n in range(length)])
        self.max_val = max_colors

        crossover = UniformCrossOver(self) if crossover is None else crossover
        mutator = ChangeOneMutator(self) if mutator is None else mutator
        super().__init__(length, fitness_fn, maximize, max_colors, crossover,
                         mutator)

        # state = [len([*g.neighbors(n)]) for n in range(length)]
        state = np.random.randint(max_colors, size=self.length)
        np.random.shuffle(state)
        # state = [0] * length
        self.set_state(state)