Beispiel #1
0
def create_toxin(name, i):
    """
    Create a toxin.
    """
    toxin = Agent(name + str(i), action=toxin_action)
    toxin["max_move"] = get_prop("toxin_move", DEF_TOXIN_MOVE)
    return toxin
Beispiel #2
0
 def setUp(self):
     (self.space, self.newton) = create_space()
     self.teeny_space = create_teeny_space()
     self.test_agent = Agent("test agent")
     self.test_agent2 = Agent("test agent 2")
     self.test_agent3 = Agent("test agent 3")
     self.test_agent4 = Agent("test agent 4")
Beispiel #3
0
def create_rholder(name, i, props=None):
    """
    Create an agent.
    """
    k_price = DEF_K_PRICE
    resources = copy.deepcopy(DEF_CAP_WANTED)
    num_resources = len(resources)

    price_list = copy.deepcopy(DEF_EACH_CAP_PRICE)
    if props is not None:
        k_price = props.get('cap_price', DEF_K_PRICE)
        for k in price_list.keys():
            price_list[k] = float("{0:.2f}".format(
                float(k_price * random.uniform(0.5, 1.5))))

    starting_cash = DEF_RHOLDER_CASH
    if props is not None:
        starting_cash = get_prop('rholder_starting_cash', DEF_RHOLDER_CASH)

    if props is not None:
        total_resources = get_prop('rholder_starting_resource_total',
                                   DEF_TOTAL_RESOURCES_RHOLDER_HAVE)
        for k in resources.keys():
            resources[k] = int(
                (total_resources * 2) * (random.random() / num_resources))

    return Agent(name + str(i),
                 action=rholder_action,
                 attrs={
                     "cash": starting_cash,
                     "resources": resources,
                     "price": price_list
                 })
Beispiel #4
0
def create_agent(i):
    """
    Creates agent for holding sand.
    """
    return Agent(SAND_PREFIX + str(i),
                 action=agent_action,
                 attrs={"grains": 0})
Beispiel #5
0
def create_grain(x, y):
    """
    Create an agent with the passed x, y value as its name.
    """
    return Agent(name=("(%d,%d)" % (x, y)),
                 action=spagent_action,
                 attrs={"save_neighbors": True})
Beispiel #6
0
def create_trader(name, i, props=None):
    return Agent(name + str(i),
                 action=seek_a_trade,
                 attrs={
                     "goods": {
                         "penguin": {
                             AMT_AVAILABLE: 0,
                             UTIL_FUNC: "penguin_util_func",
                             "incr": 0
                         },
                         "cat": {
                             AMT_AVAILABLE: 0,
                             UTIL_FUNC: "cat_util_func",
                             "incr": 0
                         },
                         "bear": {
                             AMT_AVAILABLE: 0,
                             UTIL_FUNC: "bear_util_func",
                             "incr": 0
                         },
                         "pet food": {
                             AMT_AVAILABLE: 0,
                             UTIL_FUNC: GEN_UTIL_FUNC,
                             "incr": 0
                         }
                     },
                     "util": 0,
                     "pre_trade_util": 0,
                     "trades_with": "trader"
                 })
Beispiel #7
0
def create_bb(name):
    """
    Create a big box store.
    """
    bb_book = {"expense": bb_expense,
               "capital": get_env_attr("bb_capital")}
    return Agent(name=name, attrs=bb_book, action=bb_action)
Beispiel #8
0
def create_resident(name, i, group=BLUE, **kwargs):
    """
    Creates agent of specified color type
    """
    execution_key = get_exec_key(kwargs=kwargs)
    if group == BLUE:
        grp_idx = BLUE_GRP_IDX
        mean_tol = get_prop('mean_tol',
                            DEF_TOLERANCE,
                            execution_key=execution_key)
    else:
        grp_idx = RED_GRP_IDX
        mean_tol = -get_prop(
            'mean_tol', DEF_TOLERANCE, execution_key=execution_key)
    dev = get_prop('deviation', DEF_SIGMA, execution_key=execution_key)
    this_tolerance = get_tolerance(mean_tol, dev)
    return Agent(name + str(i),
                 action=seg_agent_action,
                 attrs={
                     TOLERANCE:
                     this_tolerance,
                     GRP_INDEX:
                     grp_idx,
                     "hood_changed":
                     True,
                     "just_moved":
                     False,
                     "hood_size":
                     get_prop('hood_size',
                              DEF_HOOD_SIZE,
                              execution_key=execution_key)
                 },
                 execution_key=execution_key)
Beispiel #9
0
def create_tsetter(i, color=RED_SIN):
    """
    Create a trendsetter: all RED_SIN to start.
    """
    return Agent(TSETTER_PRENM + str(i),
                 action=tsetter_action,
                 attrs={COLOR_PREF: color,
                        DISPLAY_COLOR: color})
Beispiel #10
0
def create_agent(i, color):
    """
    Creates agent of specified color type
    """
    return Agent(group_names[color] + str(i),
                 action=agent_action,
                 attrs={TOLERANCE: DEF_TOLERANCE,
                        COLOR: color})
Beispiel #11
0
def create_grain(x, y, execution_key=None):
    """
    Create an agent with the passed x, y value as its name.
    """
    return Agent(name=("(%d,%d)" % (x, y)),
                 action=None,
                 attrs={"save_neighbors": True},
                 execution_key=execution_key)
Beispiel #12
0
def create_follower(i, color=BLUE_SIN):
    """
    Create a follower: all BLUE_SIN to start.
    """
    return Agent(FOLLOWER_PRENM + str(i),
                 action=follower_action,
                 attrs={COLOR_PREF: color,
                        DISPLAY_COLOR: color})
Beispiel #13
0
def create_bb(name):
    """
    Create a big box store.
    """
    global bb_capital

    bb_book = {"expense": 150, "capital": bb_capital}
    return Agent(name=name, attrs=bb_book, action=bb_action)
Beispiel #14
0
def create_nutrient(name, i):
    """
    Create a nutrient.
    """
    nutrient = Agent(name + str(i), action=nutrient_action)
    nutrient["max_move"] = get_prop("nutrient_move",
                                    DEF_NUTRIENT_MOVE)
    return nutrient
Beispiel #15
0
def plant_tree(i, state=HE):
    """
    Plant a new tree!
    By default, they start out healthy.
    """
    return Agent(TREE_PREFIX + str(i),
                 action=tree_action,
                 attrs={"state": state})
Beispiel #16
0
def create_other_leibniz():
    return Agent("Leibniz",
                 attrs={
                     "place": 1.0,
                     "time": LEIBBYEAR
                 },
                 action=leib_action,
                 duration=20)
Beispiel #17
0
def create_game_cell(x, y, execution_key=CLI_EXEC_KEY):
    """
    Create an agent with the passed x, y value as its name.
    """
    return Agent(name=("(%d,%d)" % (x, y)),
                 action=game_agent_action,
                 attrs={"save_neighbors": True},
                 execution_key=execution_key)
Beispiel #18
0
def create_newton():
    return Agent("Newton",
                 attrs={
                     "place": 0.0,
                     "time": 1658.0,
                     "achieve": 43.9
                 },
                 action=newt_action,
                 duration=30)
Beispiel #19
0
def create_geneng(name, i, **kwargs):
    """
    Create an agent.
    """
    dur = get_prop("lifespan", DEF_DURATION)
    return Agent(name + str(i),
                 action=geneng_action,
                 duration=dur,
                 attrs={"height": START_HEIGHT})
Beispiel #20
0
def create_central_bank(name, i):
    """
    Create the central bank to distribute the coupons
    """
    central_bank = Agent(name, action=central_bank_action)
    central_bank["percent_change"] = get_prop("percent_change", DEF_PERCENT)
    central_bank["extra_coupons"] = get_prop("extra_coupons", DEF_COUPON)
    central_bank["extra_dev"] = get_prop("extra_deviation", DEF_SIGMA)
    return central_bank
Beispiel #21
0
def create_mp(store_type, i):
    """
    Create a mom and pop store.
    """
    expense = mp_stores[str(store_type)]
    name = str(store_type) + " " + str(i)
    store_books = {"expense": expense[EXPENSE_INDX],
                   "capital": expense[CAPITAL_INDX]}
    return Agent(name=name, attrs=store_books, action=mp_action)
Beispiel #22
0
def create_follower(name, i, props=None, color=BLUE_SIN, **kwargs):
    """
    Create a follower: all BLUE to start.
    """
    execution_key = get_exec_key(kwargs=kwargs)
    return Agent(FOLLOWER_PRENM + str(i),
                 action=follower_action,
                 attrs={COLOR_PREF: color,
                        DISPLAY_COLOR: color}, execution_key=execution_key)
Beispiel #23
0
def create_tsetter(name, i, props=None, color=RED_SIN, **kwargs):
    """
    Create a trendsetter: all RED to start.
    """
    execution_key = get_exec_key(kwargs=kwargs)
    return Agent(TSETTER_PRENM + str(i),
                 action=tsetter_action,
                 attrs={COLOR_PREF: color,
                        DISPLAY_COLOR: color}, execution_key=execution_key)
Beispiel #24
0
def create_consumer(name, i, props=None):
    """
    Create consumers
    """
    spending_power = random.randint(50, 70)
    consumer_books = {"spending power": spending_power,
                      "last util": 0.0,
                      "item needed": get_rand_good()}
    return Agent(name + str(i), attrs=consumer_books, action=consumer_action)
Beispiel #25
0
 def addAgents(self, agent_name, num_of_agents):
     agents_arr = []
     for agent_num in range(1, num_of_agents + 1):
         agents_arr.append(
             Agent(
                 agent_name + "_agent" + str(agent_num),
                 action=generate_func,
                 attrs={"John": "Knox"},
             ))
     return agents_arr
Beispiel #26
0
def create_natural(name, i, **kwargs):
    """
    Create an agent.
    """
    dur = get_prop("lifespan", DEF_DURATION)
    height = gauss(START_HEIGHT, DEF_SIGMA)
    return Agent(name + str(i),
                 action=natural_action,
                 duration=dur,
                 attrs={"height": height})
Beispiel #27
0
def create_mp(store_grp, i):
    """
    Create a mom and pop store.
    """
    return Agent(name=str(store_grp) + " " + str(i),
                 attrs={
                     "expense": store_grp.get_attr("per_expense"),
                     "capital": store_grp.get_attr("init_capital")
                 },
                 action=mp_action)
Beispiel #28
0
def create_boyfriend(name, i, props=None):
    """
    Create an agent.
    """
    return Agent(name + str(i),
                 action=boyfriend_action,
                 attrs={
                     "cycle": get_prop('cycle', DEF_CYCLE),
                     "going": True
                 })
Beispiel #29
0
def create_agent(i, mean_tol, dev, color):
    """
    Creates agent of specified color type
    """
    this_tolerance = get_tolerance(mean_tol, dev)
    return Agent(group_names[color] + str(i),
                 action=agent_action,
                 attrs={
                     TOLERANCE: this_tolerance,
                     COLOR: color
                 })
Beispiel #30
0
def create_trader(name, i):
    """
    A func to create a trader.
    """
    return Agent(name + str(i),
                 action=trader_action,
                 attrs={
                     "goods": {},
                     "util": 0,
                     "pre_trade_util": 0
                 })
Beispiel #31
0
def create_bird(name, i, props=None):
    """
    Creates a bird with a numbered name and an action function
    making it flock.
    """
    return Agent(name + str(i),
                 action=bird_action,
                 attrs={
                     "max_move": BIRD_MAX_MOVE,
                     "angle": 0
                 })
Beispiel #32
0
class SpaceTestCase(TestCase):
    def setUp(self):
        (self.space, self.newton) = create_space()
        self.teeny_space = create_teeny_space()
        self.test_agent = Agent("test agent")
        self.test_agent2 = Agent("test agent 2")
        self.test_agent3 = Agent("test agent 3")
        self.test_agent4 = Agent("test agent 4")

    def tearDown(self):
        self.space = None
        self.teeny_space = None
        self.test_agent = None
        self.test_agent2 = None
        self.test_agent3 = None
        self.test_agent4 = None

    def test_constrain_x(self):
        """
        Test keeping x in bounds.
        """
        self.assertEqual(self.space.constrain_x(-10), 0)
        self.assertEqual(self.space.constrain_x(DEF_WIDTH * 2), DEF_WIDTH - 1)

    def test_constrain_y(self):
        """
        Test keeping y in bounds.
        """
        self.assertEqual(self.space.constrain_y(-10), 0)
        self.assertEqual(self.space.constrain_x(DEF_HEIGHT * 2),
                         DEF_HEIGHT - 1)

    def test_grid_size(self):
        """
        Make sure we calc grid size properly.
        """
        self.assertEqual(self.space.grid_size(), DEF_HEIGHT * DEF_WIDTH)

    def test_is_full(self):
        """
        See if the grid is full.
        """
        self.assertFalse(self.space.is_full())
        self.assertTrue(self.teeny_space.is_full())

    def test_place_members(self):
        """
        Test place_members() by making sure all agents have a pos
        when done.
        """
        for agent in self.space:
            self.assertTrue(self.space[agent].islocated())

    def test_place_member_xy(self):
        """
        Test placing an agent at a particular x, y spot.
        We will run this DEF_HEIGHT times, to test multiple
        possible placements.
        """
        space = Space("test space")
        for i in range(DEF_HEIGHT):
            spot = space.place_member(mbr=self.test_agent, xy=(i, i))
            if spot is not None:
                # the print output will usually be captured by nose,
                # but that can be turned off with --nocapture.
                (x, y) = (self.test_agent.get_x(),
                          self.test_agent.get_y())
                self.assertEqual((x, y), (i, i))

    def test_get_agent_at(self):
        """
        Test getting an agent from some locale.
        """
        space = Space("test space")
        # before adding agent, all cells are empty:
        self.assertEqual(space.get_agent_at(1, 1), None)
        for i in range(DEF_HEIGHT):
            spot = space.place_member(mbr=self.test_agent, xy=(i, i))
            whos_there = space.get_agent_at(i, i)
            self.assertEqual(whos_there, self.test_agent)

    def test_rand_x(self):
        """
        Make sure randomly generated X pos is within grid.
        If constrained, make sure it is within constraints.
        """
        x = self.space.rand_x()
        self.assertTrue(x >= 0)
        self.assertTrue(x < self.space.width)
        x2 = self.space.rand_x(low=4, high=8)
        self.assertTrue(x2 >= 4)
        self.assertTrue(x2 <= 8)

    def test_rand_y(self):
        """
        Make sure randomly generated Y pos is within grid.
        """
        y = self.space.rand_y()
        self.assertTrue(y >= 0)
        self.assertTrue(y < self.space.height)
        y2 = self.space.rand_y(low=4, high=8)
        self.assertTrue(y2 >= 4)
        self.assertTrue(y2 <= 8)

    def test_gen_new_pos(self):
        """
        Making sure new pos is within max_move of old pos.
        Since this test relies on random numbers, let's repeat it.
        """
        for i in range(REP_RAND_TESTS):
            # test with different max moves:
            max_move = (i // 2) + 1
            (old_x, old_y) = self.newton.get_pos()
            (new_x, new_y) = self.space.gen_new_pos(self.newton, max_move)
            if not abs(old_x - new_x) <= max_move:
                print("Failed test with ", old_x, " ", new_x)
            if not abs(old_y - new_y) <= max_move:
                print("Failed test with ", old_y, " ", new_y)
            self.assertTrue(abs(old_x - new_x) <= max_move)
            self.assertTrue(abs(old_y - new_y) <= max_move)

    def test_location(self):
        """
        Test that an added agent has a location.
        """
        n = create_newton()
        self.space += n
        self.assertTrue(self.space.locations[n.pos] == n)

    def test_add_location(self):
        """
        Can we add an agent to a location?
        """
        x, y = self.space.rand_x(), self.space.rand_y()
        if (x, y) not in self.space.locations:
            self.newton.set_pos(self.space, x, y)
            self.space.add_location(x, y, self.newton)
            self.assertTrue(self.space.locations[self.newton.pos] ==
                            self.newton)

    def test_move_location(self):
        """
        Can we move agent from one location to another?
        This test sometimes fails: we need to explore!
        """
        x, y = self.space.rand_x(), self.space.rand_y()
        self.space.move_location(x, y, self.newton.get_x(), self.newton.get_y())
        self.assertTrue(self.space.locations[(x, y)] == self.newton)

    def test_remove_location(self):
        """
        Test removing location from locations.
        """
        (x, y) = (self.newton.get_x(), self.newton.get_y())
        self.space.remove_location(x, y)
        self.assertTrue((x, y) not in self.space.locations)

    def test_move(self):
        """
        Test whether moving an agent stays within its max move.
        """
        for i in range(REP_RAND_TESTS):
            # test with different max moves:
            max_move = (i // 2) + 1
            (old_x, old_y) = (self.newton.get_x(), self.newton.get_y())
            self.newton.move(max_move)
            (new_x, new_y) = (self.newton.get_x(), self.newton.get_y())
            self.assertTrue(abs(new_x - old_x) <= max_move)
            self.assertTrue(abs(new_y - old_y) <= max_move)

    def test_is_empty(self):
        """
        Is cell empty?
        """
        (x, y) = (self.newton.get_x(), self.newton.get_y())
        self.assertFalse(self.space.is_empty(x, y))

    def test_get_vonneumann_hood(self):
        """
        Get von Neumann neighborhood.
        """
        space = Space("test space")
        space += self.test_agent
        space += self.test_agent2

        space.place_member(mbr=self.test_agent, xy=(0, 0))
        space.place_member(mbr=self.test_agent2, xy=(0, 1))
        hood = space.get_vonneumann_hood(self.test_agent)
        print(repr(hood))
        self.assertTrue(self.test_agent2.name in hood)

        space += self.test_agent3
        space.place_member(mbr=self.test_agent3, xy=(1, 0))
        hood = space.get_vonneumann_hood(self.test_agent)
        self.assertTrue(self.test_agent3.name in hood)

        space += self.test_agent4
        space.place_member(mbr=self.test_agent4, xy=(0, DEF_HEIGHT))
        hood = space.get_vonneumann_hood(self.test_agent)
        self.assertTrue(self.test_agent4.name not in hood)