Пример #1
0
class Controller:
    NetColony: Colony
    ControllerLogger: Logger

    def __init__(self, FileName: string):
        self.NetColony = Colony(0.5, 0.5, 0.01)
        self.ControllerLogger = Logger(FileName)
        self.ControllerLogger.Log(
            "Source ;Destination ;Trust ;MessageNumber ;Pheromone ;Prediction ;Decision; Algorithm\n"
        )

    def HandleMessage(self, Source: string, Destination: string,
                      Prediction: float):
        Heuristic = random.randint(0, 100)
        ColonyDecision = self.NetColony.HandleMessage(Source, Destination)
        if Heuristic < 80:
            if ColonyDecision:
                Link = self.NetColony.FindEdge(Source, Destination)
                MessageNumber = Link.MessageNumber
                Pheromone = Link.Pheromone
                AntSource = self.NetColony.GetAnt(Source)
                Trust = AntSource.GetTrustLevel()
                Decision = self.NetColony.AuthoriseMessage(Source, Destination)
                print("Decision Colony with prediction = " + str(Prediction))
                self.ControllerLogger.Log(Source + ";" + Destination + ";" +
                                          str(Trust) + ";" +
                                          str(MessageNumber) + ";" +
                                          str(Pheromone) + ";" +
                                          str(Prediction) + ";" +
                                          str(Decision) + ";" + "Colony \n")
            else:
                Link = self.NetColony.FindEdge(Source, Destination)
                MessageNumber = Link.MessageNumber
                Pheromone = Link.Pheromone
                AntSource = self.NetColony.GetAnt(Source)
                Trust = AntSource.GetTrustLevel()
                self.NetColony.UpdatePrediction(Source, Destination,
                                                Prediction)
                print("Decision ML with prediction = " + str(Prediction))
                self.ControllerLogger.Log(Source + ";" + Destination + ";" +
                                          str(Trust) + ";" +
                                          str(MessageNumber) + ";" +
                                          str(Pheromone) + ";" +
                                          str(Prediction) + ";" + str(0) +
                                          ";" + "ML \n")
        else:
            #self.NetColony.HandleMessage(Source,Destination)
            self.NetColony.UpdatePrediction(Source, Destination, Prediction)
            print("Decision ML with prediction = " + str(Prediction))
            Link = self.NetColony.FindEdge(Source, Destination)
            MessageNumber = Link.MessageNumber
            Pheromone = Link.Pheromone
            AntSource = self.NetColony.GetAnt(Source)
            Trust = AntSource.GetTrustLevel()
            self.ControllerLogger.Log(Source + ";" + Destination + ";" +
                                      str(Trust) + ";" + str(MessageNumber) +
                                      ";" + str(Pheromone) + ";" +
                                      str(Prediction) + ";" + str(0) + ";" +
                                      "ML \n")
Пример #2
0
def MOACO(totalPopulation, objFuncs):

    colonies = [
        Colony(int(totalPopulation / objFuncs.__len__()), objFunc,
               pokPreFilter) for objFunc in objFuncs
    ]
    coopCandSet = colonyCooperation(
        [colony.candidateSet() for colony in colonies], objFuncs)

    prevCandSet = coopCandSet
    iters = 26
    for i in range(0, iters):
        jointFun = lambda team: attackObjFun(team) * selfCoverageFun(team)
        a = sorted(list(map(jointFun, prevCandSet)))
        b = np.array(a)
        plt.scatter(range(0, b.size), b)
        plt.title("Iter: " + str(i) + " - rho: " + str(glob_var.rho) +
                  " - Q: " + str(glob_var.Q))
        plt.show()
        for colony in colonies:
            colony.updatePhCon(prevCandSet)
            colony.updatePokProb()
            colony.ACO()
        currentCandSet = deepcopy(
            colonyCooperation([colony.candidateSet() for colony in colonies],
                              objFuncs))
        prevCandSet = deepcopy(
            colonyCooperation([prevCandSet, currentCandSet], objFuncs))

    print(currentCandSet[0])
