Пример #1
0
def direct_random(T, r, iterations, verbose=False):
    """
    Direct random algorithm.
    
    Arguments:
    T -- the constraint problem
    r -- range [-r, r] of assignments to consider
    iterations -- number of maximum random iterations
    """
    
    best_gene = None
    best_gene_failed = None
    
    for i in range(iterations):
        gene = random_gene(T, r)
        gene_failed = verify_witness(gene, T)
        if not best_gene or len(gene_failed) < len(best_gene_failed):
            best_gene = gene
            best_gene_failed = gene_failed
        
        if len(gene_failed) == 0:
            break
            
    if verbose:
        print(f'num iterations: {i+1}')
        print('best gene:', best_gene)
        print('constraints failed:', len(best_gene_failed), 'out of:', len(T)) 
        print('failed constraints:', best_gene_failed)
    
    return best_gene, best_gene_failed
Пример #2
0
def direct_genetic(T, r,
            gene_pool_size,
            retainment_ratio,
            mutation_chance,
            max_iterations,
            verbose=False):
    
    """
    Direct genetic algorithm.
    
    Arguments:
    T -- the constraint problem
    r -- range [-r, r] of assignments to consider
    gene_pool_size -- number of genes kept in the pool
    retainment_ratio -- probability of keeping a gene from one iteration to the other
    mutation_chance -- how likely a gene is to be walked
    max_iterations -- maximum number of iterations before failure
    """
    
    genes = [random_gene(T, r) for i in range(gene_pool_size)]
    
    for it in range(max_iterations):
        genes = select(genes, retainment_ratio, T)
        genes = crossover(genes, gene_pool_size)
        genes = mutate(T, genes, mutation_chance, r)
    
    best_gene = select(genes, 1, T)[0] # to sort genes such that first is best
    best_gene_failed = verify_witness(best_gene, T)
    
    if verbose:
        print('best gene:', best_gene)
        print('constraints failed:', len(best_gene_failed), 'out of:', len(T)) 
        print('failed constraints:', best_gene_failed)

    return best_gene, best_gene_failed
Пример #3
0
def evaluate(genes, T):
    """
    For each gene, compute the failed constraints.
    """

    for g in genes:
        d_graph = generate_d_graph(g[1])  # costly op.
        g[2] = verify_witness(get_middles_sol(d_graph), T)
    return genes
Пример #4
0
def random_gene(T):
    """
    Returns a random gene, ie. selection of intervals for the constraints.
    The gene is of the form [interval selection, distance graph, failed constraints]
    """

    num_variables = max([max(t['i'], t['j']) for t in T])
    interval_selection = [
        randint(0,
                len(constr['intervals']) - 1) for constr in T
    ]
    gene = [interval_selection, discrete_graph(num_variables + 1), None]
    update_graph(gene, T)
    d_graph = generate_d_graph(gene[1])  # costly op.
    failed = verify_witness(get_middles_sol(d_graph), T)
    gene[2] = failed

    return gene
Пример #5
0
def meta_walk(T, max_iterations, max_flips, verbose=False):
    """
    Meta walk algorithm.
    
    Arguments:
    T -- the constraints
    max_iterations -- max number of iterations
    max_flips -- max number of flips
    """

    num_variables = max([max(t['i'], t['j']) for t in T])
    graph = discrete_graph(num_variables + 1)

    best_gene = None
    best_gene_failed = None

    for i in range(max_iterations):
        gene = random_gene(T)
        update_graph(gene, T)

        for j in range(max_flips):
            # at each iteration we modify

            is_consistent, witness = solve_stp(gene[1])
            gene_failed = verify_witness(witness, T)
            gene[2] = gene_failed

            if not best_gene or len(gene_failed) < len(best_gene_failed):
                best_gene = gene
                best_gene_failed = gene_failed

            if is_consistent:
                break

            walk_gene(gene, T)

    if verbose:
        print(f'num flips: {i+1}, num iterations: {j+1}')
        print('best gene:', best_gene)
        print('constraints failed:', len(best_gene_failed), 'out of:', len(T))
        print('failed constraints:', best_gene_failed)

    return best_gene[0], best_gene_failed
Пример #6
0
def direct_walk(T, r, max_iterations, max_flips, pick_best_gene, verbose=False):
    """
    Direct walk-based algorithm.
    
    Arguments:
    T -- the constraint problem
    r -- range [-r, r] of assignments to consider
    max_iterations -- number of maximum random iterations
    max_flips -- maximum number of walks per iteration
    pick_best_gene -- whether to do extra work to find best gene to walk
    """
    
    best_gene = None
    best_gene_failed = None
    
    for i in range(max_iterations):
        gene = random_gene(T, r)
        
        for j in range(max_flips):
            gene_failed = verify_witness(gene, T)
            
            if not best_gene or len(gene_failed) < len(best_gene_failed):
                best_gene = gene
                best_gene_failed = gene_failed
            
            if len(gene_failed) == 0:
                break
                
            gene = walk_gene(gene, T, pick_best_gene)
                
        # necessary for double break after flips loop
        if len(gene_failed) == 0: break
    
    if verbose:
        print(f'num iterations: {i+1}')
        print('best gene:', best_gene)
        print('constraints failed:', len(best_gene_failed), 'out of:', len(T)) 
        print('failed constraints:', best_gene_failed)
    
    return best_gene, best_gene_failed
Пример #7
0
def fitness(gene, T):
    return -len(verify_witness(gene, T))