1 # -------------------------------------------------------------------------
2 # Copyright (c) 2018 AT&T Intellectual Property
3 # Copyright (C) 2020 Wipro Limited.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # -------------------------------------------------------------------------
20 from collections import defaultdict
25 from apps.pci.optimizers.solver.ml_model import MlModel
26 from apps.pci.optimizers.solver.pci_utils import get_id
27 from apps.pci.optimizers.solver.pci_utils import mapping
28 from osdf.config.base import osdf_config
30 BASE_DIR = os.path.dirname(__file__)
31 cell_id_mapping = dict()
32 id_cell_mapping = dict()
35 def pci_optimize(network_cell_info, cell_info_list, request_json):
36 global cell_id_mapping, id_cell_mapping
37 cell_id_mapping, id_cell_mapping = mapping(network_cell_info)
38 original_pcis = get_original_pci_list(network_cell_info)
39 unchangeable_pcis = get_ids_of_fixed_pci_cells(request_json['cellInfo'].get('fixedPCICells', []))
40 neighbor_edges = get_neighbor_list(network_cell_info)
41 second_level_edges = get_second_level_neighbor(network_cell_info)
42 ignorable_links = get_ignorable_links(network_cell_info, request_json)
43 anr_flag = is_anr(request_json)
45 dzn_data = build_dzn_data(cell_info_list, ignorable_links, neighbor_edges, second_level_edges, anr_flag,
46 original_pcis, unchangeable_pcis)
48 ml_enabled = osdf_config.core['PCI']['ml_enabled']
50 MlModel().get_additional_inputs(dzn_data, network_cell_info)
52 return build_pci_solution(dzn_data, ignorable_links, anr_flag)
55 def get_ids_of_fixed_pci_cells(fixed_pci_list):
57 for cell in fixed_pci_list:
58 fixed_pci_ids.add(cell_id_mapping[cell])
62 def get_cell_id_pci_mapping(network_cell_info):
63 original_pcis = dict()
64 for cell in network_cell_info['cell_list']:
65 for nbr in cell['nbr_list']:
66 if cell_id_mapping[nbr['targetCellId']] not in original_pcis:
67 original_pcis[cell_id_mapping[nbr['targetCellId']]] = nbr['pciValue']
71 def get_original_pci_list(network_cell_info):
72 cell_id_pci_mapping = get_cell_id_pci_mapping(network_cell_info)
73 original_pcis_list = []
74 for i in range(len(cell_id_pci_mapping)):
75 original_pcis_list.append(cell_id_pci_mapping.get(i))
76 return original_pcis_list
79 def build_pci_solution(dzn_data, ignorable_links, anr_flag):
80 mzn_solution = solve(get_mzn_model(anr_flag), dzn_data)
81 if mzn_solution == 'UNSATISFIABLE':
83 solution = {'pci': mzn_solution[0]['pci']}
86 removables = defaultdict(list)
87 used_ignorables = mzn_solution[0]['used_ignorables']
89 for i in ignorable_links:
90 if used_ignorables[index] > 0:
91 removables[i[0]].append(i[1])
93 solution['removables'] = removables
97 def build_dzn_data(cell_info_list, ignorable_links, neighbor_edges, second_level_edges, anr_flag, original_pcis,
100 'NUM_NODES': len(cell_info_list),
101 'NUM_PCIS': len(cell_info_list),
102 'NUM_NEIGHBORS': len(neighbor_edges),
103 'NEIGHBORS': get_list(neighbor_edges),
104 'NUM_SECOND_LEVEL_NEIGHBORS': len(second_level_edges),
105 'SECOND_LEVEL_NEIGHBORS': get_list(second_level_edges),
106 'PCI_UNCHANGEABLE_CELLS': unchangeable_pcis,
107 'ORIGINAL_PCIS': original_pcis
110 dzn_data['NUM_IGNORABLE_NEIGHBOR_LINKS'] = len(ignorable_links)
111 dzn_data['IGNORABLE_NEIGHBOR_LINKS'] = get_list(ignorable_links)
115 def get_mzn_model(anr_flag):
117 mzn_model = os.path.join(BASE_DIR, 'min_confusion_inl.mzn')
119 mzn_model = os.path.join(BASE_DIR, 'no_conflicts_no_confusion.mzn')
123 def is_anr(request_json):
124 return 'pci-anr' in request_json["requestInfo"]["optimizers"]
127 def get_list(edge_list):
130 array_list.append([s[0], s[1]])
131 return sorted(array_list)
134 def solve(mzn_model, dzn_data):
135 return pymzn.minizinc(mzn=mzn_model, data=dzn_data)
138 def get_neighbor_list(network_cell_info):
139 neighbor_list = set()
140 for cell in network_cell_info['cell_list']:
141 add_to_neighbor_list(network_cell_info, cell, neighbor_list)
145 def add_to_neighbor_list(network_cell_info, cell, neighbor_list):
146 for nbr in cell.get('nbr_list', []):
148 nbr_id = get_id(network_cell_info, nbr['targetCellId'])
149 if nbr_id and host_id != nbr_id:
150 neighbor_list.add((host_id, nbr_id))
153 def get_second_level_neighbor(network_cell_info):
154 second_neighbor_list = set()
155 for cell in network_cell_info['cell_list']:
156 comb_list = build_second_level_list(network_cell_info, cell)
157 for comb in comb_list:
158 if comb[0] and comb[1]:
159 second_neighbor_list.add((comb[0], comb[1]))
160 return sorted(second_neighbor_list)
163 def build_second_level_list(network_cell_info, cell):
165 for nbr in cell.get('nbr_list', []):
166 second_nbr_list.append(get_id(network_cell_info, nbr['targetCellId']))
167 return [list(elem) for elem in list(itertools.combinations(second_nbr_list, 2))]
170 def get_ignorable_links(network_cell_info, request_json):
171 ignorable_list = set()
172 anr_input_list = request_json["cellInfo"].get('anrInputList', [])
174 for anr_info in anr_input_list:
175 cell_id = get_id(network_cell_info, anr_info['cellId'])
176 anr_removable = anr_info.get('removeableNeighbors', [])
177 for anr in anr_removable:
178 ignorable_list.add((cell_id, get_id(network_cell_info, anr)))
179 return ignorable_list