Пример #3
0
 def set_intel_empty(self):
     """reset all data in the IntelManager"""
     self.map = abathur_pb2.AbathurMap() #TODO this is an empty datastructe atm
     self.score = score_pb2.Score()
     self.common = sc2api_pb2.PlayerCommon()
     self.handler = CaseHandler.CaseHandler()
     self.upgrades_self = []
     self.buildings_self = {}            # uint -> intelUnit
     self.units_self = {}                # uint -> intelUnit
     self.workers_self = {}              # uint -> intelUnit
     self.destructibles = {}             # uint -> intelUnit
     self.structures_enemy = {}          # uint -> intelUnit
     self.units_enemy = {}                # uint -> intelUnit
     self.workers_enemy = {}             # uint -> intelUnit
     self.primary_colony = Colony()
     self.colonies = []
     self.mineral_fields = {}            # uint -> intelUnit
     self.vespene_geysers = {}           # uint -> intelUnit
     self.production_queue = []
Пример #4
0
 def __init__(self):
     self.map = abathur_pb2.AbathurMap()  # TODO this is an empty datastructe atm
     self.score = score_pb2.Score()
     self.common = sc2api_pb2.PlayerCommon()
     self.handler = CaseHandler.CaseHandler() #TODO not being used yet.
     self.upgrades_self = []
     self.buildings_self = {}  # uint -> intelUnit
     self.units_self = {}  # uint -> intelUnit
     self.workers_self = {}  # uint -> intelUnit
     self.destructibles = {}  # uint -> intelUnit
     self.structures_enemy = {}  # uint -> intelUnit
     self.units_enemy = {}  # uint -> intelUnit
     self.workers_enemy = {}  # uint -> intelUnit
     self.primary_colony = Colony()
     self.colonies = []
     self.mineral_fields = {}  # uint -> intelUnit
     self.vespene_geysers = {}  # uint -> intelUnit
     self.production_queue = []
     self.squad_repository = SquadRepository()
Пример #5
0
def colCreate(colCreateParms):

    ''' colCreateParms list:
    nColonies, nAnts, nCities are number of each.
    alpha_min, alpha_max are alpha parameter constraints
    beta and Q are ant parameters
    initMethodFlag controls the alpha weight calculation method used
    in alphaWeightInit function.
    nBins is the number of bins created for alpha assignments
    '''

    (nColonies, nAnts, nCities, alpha_min, alpha_max,
         beta, Q, initMethodFlag, nBins) = colCreateParms

    # Initialize list of colonies
    col_list = []

    
    for i in range(nColonies):
        # Initialize ant list
        my_ants = []

        # Create new ants
        for j in range(nAnts):
            # Determine the initial city
            init_city = random.randint(0, nCities-1)
            # Define dummy alpha for the individual
            alpha = 0
            my_ants.append( Ant(nCities,alpha, beta, Q, init_city) )

        # Create colony class
        newColony = Colony(my_ants)
        # Set alpha_weights
        alpha_weights = alphaWeightInit(nBins, initMethodFlag)
        newColony.setAlphaWeights(alpha_weights)
        # Assign real alpha values
        alphaAssign(newColony, alpha_min, alpha_max)

        col_list.append(newColony)


    return col_list
Пример #6
0
    def __init__(self):
        self.mean_scores_ = []
        self.pop_ = []
        self.scores_ = np.array([0 for x in range(100)])
        self.dna_pool_ = [0 for x in range(100)]

        for i in range(100):
            self.pop_.append(
                Colony(dna=gen_rand_dna(), size=(15, 15), quantity=49))
        for i in range(100):
            self.dna_pool_[i] = self.pop_[i].get_dna()
