Exemplo n.º 1
0
def get_fitness_function(problem_type):
    fitness, problem = None, None

    if problem_type == Problem.KNAPSACK:
        fitness = mlrose.Knapsack(weights=[
            70, 73, 77, 80, 82, 87, 90, 94, 98, 106, 110, 113, 115, 118, 120
        ],
                                  values=[
                                      1.35, 1.39, 1.49, 1.50, 1.56, 1.63, 1.73,
                                      1.84, 1.92, 2.01, 2.10, 2.14, 2.21, 2.29,
                                      2.40
                                  ],
                                  max_weight_pct=0.52)
        problem = mlrose.DiscreteOpt(length=15,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)

    elif problem_type == Problem.NQUEEN:
        fitness = mlrose.Queens()
        problem = mlrose.DiscreteOpt(length=8,
                                     fitness_fn=fitness,
                                     maximize=False,
                                     max_val=8)

    elif problem_type == Problem.FOUR_PEAKS:
        fitness = mlrose.FourPeaks(t_pct=0.15)
        problem = mlrose.DiscreteOpt(length=100,
                                     fitness_fn=fitness,
                                     maximize=True,
                                     max_val=2)

    return fitness, problem
Exemplo n.º 2
0
def queens_problem(n=8):
    queens_max = lambda state: sum(np.arange(len(state))) - mlrose.Queens(
    ).evaluate(state)
    fitness_queens = mlrose.CustomFitness(queens_max)
    return mlrose.DiscreteOpt(length=n,
                              fitness_fn=fitness_queens,
                              maximize=True,
                              max_val=n)
 def init_optis(self, K=False):
     self.Q_rand_state = np.random.randint(0, self.N_Q + 1, self.N_Q)
     self.fitness_Q = mlrose_hiive.Queens()
     self.P_rand_state = np.random.randint(0, 2, self.N_P)
     self.fitness_P = mlrose_hiive.FourPeaks(t_pct=0.15)
     self.eval_cnt = 1
     if K:
         base_edges = [(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (2, 5),
                       (3, 4), (4, 5)]  #Australia
         edges = []
         for au_idx in range(N_K):
             for edge in base_edges:
                 edge = (edge[0] + 5 * au_idx, edge[1] + 5 * au_idx)
                 edges.append(edge)
         self.K_rand_state = np.random.randint(0, 4, max(max(edges)) + 1)
         self.fitness_K = mlrose_hiive.MaxKColor(edges)
Exemplo n.º 4
0
    def optimize(self):
        problem_size_space = self.problem_size

        # Initializing the problem
        init_state = [i for i in range(problem_size_space)]
        fitness = mlrose.Queens()
        problem = mlrose.DiscreteOpt(length=problem_size_space,
                                     fitness_fn=fitness,
                                     maximize=False,
                                     max_val=problem_size_space)
        # SA
        # super().gridSearchSA(problem,'NQueens',problem_size_space,self.noOfiteration)
        # RHC
        # super().gridSearchRHC(problem,'NQueens',problem_size_space,self.noOfiteration)
        #GA
        # super().gridSearchGA(problem,'NQueens',problem_size_space,self.noOfiteration)
        #MIMIC
        super().gridSearchMIMIC(problem, 'NQueens', problem_size_space,
                                self.noOfiteration)
        edge = (edge[0] + 5 * au_idx, edge[1] + 5 * au_idx)
        edges.append(edge)

fitness_k = mlrose_hiive.MaxKColor(edges)
# init_state = np.array([0, 1, 0, 1, 1])
K_problem = mlrose_hiive.DiscreteOpt(length=max(max(edges)) + 1,
                                     fitness_fn=fitness_k,
                                     maximize=False,
                                     max_val=3)
fitness_k.evaluate(K_problem.state)

# Define decay schedule (sim annealing only)
schedule = mlrose_hiive.ExpDecay()

#8-Queens
fitness_Q = mlrose_hiive.Queens()
N_Q = 8
#OPti_object
# Q_problem = mlrose_hiive.DiscreteOpt(length = 8, fitness_fn = fitness_Q, maximize=False, max_val=8)

# timeit.timeit(fitness_Q.evaluate(Q_rand_state), number=100000)


class var_input_size():
    def __init__(self, N_Q=8, N_P=7, N_K=1):
        self.N_Q = N_Q
        self.N_K = N_K
        self.N_P = N_P

    def init_optis(self, K=False):
        self.Q_rand_state = np.random.randint(0, self.N_Q + 1, self.N_Q)
Exemplo n.º 6
0
def runPart1(savePath):
    fitness = mlrose.FourPeaks(t_pct=0.15)
    init_state = None
    fourPeaksProblem = mlrose.DiscreteOpt(length=12,
        fitness_fn=fitness, maximize=True, max_val=2)

    part1_1 = Part1(name='Four Peaks', fitness=fitness,
                    problem=fourPeaksProblem, init_state=init_state)
    part1_1.runAll(savePath)

    fitness = mlrose.Queens()
    init_state = None
    eightQueensProblem = mlrose.DiscreteOpt(length=8,
        fitness_fn=fitness, maximize=False, max_val=8)
    part1_2 = Part1(name='Eight Queens', fitness=fitness,
                    problem=eightQueensProblem, init_state=init_state)
    part1_2.runAll(savePath)

    fitness = mlrose.SixPeaks(t_pct=0.15)
    init_state = None
    sixPeaksProblem = mlrose.DiscreteOpt(length=11,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_4 = Part1(name='Six Peaks', fitness=fitness,
                    problem=sixPeaksProblem, init_state=init_state)
    part1_4.runAll(savePath)

    fitness = mlrose.FlipFlop()
    init_state = None
    flipFlopProblem = mlrose.DiscreteOpt(length=7,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_5 = Part1(name='Flip Flop - 7', fitness=fitness,
                    problem=flipFlopProblem, init_state=init_state)
    part1_5.runAll(savePath)


    fitness = mlrose.FlipFlop()
    init_state = None
    flipFlopProblem = mlrose.DiscreteOpt(length=100,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_5 = Part1(name='Flip Flop - 100', fitness=fitness,
                    problem=flipFlopProblem, init_state=init_state)
    part1_5.runAll(savePath)

    fitness = mlrose.Queens()
    init_state = None
    eightQueensProblem = mlrose.DiscreteOpt(length=80,
        fitness_fn=fitness, maximize=False, max_val=8)
    part1_2 = Part1(name='Eighty Queens', fitness=fitness,
                    problem=eightQueensProblem, init_state=init_state)
    part1_2.runAll(savePath)

    fitness = mlrose.FlipFlop()
    init_state = None
    flipFlopProblem = mlrose.DiscreteOpt(length=15,
        fitness_fn=fitness, maximize=True, max_val=2)
    part1_5 = Part1(name='Flip Flop - 15', fitness=fitness,
                    problem=flipFlopProblem, init_state=init_state)
    part1_5.runAll(savePath)


    edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
    fitness = mlrose.MaxKColor(edges)
    init_state = None
    maxKColorsProblem = mlrose.DiscreteOpt(length=7,
        fitness_fn=fitness, maximize=False, max_val=2)
    part1_3 = Part1(name='Max-K Color', fitness=fitness,
                    problem=maxKColorsProblem, init_state=init_state)
    part1_3.runAll(savePath)

    # =============================================================
    #  Source - Tutorial from MLRose Docs
    #  https://mlrose.readthedocs.io/en/stable/source/tutorial2.html
    # 
    # =============================================================
    # Create list of city coordinates
    coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]

    # Initialize fitness function object using coords_list
    fitness_coords = mlrose.TravellingSales(coords = coords_list)

    # Create list of distances between pairs of cities
    dist_list = [(0, 1, 3.1623), (0, 2, 4.1231), (0, 3, 5.8310), (0, 4, 4.2426), \
                (0, 5, 5.3852), (0, 6, 4.0000), (0, 7, 2.2361), (1, 2, 1.0000), \
                (1, 3, 2.8284), (1, 4, 2.0000), (1, 5, 4.1231), (1, 6, 4.2426), \
                (1, 7, 2.2361), (2, 3, 2.2361), (2, 4, 2.2361), (2, 5, 4.4721), \
                (2, 6, 5.0000), (2, 7, 3.1623), (3, 4, 2.0000), (3, 5, 3.6056), \
                (3, 6, 5.0990), (3, 7, 4.1231), (4, 5, 2.2361), (4, 6, 3.1623), \
                (4, 7, 2.2361), (5, 6, 2.2361), (5, 7, 3.1623), (6, 7, 2.2361)]

    # Initialize fitness function object using dist_list
    fitness_dists = mlrose.TravellingSales(distances = dist_list)

    # Define optimization problem object
    problem_fit = mlrose.TSPOpt(length = 8, fitness_fn = fitness_coords, maximize=False)

    coords_list = [(1, 1), (4, 2), (5, 2), (6, 4), (4, 4), (3, 6), (1, 5), (2, 3)]

    # Define optimization problem object
    problem_no_fit = mlrose.TSPOpt(length = 8, coords = coords_list, maximize=False)

    part1_6 = Part1(name='TSP', fitness=coords_list,
                    problem=problem_no_fit, init_state=None)
    part1_6.runAll(savePath)

    # Knapsack
    weights = np.random.randint(2, high=20, size=50)
    values = np.random.randint(2, high=100, size=50)
    max_weight_pct = 0.8
    fitness = mlrose.Knapsack(weights, values, max_weight_pct)
    knapsackProblem = mlrose.DiscreteOpt(length=50,
        fitness_fn=fitness, maximize=False, max_val=2)

    part1_7 = Part1(name='Knapsack', fitness=fitness,
                    problem=knapsackProblem, init_state=None)
    part1_7.runAll(savePath)
Exemplo n.º 7
0
# Andrew Nowotarski
# anowotarski3
# CS 7641 ML Spring 2020
# Assignment 2: Randomized Optimization

# Packages for matrix manipulation. 
import mlrose_hiive as mlrose
import numpy as np

# Packages for metrics and plotting.
import plotting

# Instantiate a Queens problem.
fitness = mlrose.Queens()
problem_fit = mlrose.DiscreteOpt(length = 15, fitness_fn= fitness)

# Solve the problem with genetic algorithm
plotting.plot_optimization_problem_fitness(problem_fit, 100, 2, 'N-Queens')

# CODE SOURCED FROM
# https://mlrose.readthedocs.io/en/stable/source/tutorial1.html

# Define alternative N-Queens fitness function for maximization problem.
def queens_max(state):

    # Initialize counter
    fitness_cnt = 0

    # For all pairs of queens
    for i in range(len(state) - 1):
        for j in range(i + 1, len(state)):
Exemplo n.º 8
0
def queens(length=10):
    return mlrose.Queens(), length * (length - 1) / 2