예제 #1
0
def firefly_blink(agent, **kwargs):
    """
    Blinks the given firefly agent by chaning its group. If the firefly is
    already ON, this function turns if OFF. Otherwise, checks if the
    the time passed since last blink time of the firefly agent is
    greater than the agent's blinking frequency.
    """
    # Calculate the blink parameter
    blink_frequency = agent.get_attr(BLINK_FREQUENCY)
    time_since_last_blink = abs(
        agent.get_attr(LAST_BLINKED_AT) - agent.duration)

    # Get the previous group name
    old_group = agent.group_name()

    # Turn OFF if the firefly is ON
    if old_group == FIREFLY_ON:
        agent.set_prim_group(FIREFLY_OFF)

    # Turn ON if the blinking time has arrived
    elif time_since_last_blink >= blink_frequency:
        agent.set_prim_group(FIREFLY_ON)
        # Reset the attribute
        agent.set_attr(LAST_BLINKED_AT, agent.duration)

    # Perform the actual switch
    get_model(agent.exec_key).add_switch(str(agent), old_group,
                                         agent.group_name())
예제 #2
0
def agent_action(agent, **kwargs):
    """
    The action determines what state the agent is in.
    If CALM, and lots of panic about, flip to PANIC.
    If PANICKED, but lots of CALM about, flip to CALM.
    """
    mdl = get_model(agent.exec_key)
    if mdl.get_periods() == 0:
        print("In start panic condition")
        start_panic(agent.exec_key)
    if agent.group_name() == CALM:
        ratio = neighbor_ratio(agent,
                               lambda agent: agent.group_name() == PANIC)
        if ratio > PANIC_THRESHHOLD:
            if DEBUG.debug:
                print("Changing the agent's group to panic!")
            agent.has_acted = True
            mdl.add_switch(str(agent), CALM, PANIC)
    elif agent.group_name() == PANIC:
        ratio = neighbor_ratio(agent, lambda agent: agent.group_name() == CALM)
        if ratio > CALM_THRESHHOLD:
            if DEBUG.debug:
                print("Changing the agent's group to calm!")
            agent.has_acted = True
            get_model(agent.exec_key).add_switch(str(agent), PANIC, CALM)
    return DONT_MOVE
예제 #3
0
파일: panic.py 프로젝트: gcallah/IndraABM
def start_panic(exec_key):
    maxPosn = panic_grps[CALM][WIDTH] * panic_grps[CALM][HEIGHT]
    num_panic = panic_grps[PANIC][PANICKED]
    for i in range(0, num_panic):
        agent_posn = rand.randint(0, maxPosn)
        agent_name = "Calm" + str(agent_posn)
        agent = get_agent(agent_name, exec_key)
        if agent is not None and agent.group_name() == CALM:
            get_model(exec_key).add_switch(agent_name, CALM, PANIC)
예제 #4
0
def reproduce(agent, **kwargs):
    # Check if it is time to produce
    if agent.get_attr(TIME_TO_REPRODUCE) == 0:
        if DEBUG.debug:
            print(str(agent.name) + " is having a baby!")

        # Create babies: need group name here!
        get_model(agent.exec_key).add_child(agent.prim_group_nm())

        # Reset ttr
        agent.set_attr(TIME_TO_REPRODUCE, DEF_TIME_TO_REPRO)
예제 #5
0
def start_panic(exec_key):
    """
    This function should be rewritten.
    We will make a new group method called `get_rand_subset(n)`.
    Then we will flip those agents to panicked.
    """
    maxPosn = panic_grps[CALM][WIDTH] * panic_grps[CALM][HEIGHT]
    num_panic = panic_grps[PANIC][PANICKED]
    for i in range(0, num_panic):
        agent_posn = rand.randint(0, maxPosn)
        agent_name = "Calm" + str(agent_posn)
        agent = get_agent(agent_name, exec_key)
        if agent is not None and agent.group_name() == CALM:
            get_model(exec_key).add_switch(agent_name, CALM, PANIC)
예제 #6
0
def change_color(agent, opp_group):
    """
    change agent's DISPLAY_COLOR to its opposite color
    """
    if DEBUG.debug:
        print(
            "Agent ",
            agent.name,
            " is changing colors; its prim group is ",
            agent.prim_group_nm(),
        )

    agent.set_attr(DISPLAY_COLOR, not agent.get_attr(DISPLAY_COLOR))
    get_model(agent.exec_key).add_switch(str(agent), agent.prim_group_nm(),
                                         opp_group[agent.prim_group_nm()])
