def gen_schedules( schedule_cand, m_filename): #Generate schedule candidates using simulated annealing global master_cand, final final = False master_cand = read_input(m_filename) current_cost = cost(schedule_cand) #print 'Current cost: %d' % (current_cost) flag = True while (flag): flag = False for i in range(1, 52): for x in [0, 1, 2, 3]: for y in [1, 2, 3, 5]: if (x != y): for j in range(i, 53): if (master_cand[i, x] == 0 and master_cand[i, y] == 0 and master_cand[j, x] == 0 and master_cand[j, y] == 0): #only swap non-requested slots if (schedule_cand[i, x] == schedule_cand[j, y] and schedule_cand[i, y] == schedule_cand[j, x]): before_cost = cost(schedule_cand) schedule_cand = do_swap( i, j, x, y, schedule_cand) after_cost = cost(schedule_cand) if (after_cost >= before_cost): schedule_cand = do_swap( i, j, x, y, schedule_cand) else: #print 'swapped (i = %d, j = %d, x = %d, y = %d), new cost: %d' % (i, j, x, y, after_cost) flag = True return schedule_cand
def TabuSearch(s0, k, max_iter): tabu_map = {} m = len(s0) n = len(s0[0]) sCur = np.zeros([max_iter, m, n]) sBest = np.zeros([max_iter, m, n]) solution = np.zeros(max_iter) sCur[0,:,:] = s0 sBest[0,:,:] = s0 bestCost = cost(sBest[0,:,:]) solution[0] = bestCost for i in range(max_iter-1): start_time = time() sCur[i+1,:,:] = neighbor(sBest[i,:,:], k, tabu_map) #evaluate the cost of the new solution curCost = cost(sCur[i+1,:,:]) #update the best cost and best solution if curCost < bestCost: sBest[i+1,:] = sCur[i+1,:,:] bestCost = curCost else: sBest[i+1,:] = sBest[i,:,:] #update the solution matrix solution[i+1] = bestCost # print("iteration complete %.2f" % (time() - start_time)) return solution, sBest[max_iter-1,:,:]
def computeGradient(x,currentCost,twistMode): newCosts = np.zeros(7) if twistMode == 'flat': coordinates = 4 delta = np.array([0,0,0,0,1,1,1]) twistCost,_,_ = cost(x+np.multiply(epsilon,delta)) newCosts[4]=newCosts[5]=newCosts[6]=twistCost for i in range(coordinates): newCosts[i],_,_ = cost(x+np.multiply(epsilon,signal.unit_impulse(7,i))) gradient = np.divide(newCosts-currentCost,epsilon) return gradient
def compileData(elements=num_elements, hoursout=hours_spent): """ This method calculates energy consumption in KWH and yearly cost in dollars for each PERSON. :param elements: int :param hoursout: numpy array :return: pandas dataframe """ global weeks_in_year elements = int(elements) for i in range(elements): weekday = phoneUsage.a_weekday(num_elements=elements) weekend = phoneUsage.a_weekend(hoursspent=hoursout, num_elements=elements) recharge = energyConsumption.rechargeInstances(elements) battery = energyConsumption.batteryInstances(elements) weekendUsageDay = energyConsumption.usageDay(weekend, battery, recharge) weekdayUsageDay = energyConsumption.usageDay(weekday, battery, recharge) totalConsumption = energyConsumption.energyConsumption(weekdayUsageDay, weekendUsageDay, battery) yearlyCost = cost.cost(totalConsumption) data = { "Recharging Point": recharge, "Battery Life": battery, "Weekend Usage (per day)": weekendUsageDay, "Weekday Usage (per day)": weekdayUsageDay, "Total Energy Consumption (per person)": totalConsumption, "Total Yearly Cost (per person)": yearlyCost } df = pd.DataFrame(data) pd.DataFrame.to_csv(df, "SmartphoneCostPerPerson.csv") return df
def fixed_pitch_solve(solver, pitches, wishes, relations, n): """ Call the solver for every possible combinations of n pitches, thus forcing the amount of pitches to n. Returns the best solution of all calls :param pitches: <pitch, <role, load>> :param wishes: <student, [(pitch, role)]> :param relations: <student, <student, cost>> :param n: final number of pitches in solution :return: [(student, wish index)] """ pitch_list = list(pitches.keys()) assert len(pitch_list) >= n print(f"Bruteforcing all combinations of {n} in {len(pitch_list)} pitches") total = ncr(len(pitch_list), n) best_solution = None best_cost = float("+inf") for i, select_pitches in enumerate(combinations(pitch_list, n)): print(f"{i}/{total} best cost = {best_cost} ", end="\r") select_pitches = set(select_pitches) _pitches = restrict_pitches(pitches, select_pitches) _wishes = restrict_wishes(wishes, select_pitches) solution = solver(_pitches, _wishes, relations) new_cost = cost(_pitches, _wishes, solution, relations) if new_cost < best_cost: best_cost = new_cost best_solution = solution return best_solution
def uniform_rollout(path, robot, budget): #Possible Actions action_set = list() action_set.append('left') action_set.append('right') action_set.append('forward') action_set.append('backward') # Random rollout policy # Pick random actions until budget is exhausted new_path = copy.deepcopy(path) robot.set_path(path) #Move Robot along the path action = "start" while action: action = robot.follow_path() # Move the robot robot.move(action) while cost(new_path) < budget: r = random.randint(0, len(action_set) - 1) action = action_set[r] robot.move(action) new_path.append(robot.get_loc()) return path
def simulated_annealing(s_initial, t_initial, alpha, beta, m_initial, maxtime): temp = t_initial current_s = s_initial best_s = current_s current_cost = cost(current_s) best_cost = current_cost time = 0 m = m_initial solution = [] best_s_vals = [] iter_num = 0 while time < maxtime: iter_num += 1 current_s, current_cost, best_s, best_cost = \ metropolis(current_s, current_cost, best_s, best_cost, temp, m) time += m if temp > 0.1: # Preventing divide by zero on line 68 temp *= alpha m *= beta result = [iter_num, current_cost, best_cost] solution.append(result) best_s_vals.append(best_s) return (solution, best_s)
def neighbor_classic(sCur, tenure, tabu_map): best_cost = float("inf") tabu_index = None rows, cols = len(sCur), len(sCur[0]) for i in range(rows): for j in range(cols): for k in range(j, cols): if sCur[i,j] != sCur[i,k] and (i,j,k) not in tabu_map: temp = sCur[i,j] sCur[i,j] = sCur[i,k] sCur[i,k] = temp new_cost = cost(sCur) if new_cost < best_cost: best_cost = new_cost tabu_index = (i,j,k) sCur[i,k] = sCur[i,j] sCur[i,j] = temp tabu_map = { k:(v - 1) for k,v in tabu_map.items() if k != 0 } best_neighbor = sCur[:,:] i,j,k = tabu_index temp = best_neighbor[i,j] best_neighbor[i,j] = best_neighbor[i,k] best_neighbor[i,k] = temp tabu_map[tabu_index] = tenure return best_neighbor
def main(): data = pd.read_csv('data/ex2data1.csv') initial_X = np.array(data.iloc[:,[0, 1]]) y = np.array(data.iloc[:,2]) X = normalize(initial_X) # Concatenate column of ones m, n = X.shape ones = np.array([np.ones(m)]) concat_X = np.concatenate((ones.T, X), axis=1) costs = [] theta = np.zeros((n+1, 1)) for i in range(0, ITERATIONS): costs.append(cost(theta, concat_X, y)) theta = gradient(theta, concat_X, y, ALPHA) prepare_scatter(X, y) print(theta) final_func = lambda x: (-theta[1]*x - theta[0])/theta[2] x_axis_values = [ i/1000 for i in range(0, 1000) ] y_axis_values = [ final_func(x) for x in x_axis_values] plt.plot(x_axis_values, y_axis_values, 'r--') plt.show()
def compileDataPlaces(elements, hoursout): """ This method calculates energy consumption in KWH and yearly cost in dollars for each PLACE. Remember that hoursout must have the same length as the interger value of elements. :param elements: int :param hoursout: numpy array :return: pandas dataframe """ global weeks_in_year elements = int(elements) for i in range(elements): all_days = phoneUsage.a_weekend(hoursspent = hoursout, num_elements=elements) recharge = energyConsumption.rechargeInstances(elements) battery = energyConsumption.batteryInstances(elements) usageDays = energyConsumption.usageDay(all_days, battery, recharge) totalConsumption = energyConsumption.energyConsumption(usageDays, usageDays, battery) weeklyCost = cost.cost(totalConsumption) yearlyCost = weeklyCost * weeks_in_year data = {"Recharging Point": recharge, "Battery Life": battery, "Phone Usage (per day))": usageDays, "Total Energy Consumption (per person)": totalConsumption, "Total Yearly Cost (per person)": yearlyCost} df = pd.DataFrame(data) pd.DataFrame.to_csv(df, "SmartphoneCostPerPerson.csv") return df
def main(): data = pd.read_csv('data/ex2data2.csv') initial_X = np.array(data.iloc[:, [0, 1]]) y = np.array(data.iloc[:, 2]) X = normalize(initial_X) mapped_X = np.array([[ 1.0, x[0], x[1], x[0] * x[1], x[0]**2, x[1]**2, x[0] * x[1]**2, x[1] * x[0]**2 ] for x in X]) costs = [] m, n = mapped_X.shape theta = np.zeros((n, 1)) for i in range(0, ITERATIONS): costs.append(cost(theta, mapped_X, y, LAMBDA)) theta = gradient(theta, mapped_X, y, ALPHA, LAMBDA) # Testing train data hits = 0 errors = 0 for i in range(len(X)): y_try = 1 if hypothesis(theta, mapped_X[i]) >= 0.5 else 0 if (y_try == y[i]): hits += 1 else: errors += 1 print("Acertos: {0}".format(hits)) print("Erros: {0}".format(errors)) print("Total: {0}".format(hits + errors)) print("Acurácia: {0}%".format(hits / (hits + errors) * 100)) # prepare_scatter(X, y) plt.show()
def find_cost_free_s(s): while cost(s) > 0: (cell, channel) = find_most_conflicts(s) s[cell][channel] = 0 num_values = sum(sum(row) for row in s) return (s, num_values)
def SA(sInitial, clauses, tInitial, alpha, beta, mInitial, maxTime, neighbor): # Set initial parameters temperature = tInitial currentSolution = sInitial bestSolution = currentSolution currentCost = cost(currentSolution, clauses) bestCost = currentCost time = 0 M = 1.0 * mInitial solutionMatrix = [] iteration = 1 while time < maxTime: # Metropolis function mCopy = M while mCopy > 0: newSolution = neighbor(currentSolution, clauses) newCost = cost(newSolution, clauses) deltaCost = newCost - currentCost if deltaCost < 0: currentSolution = newSolution currentCost = newCost if newCost < bestCost: bestSolution = newSolution bestCost = newCost else: if random() < exp(-deltaCost / temperature): currentSolution = newSolution currentCost = newCost # Update solution matrix and iteration solutionMatrix.append((iteration, currentCost, bestCost)) iteration = iteration + 1 mCopy = mCopy - 1 # Update SA parameters time = time + M temperature = alpha * temperature M = beta * M return (solutionMatrix, bestSolution)
def callback_cost(self): help_window().help_set_help(["cost.png",_("<big><b>Costs window</b></big>\nUse this window to calculate the cost of the solar cell and the energy payback time.")]) if self.cost_window==None: self.cost_window=cost() if self.cost_window.isVisible()==True: self.cost_window.hide() else: self.cost_window.show()
def callback_cost(self): help_window().help_set_help(["cost.png",_("<big><b>Costs window</b></big>\nUse this window to calculate the cost of the solar cell and the energy payback time.")]) if self.cost_window==False: self.cost_window=cost() if self.cost_window.isVisible()==True: self.cost_window.hide() else: self.cost_window.show()
def is_overbudget_or_invalid(a): # pat seq_copy = copy.deepcopy(current.sequence) seq_copy.append(a) result = robot.check_valid_move(a.label) if cost(seq_copy) > budget: return True # a will not be added to the new_unpicked_child_actions list if result == False: return True # a will not be added to the new_unpicked_child_actions list else: return False
def gradientDescent(theta, X, y, m, alpha, regParam, iterations): "Run Gradient Descent, returning updated theta" i = 0 costRecord = [] while i < iterations: hypo = logHypo(theta, X) theta = theta - alpha * _gradient(theta, hypo, X, y, m, regParam) costRecord.append(((np.asscalar(cost(hypo, theta, y, regParam, m))), i)) i += 1 plotCost(costRecord) #TODO remove return theta
def node_is_valid(a): seq_copy = copy.deepcopy(current.sequence) seq_copy.append(a) x, y = a.location over_budget = (cost(seq_copy) > budget) # Check invalid_locations from map for loc in world_map.invalid_locations: if x == loc[0] and y == loc[1]: return False return not over_budget
def run_schedule(s_filename, savename): global perm_list logging.basicConfig(filename='log/' + savename + '.log', format='%(asctime)s %(message)s', \ datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO, mode='w') regina_list = [1, 2, 5, 6] mvsc_list = [1, 2, 3, 6] perm_list = gen_permutations(6, mvsc_list, regina_list) schedule_cand = read_input(s_filename) #print schedule_cand logging.info('beginning schedule generation using simulated annealing') schedule_cand = gen_schedules(schedule_cand) logging.info('finished schedule generation, best cost %d', cost(schedule_cand)) save_result(savename, schedule_cand)
def __init__(self, L, phys_model): # -------- global constant variables ---------- self.L = L self.n_spins = L * L self.learning_rate = 0.01 lam = -10. # ------------- define the MLP and revnet objects --- self.F = mlp_net(self.n_spins) self.G = mlp_net(self.n_spins) self.R = rev_net(self.F, self.G) # ------------- define the cost function ------------ self.ising = phys_model self.cost = cost(self.ising, lam)
def fit_with_cost(x, y, theta, alpha, num_iters): m = len(y) cost_history = [] for index in range(num_iters): som = 0 for i in range(m): som += (predict(x[i], theta) - y[i]) t0 = theta[0] - (alpha / m) * som som = 0 for i in range(m): som += ((predict(x[i], theta) - y[i]) * x[i]) t1 = theta[1] - (alpha / m) * som theta[0] = t0 theta[1] = t1 _cost = cost(x, y, theta) print("Cost after iteration ", index, ": ", _cost[0], end="\r") cost_history.extend(_cost) print("Cost after iteration ", num_iters, ": ", cost(x, y, theta)[0], end="\n\n") return theta, cost_history
def test_load(file="solution.json"): with open("dummy_pitches.json", "r") as fpitches: # pitches[pitch]["workload"]: <role, load> # pitches[pitch]["author"]: a student pitches = json.load(fpitches) with open("dummy_wishes.json", "r") as fwishes: wishes = json.load(fwishes) # reconvert the tuples that got converted to list with json for student, ranking in wishes.items(): wishes[student] = [(pitch, role) for pitch, role in ranking] with open("dummy_relations.json", "r") as frels: relations = json.load(frels) solution = load_solution(file, wishes) c = cost(pitches, wishes, solution, relations) print(f"{file} cost: {c:.2f}")
def gradientStep(x,currentCost): # x = [y1, y2, y3, y4, t1, t2, t3] x = np.array(x) step = 10*epsilon grad = computeGradient(x,currentCost,'flat') print("Grad: ",grad) normalizedGrad = np.divide(grad,abs(grad)) print("Normalized Grad: ",normalizedGrad) newX = proj(x - np.multiply(step,normalizedGrad)) newCost, omega, vel = cost(newX) return newX, newCost, omega, vel
def metropolis(current_s, current_cost, best_s, best_cost, t, m_current): m = m_current while m > 0: new_s = neighbor(current_s) new_cost = cost(new_s) d_cost = new_cost - current_cost if d_cost > 0: current_s = new_s current_cost = new_cost if new_cost > best_cost: best_s = new_s best_cost = new_cost elif random.random() < math.e ** (d_cost / t): current_s = new_s current_cost = new_cost m -= 1 return current_s, current_cost, best_s, best_cost
def RandomSearch(maxiter, m=7, n=50): sCur = np.zeros([maxiter, m, n]) sBest = np.zeros([maxiter, m, n]) solution = np.zeros(maxiter) bestCost = 500.0 for i in range(maxiter): sCur[i,:,:] = np.array(generateRandomAllocation()) curCost = cost(sCur[i,:,:]) if curCost < bestCost: sBest[i,:,:] = sCur[i,:,:] bestCost = curCost else: sBest[i,:,:] = sBest[i-1,:,:] solution[i] = bestCost return solution, sBest[maxiter - 1,:,:]
def neighbor(sCur, tenure, tabu_map, num_cell_neighbors=2, cell_variance=5): best_cost = float("inf") tabu_cell = -1 best_neighbor = None rows, cols = len(sCur), len(sCur[0]) for i in range(rows): if i not in tabu_map: for j in range(num_cell_neighbors): temp = sCur[i,:] sCur[i,:] = shuffle_cell(sCur[i,:], cell_variance) new_cost = cost(sCur) if new_cost < best_cost: best_cost = new_cost best_neighbor = sCur[:,:] tabu_cell = i sCur[i,:] = temp tabu_map = { k:(v - 1) for k,v in tabu_map.items() if k != 0 } tabu_map[tabu_cell] = tenure return best_neighbor
def train(Xin, Yin, h, num_iter, gamma, lambda_reg): m = np.shape(Xin)[0] # Same as np.shape(Yin)[0] n = np.shape(Xin)[1] p = np.shape(Yin)[1] # Initial weights. W1 = np.random.uniform(-0.12, 0.12, h * (n + 1)).reshape(h, n + 1) # h by n + 1 W2 = np.random.uniform(-0.12, 0.12, p * (h + 1)).reshape(p, h + 1) # p by h + 1 counter = 1 while counter <= num_iter: v = predict(Xin, Yin, h, W1, W2) A1, A2, A3 = v[0], v[1], v[2] # m by n + 1, m by h + 1, m by p J = cost(Yin, A3, lambda_reg, W1, W2) print(counter, J, sep=': ') # For backpropagation delta3 = (A3 - Yin) # m by p W2bar = W2[:, 1:] # This is W2 with first column removed with shape p by h. A2prime = A2[:, 1:] # m by h delta2 = A2prime * np.dot(delta3, W2bar) # m by h # Change in weights # The first column of W1 and W2 are not regularized. dW2 = -gamma / m * np.dot(delta3.transpose(), A2) dW2[:, 1:] = dW2[:, 1:] + lambda_reg / m * W2[:, 1:] dW1 = -gamma / m * np.dot(delta2.transpose(), A1) dW1[:, 1:] = dW1[:, 1:] + lambda_reg / m * W1[:, 1:] W1 = W1 + dW1 W2 = W2 + dW2 counter = counter + 1 return [W1, W2]
def simulated_annealing(s_initial, t_initial, alpha, beta, m_initial, maxtime): t = t_initial current_s = s_initial best_s = current_s current_cost = cost(current_s) best_cost = current_cost time = 0 m = m_initial solution = [] best_s_vals = [] iter_num = 0 while time < maxtime: iter_num += 1 current_s, current_cost, best_s, best_cost = \ metropolis(current_s, current_cost, best_s, best_cost, t, m) time += m t *= alpha m *= beta result = [iter_num, current_cost, best_cost] solution.append(result) best_s_vals.append(best_s) return (solution, best_s)
def fitness(s): return max_cost - cost(s)
def greedy_path(self, robot, map): #Use robot to simulate a random path path = list() #Possible Actions action_set = list() action_set.append('left') action_set.append('right') action_set.append('forward') action_set.append('backward') # Pick random actions until budget is exhausted while cost(path) < self.budget: curr_loc = robot.get_loc() r = randint(0, len(action_set) - 1) #Get closest hotspot closest_hotspot = None min_dist = sys.maxsize hotspots = [ h for h in map.hotspots if not h in self.visited_hotspots ] for h in hotspots: dist = map.euclidean_distance(curr_loc, h) if dist < min_dist: closest_hotspot = h min_dist = dist #Determine action that moves us towards a hotspot if closest_hotspot: #If we haven't visited all the hotspots, keep visiting them best_action = None min_dist = sys.maxsize for a in action_set: robot.move(a) dist = map.euclidean_distance(robot.get_loc(), closest_hotspot) if dist < min_dist: best_action = a min_dist = dist robot.set_loc(curr_loc[0], curr_loc[1]) #If the robot visits a hotspot, don't visit it again if min_dist <= robot.sensing_range: self.visited_hotspots.add(closest_hotspot) #Choose best action robot.move(best_action) path.append(State(0, best_action, robot.get_loc())) else: #Else move Randomly randNumb = randint(0, 3) direction = None if randNumb == 0: direction = 'left' if randNumb == 1: direction = 'right' if randNumb == 2: direction = 'backward' if randNumb == 3: direction = 'forward' robot.move(direction) path.append(State(0, direction, robot.get_loc())) robot.reset_robot() return path
import time from cost import cost from allocation import generateRandomAllocation # Timing the execution of 10,000 random allocations + cost evaluations numEvals = 10000 start_time = time.time() for i in range(numEvals): cost(generateRandomAllocation()) print("%d evals take %s seconds." % (numEvals, (time.time() - start_time)))
def call(self, x): return cost(x, self.positions, self.config)
Y = data[::, -1] print(X) m = X.size theta = np.random.rand(2) theta = np.array([0, 1]) print("Original Theta: ", theta) alpha = 0.01 j = 0 while True: j = j + 1 print("Epoch %d:" % (j)) # Training Epoch hypotheses = hypothesis(m, 2, theta, X) print("hypotheses: ", hypotheses) c = cost(m, hypotheses, Y) print("Cost: ", c) if (c <= 0.01): print("Theta0 %f Theta1: %f" % (theta[0], theta[1])) break # Find derivative of cost function for theta 0 and theta 1 sumError0 = 0 sumError1 = 0 for i in range(0, 3): sumError0 += (hypotheses[i] - Y[i]) sumError1 += (hypotheses[i] - Y[i]) * X[i] print("SumError0: ", sumError0) print("SumError1: ", sumError1) temp0 = theta[0] - (alpha * (1.0 / m) * sumError0)
for i in range(m): if y[i] == 1: pos1.append(x1[i]) pos2.append(x2[i]) elif y[i] == 0: neg1.append(x1[i]) neg2.append(x2[i]) plt.scatter(pos1, pos2, c='red', alpha=0.5) plt.scatter(neg1, neg2, c='blue', alpha=0.5) plt.title('Scatter plot of training data') plt.xlabel('Exam 1 Score') plt.ylabel('Exam 2 Score') #plt.show() #print(sigmoid.sigmoid(np.array([[1,2],[3,4]]))) X = np.c_[np.ones((m,1)),x1, x2] y = np.array(y) theta = np.zeros((3,1)) J = cost.cost(X, y, theta) theta1 = cost.gradient(theta,X,y) theta = [-25.16131856, 0.20623159, 0.20147149] x_values = [np.min(X[:, 1] - 5), np.max(X[:, 2] + 5)] y_values = - (theta[0] + np.dot(theta[1], x_values)) / theta[2] plt.plot(x_values, y_values, label='Decision Boundary') plt.xlabel('Marks in 1st Exam') plt.ylabel('Marks in 2nd Exam') plt.legend() plt.show() #plt.show()
def DDS(s0, sMin, sMax, maxiter, r, pDecrease = 'exponential'): '''Returns a column vector of the best function value (for miminization problem)after each iteration and a row vector of the best solution found''' #Variable definitions: #s0 = initial solution; an n-dimensional vector where each dimension represents a decision variable #sMax = n-dimensional vector of the maximum value in the domain of each respective decision variable #sMin = n-dimensional vector of the minimum value in the domain of each respective decision variable #maxiter = total number of iterations to run #r = parameter between 0 and 1 that determines the standard deviation of the random Gaussian perturbation of each decision variable; #the standard deviation of each random Gaussian perturbation is equal to a fraction (r) of the total domain of that decision variable #pDecrease = string defining how the probability of perturbing each dimension varies with iteration number; #'linear' gives a linear decrease in probability with iterations, while the default is an exponential decrease #initialize all variables m = len(s0) n = len(s0[0]) sCur = np.zeros([maxiter, m, n]) sBest = np.zeros([maxiter, m, n]) solution = np.zeros(maxiter) sCur[0,:,:] = s0 sBest[0,:,:] = s0 bestCost = cost(sBest[0,:,:]) solution[0] = bestCost sigma = [] #determine the standard deviation of the random Gaussian perturbation for each dimension for i in range(m): sigma.append(r * (sMax[i] - sMin[i])) for i in range(maxiter-1): #determine the probability of perturbing each dimension if pDecrease == "exponential": p = 1 - np.log(i+2)/np.log(maxiter) else: p = 1 - (i+2)/maxiter #perturb dimensions probabilistically for j in range(m): if (random() < p): sCur[i+1,j,:] = neighbor(sBest[i,j,:], sMax[j], sMin[j], sigma[j]) else: sCur[i+1,j,:] = sBest[i,j,:] """ if(random() < p): sCur[i+1,j,:] = neighbor(sBest[i,j,:], sMax[j], sMin[j], sigma[j]) else: sCur[i+1,j,:] = sBest[i,j,:] """ #if no dimensions have been changed, choose one randomly to perturb if np.array_equal(sCur[i+1,:,:], sBest[i,:,:]): d = randint(0,m-1) sCur[i+1,d,:] = neighbor(sBest[i,d,:], sMax[d], sMin[d], sigma[d]) #evaluate the cost of the new solution curCost = cost(sCur[i+1,:,:]) #update the best cost and best solution if the new solution is better than the previous best if curCost < bestCost: sBest[i+1,:] = sCur[i+1,:,:] bestCost = curCost else: sBest[i+1,:] = sBest[i,:,:] #update the solution matrix solution[i+1] = bestCost return solution, sBest[maxiter-1,:,:]
def dec_mcts(budget, num_samples, computational_budget, explore_exploit, robots, input_map, rollout_policy='uniform'): world_map = copy.deepcopy(input_map) robot_paths = [] # Initialize Every Robots MCTS Tree for robot in robots: # MCTS Tree initialization robot.root = mcts_initialize(budget, robot, world_map) # Start Dec-MCTS k = 0 while k < computational_budget: # Computational Budget # for p in range(computational_budget): # if p % 100 == 0: # print("Percent Complete: {:.2f}%".format(p / float(computational_budget) * 100)) # print("Computational Budget: ", k) for i in range(num_samples): # Grow Tree for each Robot for robot in robots: # Robots determines their top 10 best sets of actions(sequences) # Selection and Expansion # move recursively down the tree from root # then add a new leaf node # print("Selection and Expansion") current = robot.root robot.reset_robot() while True: # Are there any children to be added here? if current.unpicked_child_actions: # if not empty # Pick one of the children that haven't been added | Select a valid direction num_unpicked_child_actions = len( current.unpicked_child_actions) child_index = random.randint( 0, num_unpicked_child_actions - 1) child_action = current.unpicked_child_actions[ child_index] # Move the robot # Remove the child form the unpicked list del current.unpicked_child_actions[child_index] # Setup the new action sequence new_sequence = copy.deepcopy(current.sequence) new_sequence.append(child_action) new_budget_left = budget - cost(new_sequence) # Setup the new child's unpicked children # Remove any over budget or invalid children from this set new_unpicked_child_actions = generate_neighbors( child_action, new_sequence, world_map.bounds) def node_is_valid(a): seq_copy = copy.deepcopy(current.sequence) seq_copy.append(a) x, y = a.location over_budget = (cost(seq_copy) > budget) # Check invalid_locations from map for loc in world_map.invalid_locations: if x == loc[0] and y == loc[1]: return False return not over_budget # removes any new children if from the child if they go over budget or are invalid action new_unpicked_child_actions = [ a for a in new_unpicked_child_actions if node_is_valid(a) ] # EXPANSION new_child_node = TreeNode( parent=current, sequence=new_sequence, budget=new_budget_left, unpicked_child_actions=new_unpicked_child_actions, node_id=current.node_id + 1) current.children.append(new_child_node) current = new_child_node # list_of_all_nodes.append(new_child_node) # for debugging only break # don't go deeper in the tree... else: # All possible children already exist # Therefore recurse down the tree # using the UCT selection policy if not current.children: # Reached planning horizon -- just do this again break else: # Define the UCB def ucb(average, n_parent, n_child): return average + explore_exploit * math.sqrt( (2 * math.log(n_parent)) / float(n_child)) # Pick the child that maximises the UCB if not current.children: break else: n_parent = current.num_updates best_child = -1 best_ucb_score = 0 for child_idx in range(len(current.children)): child = current.children[child_idx] ucb_score = ucb( child.average_evaluation_score, n_parent, child.num_updates) if best_child == -1 or (ucb_score > best_ucb_score): best_child = child best_ucb_score = ucb_score # Recurse down the tree current = best_child ################################ # Communications # Sample Action Sequences of other Robots # Pick one of the 10 sequences other_robots_paths = [] for bot in robots: randSequence = randint(0, 9) if bot != robot and computational_budget == 0: sampled_action_sequence = bot.top_10_sequences[ randSequence] other_robots_paths.append(sampled_action_sequence) ################################ # Rollout & Reward rollout_sequence = list() if rollout_policy == 'uniform': rollout_sequence = uniform_rollout(current.sequence, robot, budget) else: rollout_sequence = heuristic_rollout( current.sequence, robot, budget, world_map) rollout_reward = reward(rollout_sequence, other_robots_paths, robot.sensing_range, world_map) ################################ # Back-propagation # update stats of all nodes from current back to root node parent = current while parent: # is not None # Update the average parent.updateAverage(rollout_reward) # Recurse up the tree parent = parent.parent # Extract 10 Best Sequences from Current Robot # append each to robot. for robot in robots: list_of_top_10_nodes_sequences = [] # for troubleshooting list_of_top_10_nodes_ids = [] list_of_top_10_nodes = [] # for troubleshooting i = 0 while i != 10: current = robot.root # is not empty while current.children and all_children_nodes_are_not_in_the_list( current, list_of_top_10_nodes_ids): # Find the child with best score best_score = 0 best_child = -1 for child_idx in range(len(current.children)): child = current.children[child_idx] # Only consider child nodes who have not been placed in the list - pat if not child.node_id in list_of_top_10_nodes_ids: score = child.average_evaluation_score if best_child == -1 or (score > best_score): best_child = child best_score = score current = best_child # # for troubleshooting # # Append each iteration's node with the best average evaluation score # list_of_top_10_nodes_ids.append(current.node_id) # list_of_top_10_nodes.append(current) # # Append the the node's action sequence into a list # solution = [p.location for p in current.sequence] # list_of_top_10_nodes_sequences.append(solution) robot.top_10_sequences.append(current.sequence) i += 1 k += 1 ################################ # Extract best solution from all the ROBOTS # calculate best solution so far # by recursively choosing child with highest average reward for robot in robots: current = robot.root while current.children: # is not empty # Find the child with best score best_score = 0 best_child = -1 for child_idx in range(len(current.children)): child = current.children[child_idx] score = child.average_evaluation_score if best_child == -1 or (score > best_score): best_child = child best_score = score current = best_child robot.final_path = current.sequence for robot in robots: robot_paths.append(robot.final_path) return robot_paths
def spoofBest(): #interference_matrix=[[2, 1, 0, 0, 0, 0, 0], # [1, 2, 1, 1, 1, 0, 0], # [0, 1, 2, 0, 1, 0, 0], # [0, 1, 0, 2, 1, 1, 0], # [0, 1, 1, 1, 2, 1, 1], # [0, 0, 0, 1, 1, 2, 1], # [0, 0, 0, 0, 1, 1, 2]] traffic = [32, 26, 14, 32, 18, 20, 24] s = [] for i in range(7): s.append([0] * 50) # Set first row for i in range(25): s[0][i * 2] = 1 for i in range(7): s[0][i * 2 + 1] = 1 assert(sum(s[0]) == traffic[0]) # Set second row for i in range(25): s[1][i * 2 + 1] = 1 s[1][48] = 1 assert(sum(s[1]) == traffic[1]) # Set third row for i in range(14): s[2][i * 2] = 1 assert(sum(s[2]) == traffic[2]) # Set fourth row for i in range(25): s[3][i * 2] = 1 for i in range(7): s[3][i * 2 + 1] = 1 assert(sum(s[3]) == traffic[3]) # Set fifth row for i in range(18): s[4][i * 2 + 15] = 1 assert(sum(s[4]) == traffic[4]) # Set sixth row for i in range(20): s[5][i * 2 + 1] = 1 assert(sum(s[5]) == traffic[5]) # Set seventh row for i in range(24): s[6][i * 2] = 1 assert (sum(s[6]) == traffic[6]) return (s, cost(s))
def save_result(filename, schedule_cand): np.savetxt(filename[:-4] + '_2opt.csv', schedule_cand, delimiter=',') logging.info('saved schedule to %s', filename[:-4] + '_2opt.csv') logging.info('finished schedule generation, best cost %d', cost(schedule_cand))
for problem in range(FILE_START, FILE_START + NUM_FILES): fileMiddlePart = "{0:0>2}".format(problem) fileName = FILE_PREFIX + fileMiddlePart + FILE_SUFFIX clauses = loadClauses(fileName) for trial in range(NUM_TRIALS): print 'Solving:', fileName, 'Trial:', trial+1, '/', NUM_TRIALS, '(Monte Carlo)' # Try random attempts for the same amount of trials as SA would # have. randAllCosts = [] startTime = time.clock() for randAttempt in range(RAND_TRIALS): initial_rand_Solution = generateRandomSolution(NUM_VARIABLES, 0.5) randBestCost = cost(initial_rand_Solution, clauses) # record SAT solutions if randBestCost == 0: rand_sat_sols[trial+1].append(initial_rand_Solution) randAllCosts.append(randBestCost) elapsed = time.clock() - startTime all_rand_time[problem].append(elapsed) pickle.dump(randAllCosts, open('data/all-rand-sols_' + str(problem) + '_' + str(trial) + '_.p', 'wb')) # Dump results pickle.dump(rand_sat_sols, open('data/rand_sat_sols.p', 'wb')) pickle.dump(all_rand_time, open('data/all_rand_time.p', 'wb'))
import numpy as np import matplotlib.pyplot as plt W = sio.loadmat('weights_ref.mat') W1 = np.array(W['Theta1']) W2 = np.array(W['Theta2']) data = sio.loadmat('data.mat') Xin = np.array(data['X']) yin = np.array(data['y']) # This data was for octave. So zero is labeled as 10. # Convert each output digit from 0 through 9 to a vector in 10 dimensions. # 1 is represented as [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]. # 2 is represented as [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]. # 3 is represented as [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]. # ... # 0 is represented as [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]. Yin = np.zeros(len(yin) * 10).reshape(len(yin), 10) for j in np.arange(0, yin.shape[0]): if yin[j][0] == 10: Yin[j, 9] = 1 else: Yin[j, yin[j][0]-1] = 1 pred = predict(Xin, Yin, 25, W1, W2) Yhat = pred[2] cost0 = cost(Yin, Yhat, 0, W1, W2) cost1 = cost(Yin, Yhat, 1, W1, W2) print(cost0, cost1)
trainy1 = trainy1.reshape((trainy1.shape[0], 1)) trainy2 = trainy2.reshape((trainy1.shape[0], 1)) # ------- tf variables --------------------------- x1 = tf.placeholder("float", [None, n_spins]) x2 = tf.placeholder("float", [None, n_spins]) e1 = tf.placeholder("float", [None, 1]) e2 = tf.placeholder("float", [None, 1]) # ------------- define the MLP and revnet objects --- F_net = mlp_net(n_spins) G_net = mlp_net(n_spins) r_net = revnet(F_net, G_net) y1, y2 = r_net.forward(x1, x2) # ------------- define the cost function ------------ phys_model = ising(L) c = cost(phys_model, lam) loss_op = c.get(e1, e2, y1, y2) # ------------- define the optimizer ------------ optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) init = tf.global_variables_initializer() # test it with tf.Session() as sess: sess.run(init) sess.run(loss_op, feed_dict={ x1: trainx1,
def mcts( action_set, budget, max_iterations, exploration_exploitation_parameter, robot): ################################ # Setup start_sequence = [] unpicked_child_actions = copy.deepcopy(action_set) root = TreeNode(node_id = 0, parent=None, sequence=start_sequence, budget=budget, unpicked_child_actions=unpicked_child_actions) list_of_all_nodes = [] list_of_all_nodes.append(root) # for debugging only current_node = 0 ################################ # Main loop for iter in range(max_iterations): print("Current Iteration:", iter) print("Robot Loc: ", robot.get_loc()) ################################ # Selection and Expansion # move recursively down the tree from root # then add a new leaf node print("Selection and Expansion") current = root #Reset the Robot's Stating Point # robot.reset_robot() print("Robot Loc: ", robot.get_loc()) # Remove invalid actions from root state - pat for a in current.unpicked_child_actions: direction = a.label if not robot.check_valid_move(direction): current.unpicked_child_actions.remove(a) while True: # Are there any children to be added here? if current.unpicked_child_actions: # if not empty # Pick one of the children that haven't been added | Select a valid direction # Do this at random num_unpicked_child_actions = len(current.unpicked_child_actions) if num_unpicked_child_actions == 1: child_index = 0 else: child_index = random.randint(0,num_unpicked_child_actions-1) child_action = current.unpicked_child_actions[child_index] # Move the robot robot.move(child_action.label) # pat # Remove the child form the unpicked list del current.unpicked_child_actions[child_index] # Setup the new action sequence new_sequence = copy.deepcopy(current.sequence) new_sequence.append(child_action) new_budget_left = budget - cost(new_sequence) # Setup the new child's unpicked children # Remove any over budget or invalid children from this set new_unpicked_child_actions = copy.deepcopy(action_set) def is_overbudget_or_invalid(a): # pat seq_copy = copy.deepcopy(current.sequence) seq_copy.append(a) result = robot.check_valid_move(a.label) if cost(seq_copy) > budget: return True # a will not be added to the new_unpicked_child_actions list if result == False: return True # a will not be added to the new_unpicked_child_actions list else: return False # return cost(seq_copy) > budget #false #removes any new children if from the child if they go over budget or are invalid action new_unpicked_child_actions = [a for a in new_unpicked_child_actions if not is_overbudget_or_invalid(a)] # Create the new node and add it to the tree printActionSequence(new_sequence) current_node += 1 new_child_node = TreeNode(node_id = current_node, parent=current, sequence=new_sequence, budget=new_budget_left, unpicked_child_actions=new_unpicked_child_actions) current.children.append(new_child_node) current = new_child_node list_of_all_nodes.append(new_child_node) # for debugging only print('---') break # don't go deeper in the tree... else: # All possible children already exist # Therefore recurse down the tree # using the UCT selection policy if not current.children: # Reached planning horizon -- just do this again break else: # Define the UCB def ucb(average, n_parent, n_child): return average + exploration_exploitation_parameter * math.sqrt( (2*math.log(n_parent)) / float(n_child) ) # Pick the child that maximises the UCB n_parent = current.num_updates best_child = -1 best_ucb_score = 0 for child_idx in range(len(current.children)): child = current.children[child_idx] ucb_score = ucb(child.average_evaluation_score, n_parent, child.num_updates) if best_child == -1 or (ucb_score > best_ucb_score): best_child = child best_ucb_score = ucb_score # Recurse down the tree # robot.move(best_child.sequence[0].label) # pat current = best_child ################################ # Rollout print("Rollout Phase") rollout_sequence = rollout(subsequence=current.sequence, action_set=action_set, budget=budget) rollout_reward = reward(action_sequence=rollout_sequence) ################################ # Back-propagation # update stats of all nodes from current back to root node print("Back-Propagation Phase") parent = current while parent: # is not None # Update the average parent.updateAverage(rollout_reward) # Recurse up the tree parent = parent.parent print('====================') print('====================') ################################ # Extract solution # calculate best solution so far # by recursively choosing child with highest average reward print('Extracting Solutions') list_of_top_10_nodes_sequences = [] list_of_top_10_nodes_ids = [] list_of_top_10_nodes = [] i = 0 while i != 10: current = root while current.children and all_children_nodes_are_not_in_the_list(current, list_of_top_10_nodes_ids) : # is not empty # Find the child with best score best_score = 0 best_child = -1 for child_idx in range(len(current.children)): child = current.children[child_idx] # Only consider child nodes who have not been placed in the list - pat if not child.node_id in list_of_top_10_nodes_ids: score = child.average_evaluation_score if best_child == -1 or (score > best_score): best_child = child best_score = score current = best_child # Append each iteration's node with the best average evaluation score list_of_top_10_nodes_ids.append(current.node_id) list_of_top_10_nodes.append(current) # Append the the node's action sequence into a list print('Node: ', current.node_id) solution = current.sequence solution = listActionSequence(solution) solution = direction_path_to_state_path_converter(solution, robot.start_loc) print('Solution: ', solution) list_of_top_10_nodes_sequences.append(solution) i += 1 print('Top 10 Node List: ', list_of_top_10_nodes) print('Top 10 Node Id List: ', list_of_top_10_nodes_ids) print('Top 10 Sequences List: ', list_of_top_10_nodes_sequences) # Select the best node from the top 10 nodes list and provide it as the solution best_node = list_of_top_10_nodes[0] # there are still 9 other nodes to see the solutions of solution = best_node.sequence solution = listActionSequence(solution) solution = direction_path_to_state_path_converter(solution, robot.start_loc) winner = best_node print('length of node list: ', len(list_of_all_nodes)) return [solution, root, list_of_all_nodes, winner]
from allocation import generateRandomAllocation from cost import cost from SA import neighbor s_values = [] cost_values = [] #Set initial values s = generateRandomAllocation() cost_s = cost(s) s_values.append(s) cost_values.append(cost_s) best_s = s best_cost = cost_s # Find other s_values and calculate their costs while len(s_values) != 100: new_s = neighbor(s) cost_s = cost(new_s) if cost_s < best_cost: best_s = new_s best_cost = cost_s s_values.append(new_s) cost_values.append(cost_s) neighbor_total_cost = 0 for (new_s, cost_s) in zip(s_values, cost_values): if new_s != best_s: neighbor_total_cost += (cost_s - best_cost)
def heuristic_rollout(path, robot, budget, map): #Possible Actions action_set = list() action_set.append('left') action_set.append('right') action_set.append('forward') action_set.append('backward') # Random rollout policy # Pick random actions until budget is exhausted new_path = copy.deepcopy(path) robot.set_path(path) #Move Robot along the path action = "start" while action: action = robot.follow_path() # Move the robot robot.move(action) visited_hotspots = set() while cost(new_path) < budget: curr_loc = robot.get_loc() #Get closest hotspot closest_hotspot = None min_dist = sys.maxsize hotspots = [h for h in map.hotspots if not h in visited_hotspots] for h in hotspots: dist = map.euclidean_distance(curr_loc, h) if dist < min_dist: closest_hotspot = h min_dist = dist #Determine action that moves us towards a hotspot if closest_hotspot: #If we haven't visited all the hotspots, keep visiting them best_action = None min_dist = sys.maxsize for a in action_set: robot.move(a) dist = map.euclidean_distance(robot.get_loc(), closest_hotspot) if dist < min_dist: best_action = a min_dist = dist robot.set_loc(curr_loc[0], curr_loc[1]) #If the robot visits a hotspot, don't visit it again if min_dist <= robot.sensing_range: visited_hotspots.add(closest_hotspot) #Choose best action robot.move(best_action) new_path.append(robot.get_loc()) else: #Else move Randomly r = random.randint(0, len(action_set) - 1) action = action_set[r] robot.move(action) new_path.append(robot.get_loc()) return path
def LinearRegression(X, y,theta0, theta1): for i in range(0, 1000): if i % 100 == 0: plot.plotline(theta0, theta1, X, y) print(cost.cost(theta0, theta1, X, y)) theta0, theta1 = up.updateParameters(theta0, theta1, X, y, 0.005)
def proj(x): for i in range(7): if i < 4: # chord coordinate if x[i] < chordLowLimit: x[i] = chordLowLimit elif x[i] > chordHiLimit: x[i] = chordHiLimit else: # twist coordinate if x[i] < twistLowLimit: x[i] = twistLowLimit elif x[i] > twistHiLimit: x[i] = twistHiLimit return x if __name__ == '__main__': inputFile = open("startingBlade.txt","r") coordinates = 7 startingBlade = list(map(float,inputFile.readline().split(","))) currCost,startingOmega,startingVel = cost(startingBlade) newX, newCost = gradientDescent( startingBlade, currCost, startingOmega, startingVel, 50 )