Пример #7
0
def runAlgo(params):
    data = []
    qStep = params[0]
    rhoStep = params[1]
    glob_var.Q = (qStep / 20) * 1000
    glob_var.rho = rhoStep / 20
    print(params)
    Q = glob_var.Q
    rho = glob_var.rho

    with open("Pok.pkl", "rb") as f:
        pokPreFilter = pickle.load(f)
    with open("Moves.pkl", "rb") as f:
        moves = pickle.load(f)
    objFunctionOne = lambda team: sum(list(
        map(lambda x: currentPower(pokPreFilter[x[0]], getLearnedMoves(pokPreFilter, x, [x[1], x[2], x[3], x[4]])),
            team)))
    firstColony = Colony(500, objFunctionOne, pokPreFilter)
    prevCandSet = firstColony.candidateSet()
    for i in range(1, 26):
        a = list(map(objFunctionOne, prevCandSet))
        dataCandSet = [Q] + [rho] + [i] + a
        data += [dataCandSet]
        # b = np.array(a)
        # plt.scatter(range(0, b.size), b)
        # plt.title("Iter: "+ str(i) + " - rho: "+str(rho)+ " - Q: "+str(Q))
        # plt.show()
        currentCandSet = firstColony.candidateSet()
        updateSet = deepcopy(prevCandSet) + deepcopy(currentCandSet)
        candSetTemp = list(sorted(updateSet, key=firstColony.fitness))
        candSet = candSetTemp[int(candSetTemp.__len__() / 2):candSetTemp.__len__()]
        prevCandSet = candSet
        firstColony.updatePhCon(candSet)
        firstColony.updatePokProb()
        firstColony.ACO()
    return data
Пример #8
0
 def __kmeans_clustering(self):
     # get the whole population without fitness value, then flat it
     rdd_aux = self.__rdd.flatMap(lambda x: x.get_population(fitness=False))
     # train the kmeans
     kmeans_cluster = KMeans.train(
         rdd_aux,
         self.__colonies,
         maxIterations=self.__cluster_iterations,
         initializationMode="random")  # acepta "k-means||"
     # create a new rdd with the labels
     rdd_labels = kmeans_cluster.predict(rdd_aux)
     # zip each result with its class
     rdd_aux = rdd_labels.zip(rdd_aux)
     # input serialization
     cols = self.__colonies
     self.__sc.broadcast(cols)
     # divide into partitions
     rdd_aux = rdd_aux.partitionBy(cols, partitionFunc=lambda x: x).glom()
     # remove the index of each element
     rdd_aux = rdd_aux.map(lambda x: [y[1] for y in x])
     # input serialization
     evaluation = self.__evaluation
     generation = self.__generation
     cross = self.__cross
     mutation = self.__mutation
     selection = self.__selection
     survival = self.__survival
     mut_ratio = self.__mut_ratio
     survival_ratio = self.__survival_ratio
     control_obj = self.__control_obj
     # create the new colonies
     self.__rdd = rdd_aux.map(
         lambda x: Colony(evaluation,
                          generation,
                          cross=cross,
                          mutation=mutation,
                          selection=selection,
                          mut_ratio=mut_ratio,
                          survival_ratio=survival_ratio,
                          survival=survival,
                          control_obj=control_obj,
                          population=x))
Пример #9
0
 def populate(self,
              evaluation,
              generation,
              population,
              colonies,
              cross='singlepoint',
              mutation='binary',
              selection='roulette',
              survival='roulette',
              migration=10,
              mut_ratio=0.05,
              survival_ratio=0.2,
              control_obj=None):
     # stored for clustering methods
     self.__evaluation = evaluation
     self.__generation = generation
     self.__population = population
     self.__colonies = colonies
     self.__cross = cross
     self.__mutation = mutation
     self.__selection = selection
     self.__survival = survival
     self.__mut_ratio = mut_ratio
     self.__survival_ratio = survival_ratio
     self.__control_obj = control_obj
     self.__migration = migration
     if control_obj:
         self.__sc.broadcast(control_obj)
     # creates a rdd with a given number of colonies and individuals
     self.__rdd = self.__sc.parallelize([
         Colony(evaluation,
                generation,
                cross=cross,
                mutation=mutation,
                selection=selection,
                mut_ratio=mut_ratio,
                survival_ratio=survival_ratio,
                survival=survival,
                control_obj=control_obj) for _ in range(colonies)
     ])
     self.__rdd = self.__rdd.map(lambda x: x.populate(population))