예제 #7
0
    def post(self, exec_key):
        """
        Setup a test model in the registry.
        """
        model_name = None
        if 'model_name' in api.payload:
            model_name = api.payload['model_name']

        if model_name is None:
            # exec_key is supposed to match the model id if model_name is
            # not given
            model = get_model_by_id(exec_key, indra_dir)
            if model is None:
                raise (wz.NotFound(f"Model {exec_key} doesn't exist."))
            # check if a test model already exists against the given exec_
            # key which matches the model id
            model = get_model(exec_key)
            if model is not None:
                return {"msg": f'A test model {model.name} already exists'}
            else:
                return model.to_json()
        else:
            model_rec = get_model_by_name(model_name, indra_dir)
            if model_rec is None:
                raise wz.NotFound(f'Model with name {model_name} is not found')
            model = create_model_for_test(model_rec, exec_key)
            return json_converter(model)
예제 #8
0
 def test_get_model(self):
     """
     Register a model and fetch it back.
     """
     self.model = Model(exec_key=self.exec_key)
     reg_model(self.model, self.exec_key)
     self.assertEqual(self.model, get_model(self.exec_key))
예제 #9
0
    def test_model_save_load_run_from_disk(self, dump, load):
        DEF_GRP[GRP_ACTION] = self.complex_agent_action
        DEF_GRP[MBR_CREATOR] = self.complex_agent_create
        SECOND_GRP = DEF_GRP.copy()
        SECOND_GRP[COLOR] = RED
        GRP_STRUCT = {
            "COMPLEX_RED_GRP": SECOND_GRP,
            "COMPLEX_BLUE_GRP": DEF_GRP
        }
        complexModel = Model(grp_struct=GRP_STRUCT, model_nm="Basic")
        complexModel.run(5)
        registry.save_reg(key=complexModel.exec_key)
        registry.load_reg(complexModel.exec_key)
        loaded_object = get_model(complexModel.exec_key)
        self.assertTrue(type(loaded_object) == Model)
        self.assertTrue("Basic" == loaded_object.module)
        all_red_members_have_attribute_5 = True
        all_blue_memebrs_have_attribute_10 = True
        deserialized_model = loaded_object
        deserialized_model.run(5)
        for grp in deserialized_model.groups:
            for member in grp.members:
                if grp.color == BLUE:
                    all_blue_memebrs_have_attribute_10 = \
                        all_blue_memebrs_have_attribute_10 and (
                                grp[member].get_attr("value") != 5)
                else:
                    all_red_members_have_attribute_5 = \
                        all_red_members_have_attribute_5 and (
                                grp[member].get_attr("value") == 5)

        self.assertTrue(all_red_members_have_attribute_5)
        self.assertTrue(all_blue_memebrs_have_attribute_10)
예제 #10
0
def tree_action(agent, **kwargs):
    """
    A simple default agent action.
    """
    model = get_model(agent.exec_key)
    if model is None:
        print("ERROR: get_model() returned None.")
        return DONT_MOVE
    old_group = agent.group_name()
    if old_group == HEALTHY:
        if exists_neighbor(agent, lambda agent: agent.group_name() == ON_FIRE):
            agent.set_prim_group(NEW_FIRE)

    # if we didn't catch on fire above, do probabilistic transition:
    if old_group == agent.group_name():
        curr_state = STATE_MAP[old_group]
        # we gotta do these str/int shenanigans with state cause
        # JSON only allows strings as dict keys
        agent.set_prim_group(GROUP_MAP[str(prob_state_trans(int(curr_state),
                                                            state_trans))])
        if DEBUG.debug:
            if agent.group_name == NEW_FIRE:
                print("Tree spontaneously catching fire.")

    if old_group != agent.group_name():
        if DEBUG.debug:
            print(f"Add switch from {old_group} to {agent.group_name()}")
        model.add_switch(str(agent),
                         old_group,
                         agent.group_name())
    return DONT_MOVE
