def random_budget_graphs(n, p): print('budget') max_utilities = [] x_list = [] y_list_MILP = [] y_list_SIM = [] y_list_MODSIM = [] y_list_KNAPSACK = [] y_list_GREEDY = [] y_list_ACYCLIC_GREEDY = [] iterations_list_SIM = [] # iterations_list_MODSIM = [] iterations_list_MODSIM = iterations_list_SIM # random wattage energy list rand_cost = random_energy(n) # random utility array rand_utility = random_utility(n) # maximum utility of the current graph current_max_utility = sum(rand_utility) for b in range_budget(): # it creates one random graph g = new_random_graph(n, p) # this function create another graph (functional graph) from the graph g funct_g, pref_g = two_graphs(g) # compute the dependencies arrays y_p = depend_vector(pref_g) y_f = bsf_depend_vector(funct_g, n) Y_p = successors_number(y_p) Y_f = successors_number(y_f) # list of maximum utilities max_utilities.append(current_max_utility) # list of probability values (AXIS X) x_list.append(b) """ ******** MILP ******** """ # print('milp') # to call the milp algortihm m_milp, milp_sol = complete_milp(y_p, y_f, Y_p, Y_f, rand_cost, b, rand_utility, n) # to get the objective function value (AXIS Y) obj = m_milp.getObjective() y_list_MILP.append(obj.getValue()) """ ******** SIMULATED ANNEALING ******** """ print('regular sim ann') sol = np.zeros(n) # it starts with an empty subset # sol = list(np.zeros(n)) # cost = 0 # while(cost > b): # sol = list(np.random.randint(2, size = n)) # cost = cost_function(sol, rand_cost) solution, u_val, iterations = simulated_annealing( y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, b) y_list_SIM.append(u_val) iterations_list_SIM.append(iterations) # """ ******** MODIFIED SIMULATED ANNEALING ******** """ # print('modified sim ann') # sol = list(np.zeros(n)) # it starts with an empty subset # solution, u_val, iterations = modified_simulated_annealing(y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, b) # y_list_MODSIM.append(u_val) # iterations_list_MODSIM.append(iterations) """ ******* KNAPSACK ******** """ # print('knapsack') knapsack_utility, m, knap_sol = knapsack(y_p, y_f, Y_p, Y_f, rand_cost, b, rand_utility, n) y_list_KNAPSACK.append(knapsack_utility) """ ******* GREEDY ******** """ # print('greedy') greedy_solution, greedy_utility, greedy_cost, sorted_select_set = greedy_alg( y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, b) y_list_GREEDY.append(greedy_utility) """ ******* ACYCLIC GREEDY ******** """ # print('acyclic greedy') Acyclic_solution, Acyclic_utility, Acyclic_cost = acyclic_greedy_alg( funct_g, y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, b) y_list_ACYCLIC_GREEDY.append(Acyclic_utility) # convert lists into arrays to compute division y_array_MILP = np.array(y_list_MILP) y_array_SIM = np.array(y_list_SIM) y_array_MODSIM = np.array(y_list_MODSIM) y_array_KNAPSACK = np.array(y_list_KNAPSACK) y_array_GREEDY = np.array(y_list_GREEDY) y_array_ACYCLIC_GREEDY = np.array(y_list_ACYCLIC_GREEDY) max_utilities_array = np.array(max_utilities) # compute the percentage utility final_y_MILP = 100 * (y_array_MILP / max_utilities_array) final_y_SIM = 100 * (y_array_SIM / max_utilities_array) # final_y_MODSIM = 100 * (y_array_MODSIM / max_utilities_array) final_y_MODSIM = final_y_SIM final_y_KNAPSACK = 100 * (y_array_KNAPSACK / max_utilities_array) final_y_GREEDY = 100 * (y_array_GREEDY / max_utilities_array) final_y_ACYCLIC_GREEDY = 100 * (y_array_ACYCLIC_GREEDY / max_utilities_array) # convert array into list final_y_MILP = list(final_y_MILP) final_y_SIM = list(final_y_SIM) final_y_MODSIM = list(final_y_MODSIM) final_y_KNAPSACK = list(final_y_KNAPSACK) final_y_GREEDY = list(final_y_GREEDY) final_y_ACYCLIC_GREEDY = list(final_y_ACYCLIC_GREEDY) return x_list, final_y_MILP, final_y_SIM, final_y_MODSIM, final_y_KNAPSACK, final_y_GREEDY, final_y_ACYCLIC_GREEDY, iterations_list_SIM, iterations_list_MODSIM
def acyclic_greedy_alg(g_f, y_p, y_f, Y_p, Y_f, solution, cost_vector, u, B): # zero outdegree n = len(solution) current_utility = compute_utility(y_p, y_f, Y_p, Y_f, solution, u) current_cost=cost_function(solution, cost_vector) Selected_appliances = [] Selected_Super_nodes = [] final_sol = [0]*n g_f_reduced_2, Super_nodes = make_acyclic_scc(g_f) Cost_of_Super_nodes = Super_Costs(Super_nodes,cost_vector) Utility_of_Super_nodes = Super_Utilities(Super_nodes,u) L = len(Super_nodes) y_f2 = bsf_depend_vector_2(g_f_reduced_2, L) Y_f2 = successors_number(y_f2) # search_set = np.arange(L) G = copy.copy(g_f_reduced_2) current_solution = np.zeros(L) for i in range(L): out_d = list(G.out_degree()) # print('out_d = '+str(out_d)) zero_out_d = [] for i in range(len(out_d)): if out_d[i][1]==0: zero_out_d.append(out_d[i][0]) search_set = copy.copy(zero_out_d) # print('search_set = '+str(search_set)) select_set = list() A=list() for l in search_set: temp_solution = copy.copy(current_solution) temp_solution[l]=1 Potential_appliances = [] x_temp = [0]*n for k in range(L): if temp_solution[k]: Potential_appliances.extend(Super_nodes[list(Super_nodes.keys())[k]]) for k in range(n): if k in Potential_appliances: x_temp[k] = 1 ll = compute_utility(y_p, y_f, Y_p, Y_f, x_temp, u)/ (Cost_of_Super_nodes[l]) select_set.append((ll, l)) A=sorted(select_set, reverse=True) # print('A = '+str(A)) j = A[0][1] # print('j = '+str(j)) new_solution = copy.copy(current_solution) # new_solution = np.logical_or(new_solution, y_f2[j])*1 new_solution[j] = 1 Potential_appliances = [] x = [0]*n for k in range(L): if new_solution[k]: Potential_appliances.extend(Super_nodes[list(Super_nodes.keys())[k]]) for k in range(n): if k in Potential_appliances: x[k] = 1 new_utility = compute_utility(y_p, y_f, Y_p, Y_f, x, u) new_cost = cost_function(x, cost_vector) # b = np.array([j]) # search_set = np.setdiff1d(search_set,b) if new_cost <= B: # print('Yes! Picked '+str(Super_nodes[j])) current_solution = copy.copy(new_solution) # print([int(i) for i in current_solution]) current_utility = new_utility current_cost = new_cost final_sol = x G.remove_node(j) print('CODY \n'+ 'Utility: ' + str(current_utility))# + '\n' + 'Cost: ' + str(current_cost)) # return Super_nodes, Cost_of_Super_nodes, g_fr_inverse, Selected_appliances, Selected_Super_nodes return final_sol, current_utility, current_cost # n = 30 #nodes_number # p = 0.3 #probability # # random wattage energy list # rand_cost = random_energy(n) # # random utility array # rand_utility = random_utility(n) # # maximum utility of the current graph # current_max_utility = sum(rand_utility) # # for b in range_budget(): # # it creates one random graph # g = new_random_graph(n, p) # # this function create another graph (functional graph) from the graph g # funct_g, pref_g = two_graphs(g) # # compute the dependencies arrays # y_p = depend_vector(pref_g) # y_f = bsf_depend_vector(funct_g, n) # Y_p = successors_number(y_p) # Y_f = successors_number(y_f) # sol = list(np.zeros(n)) # B = 40000 # # current_solution, current_utility, current_cost, select_set = greedy_alg(y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, B) # # Appliances = list(np.where(current_solution)) # # Appliances = [x+1 for x in Appliances] # # print('Greedy Approach \n Solution: ' + str(current_solution) + '\n' + 'Utility: ' + str(current_utility) + '\n' + 'Cost: ' + str(current_cost)) # # print('Appliances: ' + str(Appliances)) # # # # Super_nodes, Cost_of_Super_nodes, g_fr_inverse, Selected_appliances,Selected_Super_nodes = acyclic_greedy_alg(funct_g, y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, B) # Acyclic_solution, Acyclic_utility, Acyclic_cost= acyclic_greedy_alg(funct_g, y_p, y_f, Y_p, Y_f, sol, rand_cost, rand_utility, B)
appliance_dict = {} appliance_index = 0 # add nodes for i in my_nodes_list: fg.add_node(i) pg.add_node(i) f_df = pd.read_csv('real_appliances/Dependency_Graph.csv', sep=',') # f_df = pd.read_csv('real_appliances/Dependency_Graph_with_fridge_freezer.csv', sep = ',') # p_df = pd.read_csv('real_appliances/preference_graph.csv', sep = ',') for i in f_df.iterrows(): x = i[1] fg.add_edge(x['Source'], x['Target']) Appliance_dict = {} for i in range(n): Appliance_dict[my_nodes_list[i]] = i func_g = nx.relabel_nodes(fg, Appliance_dict, copy=True) y_p = depend_vector(pg) y_f = bsf_depend_vector(func_g, n) Y_p = successors_number(y_p) Y_f = successors_number(y_f) nx.draw(func_g, with_labels=True) reduced, Super_nodes = make_acyclic_scc(fg) # print(str(Super_nodes))
def acyclic_greedy_alg_2(g_f, y_p, y_f, Y_p, Y_f, solution, cost_vector, u, B): n = len(solution) current_utility = compute_utility(y_p, y_f, Y_p, Y_f, solution, u) current_cost=cost_function(solution, cost_vector) Selected_appliances = [] Selected_Super_nodes = [] final_sol = [0]*n g_f_reduced_2, Super_nodes = make_acyclic_scc(g_f) Cost_of_Super_nodes = Super_Costs(Super_nodes,cost_vector) Utility_of_Super_nodes = Super_Utilities(Super_nodes,u) L = len(Super_nodes) y_f2 = bsf_depend_vector_2(g_f_reduced_2, L) Y_f2 = successors_number(y_f2) search_set = np.arange(L) current_solution = np.zeros(L) for i in range(L): select_set=list() A=list() for l in search_set: temp_solution = copy.copy(current_solution) temp_solution[l]=1 Potential_appliances = [] x_temp = [0]*n for k in range(L): if temp_solution[k]: Potential_appliances.extend(Super_nodes[list(Super_nodes.keys())[k]]) for k in range(n): if k in Potential_appliances: x_temp[k] = 1 # ll = compute_utility(y_p, y_f, Y_p, Y_f, x_temp, u)/ cost_function(x_temp, cost_vector) # ll = compute_utility(y_p, y_f, Y_p, Y_f, x_temp, u)/ Cost_of_Super_nodes[l] ll = compute_utility(y_p, y_f, Y_p, Y_f, x_temp, u)/ (Cost_of_Super_nodes[l]+sum(y_f2[l]*list(Cost_of_Super_nodes.values()))) # ll = compute_utility(y_p, y_f, Y_p, Y_f, x_temp, u) select_set.append((ll, l)) A=sorted(select_set, reverse=True) j = A[0][1] # print('j='+str(j)) new_solution = copy.copy(current_solution) new_solution = np.logical_or(new_solution, y_f2[j])*1 new_solution[j] = 1 Potential_appliances = [] x = [0]*n for k in range(L): if new_solution[k]: Potential_appliances.extend(Super_nodes[list(Super_nodes.keys())[k]]) for k in range(n): if k in Potential_appliances: x[k] = 1 new_utility = compute_utility(y_p, y_f, Y_p, Y_f, x, u) new_cost = cost_function(x, cost_vector) b = np.array([j]) search_set = np.setdiff1d(search_set,b) # print(search_set) # search_set = np.delete(search_set,j) # print(current_solution) if new_cost <= B: current_solution = copy.copy(new_solution) # print([int(i) for i in current_solution]) current_utility = new_utility current_cost = new_cost final_sol = x print('Acyclic Greedy Approach \n'+ 'Utility: ' + str(current_utility))# + '\n' + 'Cost: ' + str(current_cost)) # return Super_nodes, Cost_of_Super_nodes, g_fr_inverse, Selected_appliances, Selected_Super_nodes return final_sol, current_utility, current_cost