Пример #10
0
    def update_intel(self, intel):
        """
        :param intel: an intel response object containing all requested data
        :return: None
        """
        if intel.HasField("map"):
            self.map.CopyFrom(intel.map)  # TODO this is an empty datastructure atm
        if intel.HasField("score"):
            self.score.CopyFrom(intel.score)
        if intel.HasField("common"):
            self.common.CopyFrom(intel.common)
        if len(intel.upgrades_self) > 0:
            self.upgrades_self = intel.upgrades_self

        self.game_loop = intel.game_loop

        for building in intel.buildings_self:
            self.buildings_self.update({building.tag: IntelUnit(building, self.game_loop)})

        for unit in intel.units_self:
            self.units_self.update({unit.tag: IntelUnit(unit, self.game_loop)})

        for worker in intel.workers_self:
            self.workers_self.update({worker.tag: IntelUnit(worker, self.game_loop)})

        for destructible in intel.destructibles:
            self.destructibles.update({destructible.tag: IntelUnit(destructible, self.game_loop)})

        for structure in intel.structures_enemy:
            self.structures_enemy.update({structure.tag: IntelUnit(structure, self.game_loop)})

        for unit in intel.units_enemy:
            self.units_enemy.update({unit.tag: IntelUnit(unit, self.game_loop)})

        for worker in intel.workers_enemy:
            self.workers_enemy.update({worker.tag: IntelUnit(worker, self.game_loop)})

        if intel.HasField("primary_colony"):
            self.primary_colony.update(intel.primary_colony)

        for colony in intel.colonies:
            col = Colony()
            col.update(colony)
            self.colonies.append(col)

        for mineral in intel.mineral_fields:
            self.mineral_fields.update({mineral.tag: IntelUnit(mineral, self.game_loop)})

        for vespene in intel.vespene_geysers:
            self.vespene_geysers.update({vespene.tag: IntelUnit(vespene, self.game_loop)})

        if len(intel.production_queue) > 0:
            self.production_queue = intel.production_queue

        for squad_data in intel.squads:
            squad = self.squad_repository.internal_update(squad_data.squad_id, squad_data.name)
            for unit in squad_data.units:
                squad.units.add(IntelUnit(unit, self.game_loop))

        for event in intel.events:
            self.handler.handle(event.case_type, self.try_get(event.unit_tag))