예제 #11
0
def main(args):
    py_model = registry.get_model(args.model)
    py_eval_setting = registry.get_eval_setting(args.eval_setting)

    if args.db and utils.evaluation_completed(py_model, py_eval_setting):
        print(f'Evaluation for {py_model.name} x {py_eval_setting.name} already found. Skipping...')
        return

    args.num_gpus = torch.cuda.device_count()
    results_dict = mp.Manager().dict()
    mp.spawn(main_worker, nprocs=args.num_gpus, args=(args, results_dict))

    idx_sorted, idx_map = torch.cat([results_dict[i]['idxs'] for i in range(args.num_gpus)]).sort()
    assert idx_sorted.eq(idx_sorted.unique()).all(), 'Error collecting results'
    assert idx_sorted.eq(torch.tensor(list(range(idx_sorted.size(0))))).all(), 'Error collecting results'

    logits = torch.cat([results_dict[i]['logits'] for i in range(args.num_gpus)])[idx_map]
    targets = torch.cat([results_dict[i]['targets'] for i in range(args.num_gpus)])[idx_map]
    image_paths = np.concatenate([results_dict[i]['image_paths'] for i in range(args.num_gpus)])[idx_map]

    metrics = py_eval_setting.get_metrics(logits, targets, image_paths, py_model)

    with open(join(args.logdir, 'metrics.json'), 'w') as outfile:
        json.dump(metrics, outfile)
    torch.save(logits, join(args.logdir, 'logits.pt'))
    torch.save(targets, join(args.logdir, 'targets.pt'))

    if args.db:
        utils.store_evaluation(py_model, py_eval_setting, metrics, logits)
        print('Uploaded to db')
        utils.close_db_connection()

    print('************************************')
    print(f'RESULT {args.model} on {args.eval_setting} - {metrics}')
    print('************************************')
예제 #12
0
def drinker_action(agent, **kwargs):
    """
    To go or not to go, that is the question.
    The decision is based on the agent's memory of how crowded the
    bar has been recently (a parameter).
    """
    if DEBUG.debug:
        print("Alcoholic {} is located at {}".format(agent.name,
                                                     agent.get_pos()))
    bar = get_model(agent.exec_key)
    percent_full = memory_check(agent)
    # agent motivation is inverse agent's memory of percentage full
    agent[MOTIV] = 1 - percent_full
    going = get_decision(agent)
    if agent.group_name() == AT_HOME:
        if going:
            bar.add_switch(str(agent), AT_HOME, AT_BAR)
    else:
        if not going:
            bar.add_switch(str(agent), AT_BAR, AT_HOME)
        # Updating the agent's memory for last night.
        # There might be a better place to do this.
        # doing it here has a one day lag.
        population = sum([len(group.members) for group in bar.groups])
        attendance = bar.env.pop_hist.pops[AT_BAR]
        last_att_perc = attendance[-1] / population
        agent[MEMORY].pop(0)
        agent[MEMORY].append(last_att_perc)
    return MOVE
예제 #13
0
def main_worker(gpu, args, results_dict):
    dist.init_process_group(backend=args.backend, init_method=args.dist_url, world_size=args.num_gpus, rank=gpu)
    torch.cuda.set_device(gpu)

    registry.load_full_registry()
    py_model = registry.get_model(args.model)
    py_eval_setting = registry.get_eval_setting(args.eval_setting)

    model = py_model.generate_classifier(py_eval_setting)
    model = model.cuda()
    model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[gpu])

    batch_size = py_model.get_batch_size(py_eval_setting)
    gpu_perturbation_fn = py_eval_setting.get_perturbation_fn_gpu(py_model)
    torch.set_grad_enabled(py_eval_setting.adversarial_attack is not None)

    setting_transform = [py_eval_setting.transform] if py_eval_setting.transform is not None else []
    val_dataset = CustomImageFolder(
        root = py_eval_setting.get_dataset_root(),
        transform = transforms.Compose(setting_transform + [py_model.transform]),
        perturbation_fn = py_eval_setting.get_perturbation_fn_cpu(py_model),
        idx_subsample_list = py_eval_setting.get_idx_subsample_list(py_model),
    )

    val_sampler = DistributedSampler(val_dataset, num_replicas=args.num_gpus, rank=gpu, shuffle=False)
    val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, 
                            num_workers=args.workers, pin_memory=True, sampler=val_sampler)
    
    logits, targets, image_paths, idxs = validate(gpu, args, val_loader, model, gpu_perturbation_fn)
    results_dict[gpu] = {'logits': logits, 'targets': targets, 'image_paths': image_paths, 'idxs': idxs}
