def read(self, cm_xml_path, cdis_xml_path):
     xmlTree = et.parse(cm_xml_path)
     xmlRoot = xmlTree.getroot()
     
     ceList = []
     
     for ceElement in xmlRoot.findall('ceList'):
         ceClientList = []
         
         for ceClientElement in ceElement.findall('CEClientList'):
             id = ceClientElement.find('id').text
             latitude = float(ceClientElement.find('latitude').text)
             longitude = float(ceClientElement.find('longitude').text)
             signal = int(ceClientElement.find('nivelSinal').text)
             interference = float(ceClientElement.find('interferenciaCocanal').text)
             
             ceClient = DataStructures.CEClient(id, DataStructures.GeoPoint(latitude, longitude), signal, interference)
             
             ceClientList.append(ceClient)
         
         id = ceElement.find('id').text
         antenna = int(ceElement.find('antena').text)
         channel = int(ceElement.find('device').find('canal').text)
         latitude = float(ceElement.find('latitude').text)
         longitude = float(ceElement.find('longitude').text)
         potency = int(ceElement.find('potencia').text)
         maxPotency = int(ceElement.find('potenciaMax').text)
         
         ce = DataStructures.CE(id, antenna, channel, DataStructures.GeoPoint(latitude, longitude), potency, maxPotency, ceClientList)
         ceList.append(ce)
     
     id = xmlRoot.find('id').text  
     self.cm = DataStructures.CM(id, ceList)
     
     channelList = []
     
     for channelElement in xmlRoot.findall('channels'):
         name = channelElement.find('nome').text
         number = int(channelElement.find('numCanal').text)
         frequency = float(channelElement.find('frequencia').text)
         state = DataStructures.ChannelState[channelElement.find('estado').text]
         
         channel = DataStructures.Channel(name, number, frequency, state)
         channelList.append(channel)
     
     self.channels = channelList 
     
     xmlTree = et.parse(cdis_xml_path)
     xmlRoot = xmlTree.getroot()
     
     cdisList = []
     
     for cdisElement in xmlRoot.findall('cdis'):
         cdisChannelList = []
         
         for channelElement in cdisElement.findall('canaisTvdb'):
             name = channelElement.find('nome').text
             number = int(channelElement.find('numCanal').text)
             frequency = float(channelElement.find('frequencia').text)
             state = DataStructures.ChannelState[channelElement.find('estado').text]
             
             channel = DataStructures.Channel(name, number, frequency, state)                
             cdisChannelList.append(channel)
             
         cdis = DataStructures.CDIS(cdisChannelList)
         cdisList.append(cdis)
             
     self.cdisList = cdisList