Пример #11
0
class IntelManager:
    """Stores data about the current game state"""
    game_loop = 0

    def __init__(self):
        self.map = abathur_pb2.AbathurMap()  # TODO this is an empty datastructe atm
        self.score = score_pb2.Score()
        self.common = sc2api_pb2.PlayerCommon()
        self.handler = CaseHandler.CaseHandler() #TODO not being used yet.
        self.upgrades_self = []
        self.buildings_self = {}  # uint -> intelUnit
        self.units_self = {}  # uint -> intelUnit
        self.workers_self = {}  # uint -> intelUnit
        self.destructibles = {}  # uint -> intelUnit
        self.structures_enemy = {}  # uint -> intelUnit
        self.units_enemy = {}  # uint -> intelUnit
        self.workers_enemy = {}  # uint -> intelUnit
        self.primary_colony = Colony()
        self.colonies = []
        self.mineral_fields = {}  # uint -> intelUnit
        self.vespene_geysers = {}  # uint -> intelUnit
        self.production_queue = []
        self.squad_repository = SquadRepository()

    def update_intel(self, intel):
        """
        :param intel: an intel response object containing all requested data
        :return: None
        """
        if intel.HasField("map"):
            self.map.CopyFrom(intel.map)  # TODO this is an empty datastructure atm
        if intel.HasField("score"):
            self.score.CopyFrom(intel.score)
        if intel.HasField("common"):
            self.common.CopyFrom(intel.common)
        if len(intel.upgrades_self) > 0:
            self.upgrades_self = intel.upgrades_self

        self.game_loop = intel.game_loop

        for building in intel.buildings_self:
            self.buildings_self.update({building.tag: IntelUnit(building, self.game_loop)})

        for unit in intel.units_self:
            self.units_self.update({unit.tag: IntelUnit(unit, self.game_loop)})

        for worker in intel.workers_self:
            self.workers_self.update({worker.tag: IntelUnit(worker, self.game_loop)})

        for destructible in intel.destructibles:
            self.destructibles.update({destructible.tag: IntelUnit(destructible, self.game_loop)})

        for structure in intel.structures_enemy:
            self.structures_enemy.update({structure.tag: IntelUnit(structure, self.game_loop)})

        for unit in intel.units_enemy:
            self.units_enemy.update({unit.tag: IntelUnit(unit, self.game_loop)})

        for worker in intel.workers_enemy:
            self.workers_enemy.update({worker.tag: IntelUnit(worker, self.game_loop)})

        if intel.HasField("primary_colony"):
            self.primary_colony.update(intel.primary_colony)

        for colony in intel.colonies:
            col = Colony()
            col.update(colony)
            self.colonies.append(col)

        for mineral in intel.mineral_fields:
            self.mineral_fields.update({mineral.tag: IntelUnit(mineral, self.game_loop)})

        for vespene in intel.vespene_geysers:
            self.vespene_geysers.update({vespene.tag: IntelUnit(vespene, self.game_loop)})

        if len(intel.production_queue) > 0:
            self.production_queue = intel.production_queue

        for squad_data in intel.squads:
            squad = self.squad_repository.internal_update(squad_data.squad_id, squad_data.name)
            for unit in squad_data.units:
                squad.units.add(IntelUnit(unit, self.game_loop))

        for event in intel.events:
            self.handler.handle(event.case_type, self.try_get(event.unit_tag))

    def set_intel_empty(self):
        """reset all data in the IntelManager"""
        self.map = abathur_pb2.AbathurMap() #TODO this is an empty datastructe atm
        self.score = score_pb2.Score()
        self.common = sc2api_pb2.PlayerCommon()
        self.handler = CaseHandler.CaseHandler()
        self.upgrades_self = []
        self.buildings_self = {}            # uint -> intelUnit
        self.units_self = {}                # uint -> intelUnit
        self.workers_self = {}              # uint -> intelUnit
        self.destructibles = {}             # uint -> intelUnit
        self.structures_enemy = {}          # uint -> intelUnit
        self.units_enemy = {}                # uint -> intelUnit
        self.workers_enemy = {}             # uint -> intelUnit
        self.primary_colony = Colony()
        self.colonies = []
        self.mineral_fields = {}            # uint -> intelUnit
        self.vespene_geysers = {}           # uint -> intelUnit
        self.production_queue = []

    def get_minerals(self):
        return self.mineral_fields.values()

    def try_get_vespene_geysers(self, tag):
        """
        :param tag: tag of geyser
        :return: IntelUnit or None
        """
        return self.vespene_geysers.get(tag)

    def get_vespene_geysers(self):
        return self.vespene_geysers.values()

    def try_get_building_self(self, tag):
        """
        :param tag: tag of building
        :return: IntelUnit or None
        """
        return self.buildings_self.get(tag)

    def get_buildings_self(self):
        return self.buildings_self.values()

    def try_get_units_self(self,tag):
        """
        :param tag: tag of unit
        :return: IntelUnit or None
        """
        return self.units_self.get(tag)

    def get_units_self(self):
        return self.units_self.values()

    def try_get_worker_self(self, tag):
        """
        :param tag: tag of worker
        :return: IntelUnit or None
        """
        return self.workers_self.get(tag)

    def try_get(self, tag):
        """
        try getting the IntelUnit with "tag" from any data
        :param tag: tag of IntelUnit to retrieve
        :return: IntelUnit if success None otherwise
        """
        res = self.units_self.get(tag)
        if res is not None:
            return res
        res = self.buildings_self.get(tag)
        if res is not None:
            return res
        res = self.workers_self.get(tag)
        if res is not None:
            return res
        res = self.structures_enemy.get(tag)
        if res is not None:
            return res
        res = self.units_enemy.get(tag)
        if res is not None:
            return res
        res = self.workers_enemy.get(tag)
        if res is not None:
            return res
        res = self.destructibles.get(tag)
        if res is not None:
            return res
        res = self.mineral_fields.get(tag)
        if res is not None:
            return res
        res = self.vespene_geysers.get(tag)
        if res is not None:
            return res

    def get_workers_self(self):
        return self.workers_self.values()

    def try_get_destructible(self, tag):
        """
        :param tag: tag of destructible
        :return: IntelUnit or None
        """
        return self.destructibles.get(tag)

    def get_destructibles(self):
        return self.destructibles.values()

    def get_structures_enemy(self):
        return self.structures_enemy.values()

    def get_units_enemy(self):
        return self.units_enemy.values()

    def get_workers_enemy(self):
        return self.workers_enemy.values()

    def structures_enemy_visible(self):
        for structure in self.structures_enemy:
            if structure.last_seen == self.game_loop:
                yield structure

    def units_enemy_visible(self):
        for unit in self.units_enemy:
            if unit.last_seen == self.game_loop:
                yield unit

    def workers_enemy_visible(self):
        for worker in self.workers_enemy:
            if worker.last_seen == self.game_loop:
                yield worker

    def buildings_self(self, *types):
        """
        retreive all of the buildings you own which have any of the types given in types
        :param types: types the retrieved buildings must have
        :return: list of IntelUnit or None
        """
        for structure in self.buildings_self.values():
            if structure.unit_type in types:
                yield structure

    def units_self(self, *types):
        """
        retreive all units you own which have any of the types given in "types"
        :param types: types the retreived units must have
        :return: list of IntelUnit or None
        """
        for unit in self.units_self.values():
            if unit.unit_type in types:
                yield unit