예제 #14
0
def get_model_if_exists(exec_key):
    """
    A function that returns the model running at `exec_key`
    or raises a 404 error if it doesn't exist.
    """
    model = get_model(exec_key)
    if model is None:
        raise wz.NotFound(f"Model Key: {exec_key}, not found.")
    return model
예제 #15
0
파일: panic.py 프로젝트: gcallah/IndraABM
def agent_action(agent, **kwargs):
    """
    This is what agents do each turn of the model.
    """
    if DEBUG.debug:
        print("The agent is called", agent)
    global first_period
    if first_period:
        start_panic(agent.exec_key)
    first_period = False
    if agent.group_name() == CALM:
        ratio = neighbor_ratio(agent,
                               lambda agent: agent.group_name() == PANIC)
        if ratio > THRESHHOLD:
            if DEBUG.debug:
                print("Changing the agent's group to panic!")
            agent.has_acted = True
            get_model(agent.exec_key).add_switch(str(agent), CALM, PANIC)
    return DONT_MOVE
예제 #16
0
def consumer_action(consumer, **kwargs):
    """
    Check shops near consumer and
    consumer decide where to shop at.
    """
    global item_needed
    item_needed = consumer.get_attr(ITEM_NEEDED)
    box = get_model(consumer.exec_key)
    hood_size = box.props.get("hood_size", DEF_HOOD_SIZE)
    sellers = get_neighbors(consumer, pred=sells_good, size=hood_size)
    shop_at = choose_store(consumer, sellers.members.items())
    if shop_at is not None:
        transaction(shop_at, consumer)
        consumer[ITEM_NEEDED] = get_rand_good()
    return MOVE
예제 #17
0
def utils_from_good(store, good):
    '''
    Return util for each choice of retailers
    with preference for mom-and-pop
    '''
    grp = str(store.primary_group())
    box = get_model(store.exec_key)
    mp_pref = box.mp_pref
    # add preference if good sold in mom and pop
    if grp == MP_STORE:
        if good in store.get_attr(GOODS_SOLD):
            return (random.random() + store.get_attr(UTIL_ADJ)) * mp_pref
    elif grp == BIG_BOX:
        return NO_PREF
    return NOT_AVAIL
예제 #18
0
def extract_metadata(df):
    df_metadata = df.copy()
    df_metadata['arch'] = [
        registry.get_model(x.name).arch
        if registry.contains_model(x.name) else 'N/A' for x in df.model
    ]

    for col in df_metadata.columns:
        if type(col) == EvaluationSetting:
            size = registry.get_eval_setting(
                col.name.replace('_pm0', '').replace('_pm10', '')).size
            df_metadata[col.name +
                        '_dataset_size'] = [size] * df_metadata.shape[0]
            df_metadata = df_metadata.drop(columns=col)

    df, df_metadata = strip_metadata(df), strip_metadata(df_metadata)
    return df, df_metadata
예제 #19
0
def town_action(town):
    """
    Create big box store at appropriate turn.
    """
    bb_grp = get_group(BIG_BOX, town.exec_key)
    box = get_model(town.exec_key)
    bb_period = box.bb_period
    bb_init_capital = box.multiplier * AVG_MP_INIT_CAP
    # if no big box exists, make them:
    num_bbs = len(bb_grp)
    if num_bbs == 0:
        if town.get_periods() >= bb_period:
            new_bb = bb_grp.mbr_creator(BIG_BOX,
                                        num_bbs,
                                        bb_init_capital,
                                        exec_key=town.exec_key)
            join(bb_grp, new_bb)
            town.place_member(new_bb)
예제 #20
0
def view_model(user, update=False):
    from registry.registry import get_model
    return user.debug(repr(get_model(user.exec_key)))
예제 #21
0
def bar_graph(user, update=False):
    from registry.registry import get_model
    return get_model(user.exec_key).bar_graph()
예제 #22
0
def scatter_plot(user, update=False):
    from registry.registry import get_model
    return get_model(user.exec_key).scatter_plot()