def main(cm_file, cdis_file, output_dir):
    #------------------------------ BEGIN LOADING ------------------------------#

    print(">>>: Carregando cenário")
    scenario = Scenario.Scenario()
    scenario.read(cm_file, cdis_file)

    scenario_name = os.path.splitext(os.path.basename(cm_file))[0]
    cdis_info_name = os.path.splitext(os.path.basename(cdis_file))[0]

    scenario_path = os.path.join(output_dir, scenario_name + cdis_info_name)

    result_log_path = os.path.join(scenario_path, "result_log.csv")
    result_fig_path = os.path.join(scenario_path, "figures")
    result_res_path = os.path.join(scenario_path, "results")

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    if not os.path.exists(scenario_path):
        os.makedirs(scenario_path)
    else:
        shutil.rmtree(scenario_path)
        os.makedirs(scenario_path)

    if not os.path.exists(result_fig_path):
        os.makedirs(result_fig_path)

    if not os.path.exists(result_res_path):
        os.makedirs(result_res_path)

    Visualization.visualize(scenario, scenario.cm.ceList, result_fig_path,
                            "00")

    #------------------------------ END LOADING ------------------------------#

    with open(result_log_path, "w+") as resultLogFile:
        resultLogFile.write(
            "Data_e_Hora;Nome_do_cenário;Caminho_do_arquivo_do_cenário;Nome_das_informações_do_CDIS;Caminho_do_arquivo_das_informações_do_cdis;Tempo_de_execução\n"
        )
        resultLogFile.write(datetime.now().strftime("%d/%m/%Y %H:%M:%S") +
                            ";" + scenario_name + ";" + cm_file + ";" +
                            cdis_info_name + ";" + cdis_file)

        startTime = time.time()
        totalIteration = len(scenario.cdisList)

        for iteration in range(0, totalIteration):
            print("\n>>>: Resolvendo cenário com informações do CDIS=" +
                  str(iteration) + "/" + str(totalIteration))
            scenario.updateChannels(scenario.cdisList[iteration].channelList)

            #------------------------------ BEGIN PREPROCESSING ------------------------------#

            print(">>>: Pré-processando entrada")
            (allCEVarList, ceVarList, ceByChannelVarList,
             interferenceList) = PreProcessing.process(scenario)

            #------------------------------ END PREPROCESSING ------------------------------#

            #------------------------------ BEGIN SOLVER ------------------------------#

            print(">>>: Criando modelo")
            model = gb.Model('cognitive-networks')

            print(">>>: Adicionando variáveis ao modelo")
            allCEModelVarList = []

            for ceVar in allCEVarList:
                allCEModelVarList.append(
                    model.addVar(name=ceVar.name, vtype=gb.GRB.BINARY))

            ceByChannelModelVarList = []

            for ceByChannelVar in ceByChannelVarList:
                ceByChannelModelVarList.append(
                    model.addVar(name=ceByChannelVar.name))

            model.update()

            ceModelVarList = []

            for ceVars in ceVarList:
                modelVarList = []

                for ceVar in ceVars:
                    modelVarList.append(model.getVarByName(ceVar.name))

                ceModelVarList.append(modelVarList)

            print(">>>: Adicionando restrições ao modelo")
            ceId = 0

            for ceModelVars in ceModelVarList:
                model.addConstr(gb.quicksum(ceModelVars), gb.GRB.EQUAL, 1,
                                "Única_configuração_para_CE_" + str(ceId))

                ceId += 1

            interferenceModelVarList = []

            for interference in interferenceList:
                ceVar = interference[0]
                ceTotalInterference = interference[1]
                ceInterferenceList = interference[2]

                if (ceTotalInterference > 0):
                    interferenceModelVar = model.addVar(
                        name="Interferência-devido-" + ceVar.name)
                    interferenceModelVarList.append(interferenceModelVar)
                    model.update()

                    ceInterferenceModelVarList = []

                    for ceInterference in ceInterferenceList:
                        ceInterferenceModelVarList.append(
                            ceInterference * model.getVarByName(ceVar.name))

                    model.addConstr(
                        gb.quicksum(ceInterferenceModelVarList), gb.GRB.EQUAL,
                        interferenceModelVar,
                        "Interferência_provocada_por_" + ceVar.name)

                    model.addConstr(
                        interferenceModelVar, gb.GRB.LESS_EQUAL,
                        args.max_interference,
                        "Máximo_de_interferência_tolerada_de_" + ceVar.name)

            for ceByChannelModelVar in ceByChannelModelVarList:
                ceByChannelVarNameSplit = ceByChannelModelVar.varName.split(
                    '_')

                channel = int(ceByChannelVarNameSplit[3])

                filtredCEModelVarList = PreProcessing.filterCEByChannelModelVar(
                    allCEModelVarList, channel)

                model.addConstr(gb.quicksum(filtredCEModelVarList),
                                gb.GRB.EQUAL, ceByChannelModelVar,
                                "Qtd_de_CE_no_canal_" + str(channel))

                model.addConstr(
                    ceByChannelModelVar, gb.GRB.LESS_EQUAL,
                    min(len(scenario.cm.ceList),
                        ((len(scenario.cm.ceList) /
                          PreProcessing.countAvailableChannels(scenario)) +
                         1)), "Máximo_de_CEs_no_canal_" + str(channel))

            ceId = 0

            for ceModelVars in ceModelVarList:
                potencyList = []

                for ceModelVar in ceModelVars:
                    ceModelVarNameSplit = ceModelVar.varName.split("_")
                    cePotency = int(ceModelVarNameSplit[3])
                    potencyList.append(cePotency * ceModelVar)

                model.addConstr(
                    gb.quicksum(potencyList), gb.GRB.GREATER_EQUAL,
                    args.min_potency,
                    "Mínimo_de_potência_para_máxima_cobertura_do_CE_" +
                    str(ceId))

                ceId += 1

            print(">>>: Definindo a função objetivo")
            model.setObjective(gb.quicksum(interferenceModelVarList),
                               gb.GRB.MINIMIZE)

            model.write(
                os.path.join(result_res_path,
                             "model_it_" + str(iteration) + ".lp"))
            print(">>>: Modelo salvo")

            print(">>>: Otimizando modelo")
            model.optimize()

            resultCEVarList = []

            with open(
                    os.path.join(result_res_path,
                                 "it_" + str(iteration) + ".txt"),
                    "w") as resultFile:
                if (model.status == gb.GRB.Status.OPTIMAL):
                    resultFile.write(">>>: Resultado ótimo:\n")
                    print(">>>: Resultado ótimo:")

                    for ceModelVar in allCEModelVarList:
                        if (ceModelVar.x == 1.0):
                            resultCEVarList.append(ceModelVar.varName)
                            resultFile.write("%s\n" % ceModelVar.varName)
                            print("%s" % ceModelVar.varName)

                    for interferenceModelVar in interferenceModelVarList:
                        ceModelVar = model.getVarByName(
                            interferenceModelVar.varName.split("-")[2])

                        if ((ceModelVar.x == 1.0)
                                and (interferenceModelVar.x > 0.0)):
                            resultFile.write("%s %s\n" %
                                             (interferenceModelVar.varName,
                                              interferenceModelVar.x))
                            print("%s %s" % (interferenceModelVar.varName,
                                             interferenceModelVar.x))

                    for ceByChannelModelVar in ceByChannelModelVarList:
                        resultFile.write("%s %s\n" %
                                         (ceByChannelModelVar.varName,
                                          ceByChannelModelVar.x))
                        print("%s %s" % (ceByChannelModelVar.varName,
                                         ceByChannelModelVar.x))
                elif (model.status == gb.GRB.Status.INFEASIBLE):
                    resultFile.write(">>>: O modelo é inviável!\n")
                    print(">>>: O modelo é inviável!")

                    print(">>>: Computando IIS")
                    model.computeIIS()

                    resultFile.write(
                        "\n>>>: As restrições a seguir não foram satisfeitas:\n"
                    )
                    print(">>>: As restrições a seguir não foram satisfeitas:")
                    for c in model.getConstrs():
                        if c.IISConstr:
                            resultFile.write("%s\n" % c.constrName)
                            print("%s" % c.constrName)

                    print(">>>: Otimizando modelo relaxado")
                    model.feasRelaxS(0, False, False, True)
                    model.optimize()

                    if (model.status == gb.GRB.Status.OPTIMAL):
                        resultFile.write(
                            "\n>>>: Resultado ótimo do modelo relaxado:\n")
                        print(">>>: Resultado ótimo do modelo relaxado:")

                        for ceModelVar in allCEModelVarList:
                            if (ceModelVar.x == 1.0):
                                resultCEVarList.append(ceModelVar.varName)
                                resultFile.write("%s\n" % ceModelVar.varName)
                                print("%s" % ceModelVar.varName)

                        for interferenceModelVar in interferenceModelVarList:
                            ceModelVar = model.getVarByName(
                                interferenceModelVar.varName.split("-")[2])

                            if ((ceModelVar.x == 1.0)
                                    and (interferenceModelVar.x > 0.0)):
                                resultFile.write("%s %s\n" %
                                                 (interferenceModelVar.varName,
                                                  interferenceModelVar.x))
                                print("%s %s" % (interferenceModelVar.varName,
                                                 interferenceModelVar.x))

                        for ceByChannelModelVar in ceByChannelModelVarList:
                            resultFile.write("%s %s\n" %
                                             (ceByChannelModelVar.varName,
                                              ceByChannelModelVar.x))
                            print("%s %s" % (ceByChannelModelVar.varName,
                                             ceByChannelModelVar.x))
                    elif (model.status in (gb.GRB.Status.INF_OR_UNBD,
                                           gb.GRB.Status.UNBOUNDED,
                                           gb.GRB.Status.INFEASIBLE)):
                        print(
                            ">>>: O modelo relaxado não pode ser resolvido porque é ilimitado ou inviável"
                        )
                    else:
                        resultFile.write(
                            ">>>: A otimização parou com status: %d\n" %
                            model.status)
                        print(">>>: A otimização parou com status: %d" %
                              model.status)
                elif (model.status == gb.GRB.Status.UNBOUNDED):
                    resultFile.write(
                        ">>>: O modelo não pode ser resolvido porque é ilimitado\n"
                    )
                    print(
                        ">>>: O modelo não pode ser resolvido porque é ilimitado"
                    )
                else:
                    resultFile.write(
                        ">>>: A otimização parou com status: %d\n" %
                        model.status)
                    print(">>>: A otimização parou com status: %d" %
                          model.status)

            resultCEList = []

            for resultCEVar in resultCEVarList:
                ceVarNameSplit = resultCEVar.split('_')

                ceId = int(ceVarNameSplit[1])
                resultCEChannelNumber = int(ceVarNameSplit[2])
                resultCEPotency = int(ceVarNameSplit[3])

                ce = scenario.cm.ceList[ceId]

                resultCEList.append(
                    DataStructures.CE(ceId, ce.antenna, resultCEChannelNumber,
                                      ce.geoPoint, resultCEPotency,
                                      ce.maxPotency, ce.clientList))

            #------------------------------ END SOLVER ------------------------------#

            #------------------------------ BEGIN VISUALIZATION ------------------------------#

            if (len(resultCEVarList) > 0):
                Visualization.visualize(scenario, resultCEList,
                                        result_fig_path, str(iteration))

            #------------------------------ END VISUALIZATION ------------------------------#

        resultLogFile.write(";" + str((time.time() - startTime)))