Пример #12
0
 def __init__(self, FileName: string):
     self.NetColony = Colony(0.5, 0.5, 0.01)
     self.ControllerLogger = Logger(FileName)
     self.ControllerLogger.Log(
         "Source ;Destination ;Trust ;MessageNumber ;Pheromone ;Prediction ;Decision; Algorithm\n"
     )
Пример #13
0
 def population(self):
     self.pop_ = []
     for i in range(100):
         self.pop_.append(
             Colony(dna=self.dna_pool_[i], size=(15, 15), quantity=49))
Пример #14
0
def run():
    distances = np.array(fileUtils.read_from_directory(5))
    ant_colony = Colony(distances, 10, 100)
    ant_colony.apply_perturbation()
    shortest_path = ant_colony.run()
    print_route(shortest_path, "\nRuta cea mai buna : ")
Пример #15
0
        params = {
            'colSize': 10,
            'noIter': 150,
            'noNodes': noNodes,
            'matrix': matrix,
            'alpha': 2,
            'beta': 5,
            'ro': 0.1,
            'initPheromone': 1.0,
            'q0': 0.5,
            'noChanges': 2,
            'minValue': 1,
            'maxValue': 50
        }

        colony = Colony(params)

        bestSolution = Ant(params)
        bestSolution.distance = sys.maxsize

        for i in range(params['noIter']):
            colony.initialisation()
            colony.constructSolution()
            solution = colony.bestSolution()
            print("Best ant in generation: " + str(i) + " is: " +
                  str(solution.solution) + " with cost: " +
                  str(solution.distance))
            if solution.distance < bestSolution.distance:
                bestSolution = solution
            colony.pheromoneUpdate()
            # colony.dynamic()