def greedy_Random_Construct(): sn = [0]*len(bp.OBJs) # initial state c = True # continue flag while c: cs = sn # storing current state c, sn = select_Random(bp.state_Expansion(cs)) return cs
def hill_Climbing(): sn = [0] * len(bp.OBJs) # initial state c = True # continue flag while c: cs = sn # storing current state c, sn = select_Best(bp.state_Expansion(cs)) return cs
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 select_Best_States(n, st): si = bp.state_Expansion(st) si = [[bp.state_Value(s),s] for s in si] si.sort(reverse=True) si = [i[1] for i in si] bss = [] for i in filter(bp.state_Verify, si): bss.append(i) return bss[:n]
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 hill_Climbing(T, OBJs, execTime, *args): sn = [0] * len(OBJs) # initial state c = True # continue flag start = time() while c: if time() - start > execTime: break cs = sn # storing current state c, sn = select_Best(bp.state_Expansion(cs), T, OBJs) return cs
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 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 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