Beispiel #1
0
def attr_direction(loc, d, locsites, momentumMap, momentumTerm=0.5):
    potential = locsites[(loc, d)].potential_attr
    inv_d = utils.invert_direction(d)
    # if d == momentumMap[loc]:
    #     momentum = momentumTerm
    if inv_d == momentumMap[loc]:
        momentum = -momentumTerm
    else:
        momentum = 0.
    return potential + momentum
Beispiel #2
0
def map_directions(myID,locslist,locsmap_d,locsites,momentumMap,directions_dict=None,momentumTerm=0.5):
    moves_list = []
    map_old = {loc:(
        locsites[(loc,0)].strength,
        locsites[(loc,0)].production,
        locsites[(loc,0)].owner) for x,y,loc in locslist}
    map_uptodate = {loc:(
        locsites[(loc,0)].strength if locsites[(loc,0)].owner!=myID else 0.,
        locsites[(loc,0)].production,
        locsites[(loc,0)].owner) for x,y,loc in locslist}
    for x,y,loc in locslist:
        site = locsites[(loc,0)]
        if site.owner!=myID:
            site.direction=None
            site.wanted_direction = None
            # site.potential_direction=None
            continue
        # logging.debug(loc)
        if directions_dict is None or directions_dict.get(loc) is None:
            potential_directions = sorted(CARDINALS,reverse=True,key=lambda d:attr_direction(loc,d,locsites,momentumMap,momentumTerm=momentumTerm))[:1]
        else:
            # logging.debug("using directions dict")
            potential_directions = [directions_dict.get(loc,STILL)] # NEED TO FIX THAT
            # except KeyError:
            #     potential_directions = sorted(CARDINALS,reverse=True,key=lambda d:attr_direction(loc,d,locsites,momentumMap,momentumTerm=momentumTerm))[:1]
            # logging.debug(potential_directions)
        moved = False
        site.wanted_direction = potential_directions[0]
        for d in potential_directions:
            new_loc = locsmap_d[(loc,d)]
            if shouldMove(loc,new_loc,map_old,map_uptodate,myID):
                # logging.debug("should move")
                moved = True
                site.direction = d
                new_strength = map_old[loc][0] + map_uptodate[new_loc][0] if map_uptodate[new_loc][2]==myID else map_old[loc][0]-map_uptodate[new_loc][0]
                map_uptodate[new_loc] = (new_strength,map_uptodate[new_loc][1],myID)
                map_uptodate[loc] = (0,map_old[loc][1],myID)
                break
        if not moved:
            # logging.debug("should not move")
            site.direction = STILL
        if utils.invert_direction(site.direction) == momentumMap[loc] and site.direction!=0:
            logging.debug("MOMENTUM REVERSION")
            logging.debug('map')
            logging.debug(momentumMap)
            logging.debug('potential dirs')
            logging.debug(potential_directions)
            logging.debug('momentum')
            logging.debug(momentumMap[loc])
            logging.debug('chosen')
            logging.debug(site.direction)

        moves_list.append((loc,site.direction))
    return moves_list