def neightborhood(s): neig = [] for i in bp.state_Expansion(s): # add all valid states from the expansion of the given state if bp.state_Verify(i): neig.append(i) for i in bp.state_Retract(s): # add all valid states from the retraction of the given state if bp.state_Verify(i): neig.append(i) return neig
def neightborhood(s): neig = [] for i in bp.state_Expansion(s): # add all valid states from the expansion of the given state if bp.state_Verify(i): neig.append(i) for i in neig: # adding all valid retractions of each state currently in the neightborhood for j in bp.state_Retract(i): if bp.state_Verify(j): neig.append(j) for i in bp.state_Retract(s): # add all valid states from the retraction of the given state if bp.state_Verify(i): neig.append(i) return neig
def neightborhood(s, T, OBJs): neig = [] for i in bp.state_Expansion( s): # adding all valid expansions of the given state if bp.state_Verify(i, T, OBJs): neig.append(i) # for i in neig: # adding all valid retractions of each state currently in the neightborhood # for j in bp.state_Retract(i): # if bp.state_Verify(j, T, OBJs): # neig.append(j) for i in bp.state_Retract( s): # adding all valid retractions of the given state if bp.state_Verify(i, T, OBJs): neig.append(i) return neig
def grasp(iter): bs = [0]*len(bp.OBJs) for _ in range(iter): s = greedy_Random_Construct() s = local_Search(s) if bp.state_Verify(s) and bp.state_Value(s) > bp.state_Value(bs): bs = s return bs
def opt_Estimate(status): # Selecting the most cost-effective object: w = [o[0] / o[1] for o in bp.OBJs] e = w.index(max(w)) # Filling the given status with it: s = status.copy() while bp.state_Verify(s): s[e] += 1 return s
def genetic(popMaxSize, iter, crossoverRate, mutationRate): si = init_Population(popMaxSize) bs = [0]*len(bp.OBJs) for _ in range(iter): si = generate_New_Pop(si, crossoverRate, mutationRate) s = best_in_Pop(si) if bp.state_Verify(s) and bp.state_Value(s) > bp.state_Value(bs): bs = s return bs
def select_Best_States(n, st, T, OBJs): si = bp.state_Expansion(st) si = [[bp.state_Value(s, OBJs), s] for s in si] si.sort(reverse=True) si = [i[1] for i in si] bss = [] for i in si: if bp.state_Verify(i, T, OBJs): bss.append(i) return bss[:n]
def select_Best(si): sn = -1 # best state position sv = 0 # state value for i in range(len(si)): v = bp.state_Value(si[i]) # current value if bp.state_Verify(si[i]) and v > sv: sv = v sn = i if sn == -1: return False, [] return True, si[sn]
def multistart_Descend(iter, use_Deepest=False): if use_Deepest: func = dd.deepest_Descent else: func = sd.simple_Descent bs = [0] * len(bp.OBJs) # best state found for _ in range(iter): s = random_Start_State() sn = func(s) if bp.state_Verify(sn) and bp.state_Value(sn) > bp.state_Value(bs): bs = sn return bs
def grasp(T, OBJs, execTime, *args): niter = args[0] numBest = args[1] s = [0] * len(OBJs) bs = s # best state found start = time() for _ in range(niter): if time() - start > execTime: break s = greedy_Random_Construct(s, numBest, T, OBJs, start, execTime) sl = local_Search(T, OBJs, s, start, execTime) # local state found if bp.state_Verify( sl, T, OBJs) and bp.state_Value(sl, OBJs) > bp.state_Value(bs, OBJs): bs = sl return bs
def genetic(T, OBJs, execTime, *args): popMaxSize = args[0] niter = 500 crossoverRate = args[1] mutationRate = args[2] si = init_Population(popMaxSize, OBJs) bs = [0] * len(OBJs) start = time() for _ in range(niter): if time() - start > execTime: break si = generate_New_Pop(si, crossoverRate, mutationRate, T, OBJs) s = best_in_Pop(si, T, OBJs) if bp.state_Verify( s, T, OBJs) and bp.state_Value(s, OBJs) > bp.state_Value(bs, OBJs): bs = s return bs
def branch_n_bound(use_opt_estimate=True): bs = triv_solution() # best state found sv = bp.state_Value(bs) # value of best bs pq.insert(0, [0] * len(bp.OBJs)) # pushing initial state in queue while not pq.isEmpty(): cs = pq.remove() # current state si = bp.state_Expansion(cs) # expansion of current state for s in si: if bp.state_Verify(s): if use_opt_estimate: # selecting estimate method est = opt_Estimate(s) else: est = n_Opt_Estimate(s) if bp.state_Value(est) > sv: if bp.state_Value(s) > sv: bs = s # updating best bs sv = bp.state_Value(s) # updating best value pq.insert(bp.state_Value(s), s) return bs
def select_Random(si): probRatio = [] # roulette # Adding all states's values to probRatio: for s in si: probRatio.append(bp.state_Value(s)) # Normalizing the values: ratioSum = sum(probRatio) probRatio = [ (i/ratioSum) for i in probRatio] # Building the "partitions" of the roulette: for i in range(len(probRatio)): probRatio[i] = sum(probRatio[i:]) # Selecting a random element: ratioSum = sum(probRatio) selector = random.random() for i in range(len(probRatio)): if selector >= probRatio[i]: s = si[i] break if bp.state_Verify(s): return True, s return False, []
def greedy_Random_Construct(s, numBest, T, OBJs, start, execTime): sn = [0] * len(OBJs) # initial state while True: if time() - start > execTime: break additions = bp.state_Expansion( sn) # list of possible additions to state sn best = [] # list of best additions # Selecting the best additions: i = 0 # add states to best until best is full or there are no more states: while len(best) < numBest and i < len(additions): if bp.state_Verify(additions[i], T, OBJs): best.append(additions[i]) if len(best) >= len(additions): break i += 1 # if best is not empty chose a state by the roulette method: if len(best) > 0: c = roulette(best, OBJs) sn = c continue break return sn
def fitness(s, T, OBJs): if bp.state_Verify(s, T, OBJs): return bp.state_Value(s, OBJs) return 0