• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlanticwave-sdx / pce / 3680947894

pending completion
3680947894

Pull #81

github

GitHub
Merge a6cd61918 into b40be3bdd
Pull Request #81: Updates coveralls.io configuration

845 of 1796 relevant lines covered (47.05%)

0.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.18
/src/LoadBalancing/MC_Solver.py
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
1✔
4
Created on Mon Mar  7 13:42:31 2022
5

6
@author: yifeiwang
7
"""
8

9
import json
1✔
10

11
from ortools.linear_solver import pywraplp
1✔
12

13

14
def create_data_model(graph):
1✔
15
    data = {}
1✔
16
    data["constraint_coeffs"] = graph["constraint_coeffs"]
1✔
17

18
    data["bounds"] = graph["bounds"]
1✔
19

20
    data["obj_coeffs"] = graph["obj_coeffs"]
1✔
21
    data["num_vars"] = graph["num_vars"]
1✔
22
    data["num_constraints"] = graph["num_constraints"]
1✔
23
    num_inequality = graph["num_inequality"]
1✔
24

25
    return (data, num_inequality)
1✔
26

27

28
def split(list_a, chunk_size):
1✔
29
    for i in range(0, len(list_a), chunk_size):
1✔
30
        yield list_a[i : i + chunk_size]
1✔
31

32

33
def pathlookup(solution, linknum, linklist):
1✔
34
    with open(linklist) as f:
×
35
        linklist = json.load(f)
×
36

37
    linkindex = []
×
38
    for i in range(len(solution)):
×
39
        if abs(solution[int(i)] - 1) < 0.01:
×
40
            linkindex.append(i)
×
41
    linkselection = []
×
42

43
    for i in linkindex:
×
44
        linkselection.append(linklist[int(i)])
×
45
    return (linkselection, linkindex)
×
46

47

48
def solutiontranslator(solution):
1✔
49
    output = []
×
50
    c = 0
×
51
    for i in solution:
×
52
        if abs(i - 1) <= 0.001:
×
53
            output.append(c)
×
54
        c += 1
×
55
    return output
×
56

57

58
def solution_translator(solution, linklistname):
1✔
59
    with open(linklistname) as f:
1✔
60
        linklist = json.load(f)
1✔
61
    link_num = len(linklist)
1✔
62

63
    solution_list = list(split(solution, link_num))
1✔
64
    path_list = {}
1✔
65
    c = 1
1✔
66
    for request in solution_list:
1✔
67
        individual_solution = []
1✔
68
        i = 0
1✔
69

70
        for solution in request:
1✔
71
            if abs(solution - 1) < 0.01:
1✔
72

73
                individual_solution.append(linklist[i])
1✔
74
            i += 1
1✔
75

76
        path_list[str(c)] = individual_solution
1✔
77
        c += 1
1✔
78
    ordered_path_list = pathordering(path_list)
1✔
79
    return ordered_path_list
1✔
80

81

82
def pathordering(path_list):
1✔
83
    ordered_path_list = {}
1✔
84
    source_list = []
1✔
85
    c = 0
1✔
86
    with open("./tests/data/connection.json") as f:
1✔
87
        query_list = json.load(f)
1✔
88
    for query in query_list:
1✔
89
        source_list.append(query[0])
1✔
90

91
    for query in path_list:
1✔
92
        source_node = source_list[c]
1✔
93
        ordered_path_list[query] = []
1✔
94
        while len(path_list[query]) != 0:
1✔
95
            for path in path_list[query]:
1✔
96
                if path[0] == source_node:
1✔
97
                    ordered_path_list[query].append(path)
1✔
98
                    source_node = path[1]
1✔
99
                    path_list[query].remove(path)
1✔
100
        c += 1
1✔
101
    return ordered_path_list
1✔
102

103

104
def MC_Solver(data):
1✔
105
    graph = create_data_model(data)
1✔
106
    data = graph[0]
1✔
107
    num_inequality = graph[1]
1✔
108

109
    # Create the mip solver with the SCIP backend.
110
    solver = pywraplp.Solver.CreateSolver("SCIP")
1✔
111

112
    x = {}
1✔
113
    for j in range(data["num_vars"]):
1✔
114
        x[j] = solver.IntVar(0, 1, "x[%i]" % j)
1✔
115
    print("Number of variables =", solver.NumVariables())
1✔
116

117
    for i in range(data["num_constraints"] - num_inequality):
1✔
118
        constraint_expr = [
1✔
119
            data["constraint_coeffs"][i][j] * x[j] for j in range(data["num_vars"])
120
        ]
121
        solver.Add(sum(constraint_expr) == data["bounds"][i])
1✔
122
    for i in range(data["num_constraints"] - num_inequality, data["num_constraints"]):
1✔
123
        constraint_expr = [
1✔
124
            data["constraint_coeffs"][i][j] * x[j] for j in range(data["num_vars"])
125
        ]
126
        solver.Add(sum(constraint_expr) <= data["bounds"][i])
1✔
127
    print("Number of constraints =", solver.NumConstraints())
1✔
128

129
    objective = solver.Objective()
1✔
130
    for j in range(data["num_vars"]):
1✔
131
        objective.SetCoefficient(x[j], data["obj_coeffs"][j])
1✔
132
    objective.SetMinimization()
1✔
133

134
    status = solver.Solve()
1✔
135
    solution = []
1✔
136

137
    if status == pywraplp.Solver.OPTIMAL:
1✔
138
        print("Objective value =", solver.Objective().Value())
1✔
139
        for j in range(data["num_vars"]):
1✔
140
            # print(x[j].name(), ' = ', x[j].solution_value())
141
            solution.append(x[j].solution_value())
1✔
142
        print()
1✔
143
        # print('Problem solved in %f milliseconds' % solver.wall_time())
144
        # print('Problem solved in %d iterations' % solver.iterations())
145
        # print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
146
    else:
147
        print("The problem does not have an optimal solution.")
×
148

149
    return solution, solver.Objective().Value()
1✔
150

151

152
def runMC_Solver():
1✔
153
    with open("./tests/data/LB_data.json") as f:
1✔
154
        data = json.load(f)
1✔
155

156
    output = MC_Solver(data)
1✔
157
    solution = output[0]
1✔
158
    objective = output[1]
1✔
159
    # print(solution_translator(solution,'../test/data/LB_linklist.json'), objective)
160

161
    return [solution_translator(solution, "./tests/data/LB_linklist.json"), objective]
1✔
162

163

164
# print(runMC_Solver())
165

166
# sol = ({1: [[1, 24], [24, 11]], 2: [[3, 22], [22, 23], [23, 12], [12, 7], [7, 17], [17, 18]], 3: [[2, 20], [20, 17], [17, 9], [9, 13]]}, 24789877.0)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc