Exemple #1
0
    def __init__(self, db, islandid, session):
        """
		@param db: db instance with island table
		@param islandid: id of island in that table
		@param session: reference to Session instance
		"""
        super(Island, self).__init__(worldid=islandid)
        self.session = session

        x, y, filename = db("SELECT x, y, file FROM island WHERE rowid = ? - 1000", islandid)[0]
        self.__init(Point(x, y), filename)

        # create building indexers
        self.building_indexers = {}
        self.building_indexers[BUILDINGS.TREE_CLASS] = BuildingIndexer(
            WildAnimal.walking_range, self, self.session.random
        )

        # load settlements
        for (settlement_id,) in db("SELECT rowid FROM settlement WHERE island = ?", islandid):
            Settlement.load(db, settlement_id, self.session)

            # load buildings
        from horizons.world import load_building

        for (building_worldid, building_typeid) in db("SELECT rowid, type FROM building WHERE location = ?", islandid):
            load_building(self.session, db, building_typeid, building_worldid)
Exemple #2
0
    def __init__(self, db, islandid, session):
        """
		@param db: db instance with island table
		@param islandid: id of island in that table
		@param session: reference to Session instance
		"""
        super(Island, self).__init__(worldid=islandid)
        self.session = session

        x, y, filename = db(
            "SELECT x, y, file FROM island WHERE rowid = ? - 1000",
            islandid)[0]
        self.__init(Point(x, y), filename)

        # create building indexers
        self.building_indexers = {}
        self.building_indexers[BUILDINGS.TREE_CLASS] = BuildingIndexer(
            WildAnimal.walking_range, self, self.session.random)

        # load settlements
        for (settlement_id, ) in db(
                "SELECT rowid FROM settlement WHERE island = ?", islandid):
            Settlement.load(db, settlement_id, self.session)

        # load buildings
        from horizons.world import load_building
        for (building_worldid, building_typeid) in \
            db("SELECT rowid, type FROM building WHERE location = ?", islandid):
            load_building(self.session, db, building_typeid, building_worldid)
 def __init__(self, url='https://api.hitbtc.com'):
     self.url = url
     self.session = requests.session()
     self.trader_dict = None
     self.settlement = Settlement()
     self.database = None
     self.from_time = None
     self.till_time = None
     self.settlement_price = {'BTCUSD': 0, 'ETHUSD': 0}
 def test_adjacentSettlements_11_20_21(self):
     s = Settlement(Point(1,1), Point(2,0), Point(2,1), 1)
     adjSettlements = s.adjacentSettlements()
     self.assertIn(Settlement(Point(1,1), Point(2,0), Point(1,0), -1), \
                       adjSettlements)
     self.assertIn(Settlement(Point(1,1), Point(2,1), Point(1,2), -1), \
                       adjSettlements)
     self.assertIn(Settlement(Point(2,0), Point(2,1), Point(3,0), -1), \
                       adjSettlements)
     self.assertEqual(len(adjSettlements), 3)
class TestSettlement(unittest.TestCase):

    def setUp(self):
        self.s = Settlement(Point(1,1), Point(2,2), Point(2,1), 1)

    def test_adjacentRoads_1_1_and_2_2_and_2_1(self):
        self.s = Settlement(Point(1,1), Point(2,2), Point(2,1), 1)
        adjRoads = self.s.adjacentRoads()
        self.assertIn(Road(Point(1,1), Point(2,2), -1), adjRoads)
        self.assertIn(Road(Point(1,1), Point(2,1), -1),adjRoads)
        self.assertIn(Road(Point(2,1), Point(2,2), -1),adjRoads)
        self.assertEqual(3, len(adjRoads))

    def test_adjacentRoads_1_1_and_2_2_and_1_2(self):
        self.s = Settlement(Point(1,1), Point(2,2), Point(1,2), 1)
        adjRoads = self.s.adjacentRoads()
        self.assertIn(Road(Point(1,1), Point(2,2), -1), adjRoads)
        self.assertIn(Road(Point(1,1), Point(1,2), -1),adjRoads)
        self.assertIn(Road(Point(1,2), Point(2,2), -1),adjRoads)
        self.assertEqual(3, len(adjRoads))

    def test_adjacentSettlementsByRoad_10_11(self): 
        r = Road(Point(1,0), Point(1,1), 1)   
        adjSettlements = Settlement.adjacentSettlementsByRoad(r)   
        self.assertIn(Settlement(Point(0,1), Point(1,0), Point(1,1), -1), \
                          adjSettlements)  
        self.assertIn(Settlement(Point(2,0), Point(1,0), Point(1,1), -1), \
                          adjSettlements)   
        self.assertEqual(len(adjSettlements), 2)

    def test_adjacentSettlementsByRoad_11_21(self):
        r = Road(Point(1,1), Point(2,1), 1)
        adjSettlements = Settlement.adjacentSettlementsByRoad(r)
        self.assertIn(Settlement(Point(1,1), Point(2,1), Point(1,2), -1), \
                          adjSettlements)
        self.assertIn(Settlement(Point(1,1), Point(2,1), Point(2,0), -1), \
                          adjSettlements)
        self.assertEqual(len(adjSettlements), 2)

    def test_adjacentSettlements_11_20_21(self):
        s = Settlement(Point(1,1), Point(2,0), Point(2,1), 1)
        adjSettlements = s.adjacentSettlements()
        self.assertIn(Settlement(Point(1,1), Point(2,0), Point(1,0), -1), \
                          adjSettlements)
        self.assertIn(Settlement(Point(1,1), Point(2,1), Point(1,2), -1), \
                          adjSettlements)
        self.assertIn(Settlement(Point(2,0), Point(2,1), Point(3,0), -1), \
                          adjSettlements)
        self.assertEqual(len(adjSettlements), 3)

    def test_getSettlementWithOwner(self):
        s = Settlement(Point(1,1), Point(2,0), Point(2,1), -1)
        newOwnerId = 1
        expectedResult = Settlement(Point(1,1), Point(2,0), Point(2,1), newOwnerId)
        self.assertEqual(s.getSettlementWithOwner(newOwnerId), expectedResult)
	def __init__(self, db, islandid, session, preview=False):
		"""
		@param db: db instance with island table
		@param islandid: id of island in that table
		@param session: reference to Session instance
		@param preview: flag, map preview mode
		"""
		super(Island, self).__init__(worldid=islandid)

		if False:
			from horizons.session import Session
			assert isinstance(session, Session)
		self.session = session

		x, y, filename = db("SELECT x, y, file FROM island WHERE rowid = ? - 1000", islandid)[0]
		self.__init(Point(x, y), filename, preview=preview)

		if not preview:
			# create building indexers
			from horizons.world.units.animal import WildAnimal
			self.building_indexers = {}
			self.building_indexers[BUILDINGS.TREE] = BuildingIndexer(WildAnimal.walking_range, self, self.session.random)

		# load settlements
		for (settlement_id,) in db("SELECT rowid FROM settlement WHERE island = ?", islandid):
			settlement = Settlement.load(db, settlement_id, self.session, self)
			self.settlements.append(settlement)

		if not preview:
			# load buildings
			from horizons.world import load_building
			for (building_worldid, building_typeid) in \
				  db("SELECT rowid, type FROM building WHERE location = ?", islandid):
				load_building(self.session, db, building_typeid, building_worldid)
 def test_adjacentRoads_1_1_and_2_2_and_1_2(self):
     self.s = Settlement(Point(1,1), Point(2,2), Point(1,2), 1)
     adjRoads = self.s.adjacentRoads()
     self.assertIn(Road(Point(1,1), Point(2,2), -1), adjRoads)
     self.assertIn(Road(Point(1,1), Point(1,2), -1),adjRoads)
     self.assertIn(Road(Point(1,2), Point(2,2), -1),adjRoads)
     self.assertEqual(3, len(adjRoads))
 def test_adjacentSettlementsByRoad_11_21(self):
     r = Road(Point(1,1), Point(2,1), 1)
     adjSettlements = Settlement.adjacentSettlementsByRoad(r)
     self.assertIn(Settlement(Point(1,1), Point(2,1), Point(1,2), -1), \
                       adjSettlements)
     self.assertIn(Settlement(Point(1,1), Point(2,1), Point(2,0), -1), \
                       adjSettlements)
     self.assertEqual(len(adjSettlements), 2)
	def add_settlement(self, position, radius, player, load=False):
		"""Adds a settlement to the island at the position x, y with radius as area of influence.
		@param position: Rect describing the position of the new warehouse
		@param radius: int radius of the area of influence.
		@param player: int id of the player that owns the settlement"""
		settlement = Settlement(self.session, player)
		settlement.initialize()
		self.add_existing_settlement(position, radius, settlement, load)
		# TODO: Move this to command, this message should not appear while loading
		self.session.ingame_gui.message_widget.add(string_id='NEW_SETTLEMENT',
		                                           point=position.center(),
		                                           message_dict={'player':player.name},
		                                           play_sound=player.is_local_player)

		NewSettlement.broadcast(self, settlement)

		return settlement
 def __init__(self,
              trader_dict,
              user_name='postgres',
              password='******',
              ip='5432',
              database_name='postgres'):
     database_url = ('postgresql://{}:{}@localhost:{}/{}'.format(
         user_name, password, ip, database_name))
     self.engine = create_engine(database_url)
     self.metadata = MetaData(self.engine)
     self.tradetable = self.build_trade_table()
     self.balancetable = self.build_balance_table(trader_dict)
     self.settlementtable = self.build_settlements_table()
     self.conn = self.engine.connect()
     DBSession = sessionmaker(bind=self.engine)
     self.session = DBSession()
     self.settlement = Settlement()
Exemple #11
0
	def add_settlement(self, position, radius, player, load=False):
		"""Adds a settlement to the island at the position x, y with radius as area of influence.
		@param position: Rect describing the position of the new warehouse
		@param radius: int radius of the area of influence.
		@param player: int id of the player that owns the settlement"""
		settlement = Settlement(self.session, player)
		settlement.initialize()
		self.add_existing_settlement(position, radius, settlement, load)
		# TODO: Move this to command, this message should not appear while loading
		self.session.ingame_gui.message_widget.add(position.center().x, \
		                                           position.center().y, \
		                                           'NEW_SETTLEMENT', \
		                                           {'player':player.name}, \
		                                           self.session.world.player == player)

		self.session.world.notify_new_settlement()

		return settlement
Exemple #12
0
	def __init__(self, db, islandid, session):
		"""
		@param db: db instance with island table
		@param islandid: id of island in that table
		@param session: reference to Session instance
		"""
		super(Island, self).__init__(worldid=islandid)
		self.session = session

		x, y, filename = db("SELECT x, y, file FROM island WHERE rowid = ? - 1000", islandid)[0]
		self.__init(Point(x, y), filename)

		# load settlements
		for (settlement_id,) in db("SELECT rowid FROM settlement WHERE island = ?", islandid):
			Settlement.load(db, settlement_id, self.session)

		# load buildings
		from horizons.world import load_building
		for (building_worldid, building_typeid) in \
		    db("SELECT rowid, type FROM building WHERE location = ?", islandid):
			load_building(self.session, db, building_typeid, building_worldid)
    def __init__(self, db, islandid, session):
        """
		@param db: db instance with island table
		@param islandid: id of island in that table
		@param session: reference to Session instance
		"""
        super(Island, self).__init__(worldid=islandid)
        self.session = session

        x, y, filename = db(
            "SELECT x, y, file FROM island WHERE rowid = ? - 1000",
            islandid)[0]
        self.__init(Point(x, y), filename)

        # load settlements
        for (settlement_id, ) in db(
                "SELECT rowid FROM settlement WHERE island = ?", islandid):
            Settlement.load(db, settlement_id, self.session)

        # load buildings
        from horizons.world import load_building
        for (building_worldid, building_typeid) in \
            db("SELECT rowid, type FROM building WHERE location = ?", islandid):
            load_building(self.session, db, building_typeid, building_worldid)
Exemple #14
0
    def add_settlement(self, position, radius, player):
        """Adds a settlement to the island at the position x, y with radius as area of influence.
		@param position: Rect describing the position of the new branch office
		@param radius: int radius of the area of influence.
		@param player: int id of the player that owns the settlement"""
        settlement = Settlement(self.session, player)
        self.add_existing_settlement(position, radius, settlement)
        # TODO: Move this to command, this message should not appear while loading
        self.session.ingame_gui.message_widget.add(position.center().x, \
                                                   position.center().y, \
                                                   'NEW_SETTLEMENT', \
                                                   {'player':player.name}, \
                                                   self.session.world.player == player)

        self.session.world.notify_new_settlement()

        return settlement
class DataBase(object):
    """
    This is the database.
    When create database, provide your own configs.
    """
    def __init__(self,
                 trader_dict,
                 user_name='postgres',
                 password='******',
                 ip='5432',
                 database_name='postgres'):
        database_url = ('postgresql://{}:{}@localhost:{}/{}'.format(
            user_name, password, ip, database_name))
        self.engine = create_engine(database_url)
        self.metadata = MetaData(self.engine)
        self.tradetable = self.build_trade_table()
        self.balancetable = self.build_balance_table(trader_dict)
        self.settlementtable = self.build_settlements_table()
        self.conn = self.engine.connect()
        DBSession = sessionmaker(bind=self.engine)
        self.session = DBSession()
        self.settlement = Settlement()

    def drop_all(self):
        self.metadata.drop_all(bind=self.engine, tables='balances')

    def build_trade_table(self):
        """
        Build a table for trade data
        """
        tradetable = Table('trades', self.metadata,
                           Column('id', Integer, primary_key=True),
                           Column('price', String(50)),
                           Column('timestamp', String(50)),
                           Column('symbol', String(50)),
                           Column('side', String(50)),
                           Column('quantity', String(50)),
                           Column('trader_id', String(50)),
                           Column('counterparty', String(50)))
        self.metadata.create_all()
        return tradetable

    def build_balance_table(self, trader_dict):
        """
        Build a table for balance and initialize this table according to
        trader's balance

        :param trader_dict:
            A list of Trader class
        """
        balancetable = Table('balances', self.metadata,
                             Column('trader_id', String(50), primary_key=True),
                             Column('balance', FLOAT))
        self.metadata.drop_all()
        self.metadata.create_all()
        initial_balance = [{
            'trader_id': id,
            'balance': trader.balance
        } for id, trader in trader_dict.items()]
        self.engine.execute(balancetable.insert(), initial_balance)
        return balancetable

    def build_settlements_table(self):
        """
        Build a table for settlements
        """
        settlementstable = Table('settlements', self.metadata,
                                 Column('id', Integer, primary_key=True),
                                 Column('from_participant', String(50)),
                                 Column('to_participant', String(50)),
                                 Column('currency', FLOAT))
        self.metadata.create_all()
        return settlementstable

    def write_trade_data(self, trade_data):
        """
        Write trade data to trade table
        :param trade_data:
            A list of dict
        """
        self.engine.execute(self.tradetable.insert(), trade_data)

    def write_settlement_data(self, trade_data, stl_price):
        """
        Write settlement data to settlements table

        :param trade_data:
            A list of dict
        :param stl_price:
            A float
        """
        settlement_data = []
        for data in trade_data:
            stl_data = self.settlement.settlement_obligation(data, stl_price)
            settlement_data.append(stl_data)
        self.engine.execute(self.settlementtable.insert(), settlement_data)
        return settlement_data

    def write_balance_data(self, settlement_data):
        """
        Find trader according to trader_id and update his balance

        :param settlement_data:
            A list of dict
        """
        for data in settlement_data:
            from_trader_bal = self.query_balance(data['from_participant'])
            to_trader_bal = self.query_balance(data['to_participant'])
            bal_data = self.settlement.settlement_balance(
                from_trader_bal, to_trader_bal, data['currency'])
            from_bal = self.balancetable.update().where(
                self.balancetable.c.trader_id == data['from_participant']
            ).values(balance=bal_data['from_trader_bal'])
            rsf = self.conn.execute(from_bal)
            to_bal = self.balancetable.update().where(
                self.balancetable.c.trader_id == data['to_participant']
            ).values(balance=bal_data['to_trader_bal'])
            rst = self.conn.execute(to_bal)

    def query_balance(self, trader_id):
        """
        Query the balance of a trader according to trader_id

        :param trader_id:
            A string
        :return:
            A float
        """
        trader = self.session.query(self.balancetable).filter(
            self.balancetable.c.trader_id == trader_id).first()
        balance = trader.balance
        return balance
def parallel_settlements(grid):

    # generate a random seed, use it and store it

    random_seed = round(np.random.random() * 1000000)
    np.random.seed(random_seed)
    file_seed = open(directorySettlements + subpath + 'randomSeeds.txt', 'a')
    file_seed.write(
        str(scenario['interactionW']) + "," + str(grid) + "," +
        str(random_seed) + '\n')
    file_seed.close()

    # read the previously generated landscape object (the grid)

    lscape = Landscape()
    lscape.pkl_import(path=directoryLandscapes + 'landscape' + str(grid) +
                      '.pkl')

    # start with an empty list of distributions

    settlements = []

    # loop over multiple distributions

    for distribution in range(0, nDistributions):

        # print message to keep track of the progress

        print("Generating settlement " + str(distribution) + " on grid " +
              str(grid))

        # sample the random number of individuals

        if nIndividualsMin < nIndividualsMax:
            n_individuals = np.random.randint(low=nIndividualsMin,
                                              high=nIndividualsMax)
        else:
            n_individuals = nIndividualsMin

        # new settlement object

        settlements.append(
            Settlement(interaction_w=interactionW, landscape=lscape))

        # set the response of the species to the different variables

        for index, label in enumerate(varLabel):
            settlements[distribution].set_weight(label=label,
                                                 weight=varWeight[index])

        # place the individuals in the grid using gibbs sampling

        settlements[distribution].gibbs(n_individuals=n_individuals,
                                        n_iterations=n_individuals *
                                        nSelections)

        # save the distribution

        settlements[distribution].export_to_pkl(
            path=directorySettlements + subpath + 'settlement' + str(grid) +
            '_' + str(distribution) + ".pkl")
class Exchange(object):
    """
    This is the exchange of cryptocurrency.
    Hitbtc exchange was used in this code.
    Hitbtc will provide the trade data.
    """
    def __init__(self, url='https://api.hitbtc.com'):
        self.url = url
        self.session = requests.session()
        self.trader_dict = None
        self.settlement = Settlement()
        self.database = None
        self.from_time = None
        self.till_time = None
        self.settlement_price = {'BTCUSD': 0, 'ETHUSD': 0}

    def assign_traders(self, trader_dict):
        """
        Assign traders to exchange

        :param trader_dict:
            a dict of Trader
        """
        self.trader_dict = trader_dict

    def assign_database(self,
                        user_name='postgres',
                        password='******',
                        ip='5432',
                        database_name='postgres'):
        """
        Add database to exchange
        WARNING:
            Please customize your own configs
        """
        self.database = DataBase(self.trader_dict,
                                 user_name=user_name,
                                 password=password,
                                 ip=ip,
                                 database_name=database_name)

    def initialize(self):
        """
        Initialize the starting time. From now on the settlement begins
        """
        self.from_time = datetime.datetime.now()

    def settle_trigger(self, symbols):
        """
        A trigger to run incremental settlements.
        Using RESTful api to query data from exchange.
        The settlement process includes produce a settlement price;
        calculate the settlements obligations; and adjust traders' balance;
        also record the trade data, settlements, and balance to a database

        :param symbol:
            A list of string. (ie. ['BTCUSD', 'ETHUSD'])
        """
        self.till_time = datetime.datetime.now()
        for symbol in symbols:
            query = ('/api/2/public/trades/{}?sort=DESC&by=timestamp&'
                     'from={}&till={}&limit=1000'.format(
                         symbol, self.from_time, self.till_time))
            trade_data = self.session.get(self.url + query).json()
            trade_data = self.process_trade_data(trade_data, symbol)
            self.database.write_trade_data(trade_data)
            self.settlement_price[symbol] = self.one_hour_vwap(symbol)
            stl_data = self.database.write_settlement_data(
                trade_data, self.settlement_price[symbol])
            self.database.write_balance_data(stl_data)
        self.from_time = self.till_time

    def process_trade_data(self, trade_data, symbol):
        """
        Make up the symbol in the trade_data;
        randomly assign trader id and counter-party id to trade data

        :param trade_data:
            A list of dict
        :param symbol:
            A string
        :return:
            A list of dict
        """
        for data in trade_data:
            trader_id, counterparty_id = np.random.choice(range(10),
                                                          size=2,
                                                          replace=False)
            data.update({
                'symbol': symbol,
                'trader_id': str(trader_id),
                'counterparty': str(counterparty_id)
            })
        return trade_data

    def one_hour_vwap(self, symbol):
        """
        Query the last one hour trade data and calculate the volume weighed
        average price as the settlement price

        :param symbol:
            A string
        :return:
            A float
        """
        till_time = datetime.datetime.now()
        from_time = till_time - datetime.timedelta(hours=1)
        query = ('/api/2/public/trades/{}?sort=DESC&by=timestamp&'
                 'from={}&till={}&limit=1000'.format(symbol, from_time,
                                                     till_time))
        one_hour_data = self.session.get(self.url + query).json()
        vwap = self.settlement.settlement_price(one_hour_data)
        return vwap
 def test_getSettlementWithOwner(self):
     s = Settlement(Point(1,1), Point(2,0), Point(2,1), -1)
     newOwnerId = 1
     expectedResult = Settlement(Point(1,1), Point(2,0), Point(2,1), newOwnerId)
     self.assertEqual(s.getSettlementWithOwner(newOwnerId), expectedResult)
 def setUp(self):
     self.s = Settlement(Point(1,1), Point(2,2), Point(2,1), 1)