def __init__(self, domain: int, module_directory: str): self.domain = domain self.requests = {} self.config = Config().get_config() self.field_sets = {} self.logger = log().get_logger() if not module_directory: if 'AGNTProcessor' in self.config: if 'ModuleDir' in self.config['AGNTProcessor']: module_directory = self.config['AGNTProcessor'][ 'ModuleDir'] else: ValueError( 'Configuration file does not have {0} entry within {1}' .format('ModuleDir', 'AGNTProcessor')) else: ValueError( 'Configuration file does not have {0} entry,please include it' .format('AGNTProcessor')) if 'AGNTProcessor' in self.config: if 'Modules' in self.config['AGNTProcessor']: modules = self.config['AGNTProcessor']['Modules'] self.module_loader = ModuleLoader(module_directory, 'AGNTProcessor', modules) else: ValueError( 'Configuration file does not have {0} entry within {1}'. format('Modules', 'AGNTProcessor')) else: ValueError( 'Configuration file does not have {0} entry,please include it'. format('AGNTProcessor'))
def __init__(self, domain: int, module_directory: str=None): super(AuctionProcessor, self).__init__(domain) self.auctions = {} self.config = Config().get_config() self.field_sets = {} self.logger = log().get_logger() self.field_def_manager = FieldDefManager() if not module_directory: if 'AUMProcessor' in self.config: if 'ModuleDir' in self.config['AUMProcessor']: module_directory = self.config['AUMProcessor']['ModuleDir'] else: ValueError( 'Configuration file does not have {0} entry within {1}'.format('ModuleDir', 'AumProcessor')) else: ValueError('Configuration file does not have {0} entry,please include it'.format('AumProcessor')) if 'AUMProcessor' in self.config: if 'Modules' in self.config['AUMProcessor']: modules = self.config['AUMProcessor']['Modules'] self.module_loader = ModuleLoader(module_directory, 'AUMProcessor', modules) else: ValueError( 'Configuration file does not have {0} entry within {1}'.format('Modules', 'AumProcessor')) else: ValueError('Configuration file does not have {0} entry,please include it'.format('AumProcessor')) self.build_field_sets()
def setUp(self): module_directory = '/home/ns3/py_charm_workspace/paper_subastas/auction/proc_modules' self.module_loader = ModuleLoader(module_directory, "AUMProcessor", None)
class ModuleLoaderTest(unittest.TestCase): def setUp(self): module_directory = '/home/ns3/py_charm_workspace/paper_subastas/auction/proc_modules' self.module_loader = ModuleLoader(module_directory, "AUMProcessor", None) def test_load_module(self): module_name = 'basic_server_module' module = self.module_loader.load_module(module_name, False) self.assertEqual(module is not None, True) with self.assertRaises(ModuleNotFoundError): module_name = 'basic_asda_module' module2 = self.module_loader.load_module(module_name, False) with self.assertRaises(ValueError): module_name = '' module2 = self.module_loader.load_module(module_name, False) def test_get_module(self): module_name = 'basic_server_module' self.module_loader.load_module(module_name, False) module = self.module_loader.get_module(module_name) self.assertEqual(module is not None, True) module_name = 'basic_test' with self.assertRaises(ModuleNotFoundError): module2 = self.module_loader.get_module(module_name) def test_release_module(self): module_name = 'basic_server_module' self.module_loader.load_module(module_name, False) self.module_loader.release_module(module_name) self.assertEqual(len(self.module_loader.modules), 0)
def setUp(self): try: print('starting setup') self.bids = {} # Load the module module_name = "progressive_second_price" module_directory = Config('auction_server.yaml').get_config_param( 'AUMProcessor', 'ModuleDir') self.loader = ModuleLoader(module_directory, "AUMProcessor", module_name) domain = 1 manager = BiddingObjectManager(domain) # Parse the bidding objects in file example_bids1.xml, it allocates the memory. filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids1.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid = new_bids[0] self.bids[bid.get_key()] = bid # Parse the bidding objects in file example_bids2.xml, it allocates the memory. domain = 2 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids2.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid2 = new_bids[0] self.bids[bid2.get_key()] = bid2 # Parse the bidding objects in file example_bids3.xml, it allocates the memory. domain = 3 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids3.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid3 = new_bids[0] self.bids[bid3.get_key()] = bid3 # Parse the bidding objects in file example_bids4.xml, it allocates the memory. domain = 4 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids4.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid4 = new_bids[0] self.bids[bid4.get_key()] = bid4 # Parse the bidding objects in file example_bids5.xml, it allocates the memory. domain = 5 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids5.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid5 = new_bids[0] self.bids[bid5.get_key()] = bid5 # Parse the bidding objects in file example_bids6.xml, it allocates the memory. domain = 6 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids6.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid6 = new_bids[0] self.bids[bid6.get_key()] = bid6 # Parse the bidding objects in file example_bids7.xml, it allocates the memory. domain = 7 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids7.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid7 = new_bids[0] self.bids[bid7.get_key()] = bid7 # Parse the bidding objects in file example_bids8.xml, it allocates the memory. domain = 8 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids8.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid8 = new_bids[0] self.bids[bid8.get_key()] = bid8 # Parse the bidding objects in file example_bids9.xml, it allocates the memory. domain = 9 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids9.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid9 = new_bids[0] self.bids[bid9.get_key()] = bid9 # Parse the bidding objects in file example_bids10.xml, it allocates the memory. domain = 10 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids10.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid10 = new_bids[0] self.bids[bid10.get_key()] = bid10 self.assertEqual(len(self.bids), 10) print('ending setup') except Exception as e: print(str(e))
class ProgressiveSecondPriceTest(unittest.TestCase): def setUp(self): try: print('starting setup') self.bids = {} # Load the module module_name = "progressive_second_price" module_directory = Config('auction_server.yaml').get_config_param( 'AUMProcessor', 'ModuleDir') self.loader = ModuleLoader(module_directory, "AUMProcessor", module_name) domain = 1 manager = BiddingObjectManager(domain) # Parse the bidding objects in file example_bids1.xml, it allocates the memory. filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids1.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid = new_bids[0] self.bids[bid.get_key()] = bid # Parse the bidding objects in file example_bids2.xml, it allocates the memory. domain = 2 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids2.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid2 = new_bids[0] self.bids[bid2.get_key()] = bid2 # Parse the bidding objects in file example_bids3.xml, it allocates the memory. domain = 3 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids3.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid3 = new_bids[0] self.bids[bid3.get_key()] = bid3 # Parse the bidding objects in file example_bids4.xml, it allocates the memory. domain = 4 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids4.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid4 = new_bids[0] self.bids[bid4.get_key()] = bid4 # Parse the bidding objects in file example_bids5.xml, it allocates the memory. domain = 5 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids5.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid5 = new_bids[0] self.bids[bid5.get_key()] = bid5 # Parse the bidding objects in file example_bids6.xml, it allocates the memory. domain = 6 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids6.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid6 = new_bids[0] self.bids[bid6.get_key()] = bid6 # Parse the bidding objects in file example_bids7.xml, it allocates the memory. domain = 7 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids7.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid7 = new_bids[0] self.bids[bid7.get_key()] = bid7 # Parse the bidding objects in file example_bids8.xml, it allocates the memory. domain = 8 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids8.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid8 = new_bids[0] self.bids[bid8.get_key()] = bid8 # Parse the bidding objects in file example_bids9.xml, it allocates the memory. domain = 9 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids9.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid9 = new_bids[0] self.bids[bid9.get_key()] = bid9 # Parse the bidding objects in file example_bids10.xml, it allocates the memory. domain = 10 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids10.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid10 = new_bids[0] self.bids[bid10.get_key()] = bid10 self.assertEqual(len(self.bids), 10) print('ending setup') except Exception as e: print(str(e)) def test_calculate_allocation_quantities(self): print('in test_not_enough_quantities') auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("progressive_second_price") if module: params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth") config_param_1.parse_field_value("20") params["bandwidth"] = config_param_1 ordered_bids = module.proc_module.sort_bids_by_price(self.bids) allocations = module.calculate_allocations(20, ordered_bids) for alloc_key in allocations: print(allocations[alloc_key].bidding_object_key, allocations[alloc_key].quantity) def test_not_enough_quantities(self): print('in test_not_enough_quantities') auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("progressive_second_price") if module: params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth") config_param_1.parse_field_value("20") params["bandwidth"] = config_param_1 tot_demand = module.proc_module.calculate_requested_quantities( self.bids) self.assertEqual(tot_demand, 85) allocations = module.execute(params, auction_key, start, stop, self.bids) self.assertEqual(len(allocations), 10) qty_allocated = 0 for allocation in allocations: qty_allocated += module.proc_module.get_allocation_quantity( allocation) self.assertEqual(qty_allocated, 20) sell_prices = [] qty_allocates = [] bid_ids = [] revenue = 0 for allocation in allocations: qty = module.proc_module.get_allocation_quantity(allocation) sell_price = module.proc_module.get_bid_price(allocation) bid_id = allocation.get_parent_key() revenue = revenue + (qty * sell_price) sell_prices.append(sell_price) qty_allocates.append(qty) bid_ids.append(bid_id) print(sell_prices) print(qty_allocates) print(bid_ids) def test_enough_quantities(self): print('in test_enough_quantities') auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("progressive_second_price") if module: params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth") config_param_1.parse_field_value("45") params["bandwidth"] = config_param_1 allocations = module.execute(params, auction_key, start, stop, self.bids) self.assertEqual(len(allocations), 10) qty_allocated = 0 for allocation in allocations: qty_allocated += module.proc_module.get_allocation_quantity( allocation) self.assertEqual(qty_allocated, 73)
class SubsidyAuctionTest(unittest.TestCase): def setUp(self): try: self.bids = {} # Load the module module_name = "subsidy_auction" module_directory = Config('auction_server.yaml').get_config_param( 'AUMProcessor', 'ModuleDir') self.loader = ModuleLoader(module_directory, "AUMProcessor", module_name) domain = 1 manager = BiddingObjectManager(domain) # Parse the bidding objects in file example_bids1.xml, it allocates the memory. filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids1.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid = new_bids[0] self.bids[bid.get_key()] = bid # Parse the bidding objects in file example_bids2.xml, it allocates the memory. domain = 2 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids2.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid2 = new_bids[0] self.bids[bid2.get_key()] = bid2 # Parse the bidding objects in file example_bids3.xml, it allocates the memory. domain = 3 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids3.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid3 = new_bids[0] self.bids[bid3.get_key()] = bid3 # Parse the bidding objects in file example_bids4.xml, it allocates the memory. domain = 4 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids4.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid4 = new_bids[0] self.bids[bid4.get_key()] = bid4 # Parse the bidding objects in file example_bids5.xml, it allocates the memory. domain = 5 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids5.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid5 = new_bids[0] self.bids[bid5.get_key()] = bid5 # Parse the bidding objects in file example_bids6.xml, it allocates the memory. domain = 6 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids6.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid6 = new_bids[0] self.bids[bid6.get_key()] = bid6 # Parse the bidding objects in file example_bids7.xml, it allocates the memory. domain = 7 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids7.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid7 = new_bids[0] self.bids[bid7.get_key()] = bid7 # Parse the bidding objects in file example_bids8.xml, it allocates the memory. domain = 8 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids8.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid8 = new_bids[0] self.bids[bid8.get_key()] = bid8 # Parse the bidding objects in file example_bids9.xml, it allocates the memory. domain = 9 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids9.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid9 = new_bids[0] self.bids[bid9.get_key()] = bid9 # Parse the bidding objects in file example_bids10.xml, it allocates the memory. domain = 10 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids10.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid10 = new_bids[0] self.bids[bid10.get_key()] = bid10 self.assertEqual(len(self.bids), 10) print('ending setup') except Exception as e: print(str(e)) def test_not_enough_quantities(self): print('in test_not_enough_quantities') auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("subsidy_auction") if module: params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth") config_param_1.parse_field_value("40") params["bandwidth"] = config_param_1 config_param_2 = FieldValue(name="subsidy") config_param_2.parse_field_value("1.2") params["subsidy"] = config_param_2 config_param_3 = FieldValue(name="maxvalue01") config_param_3.parse_field_value("0.5") params["maxvalue01"] = config_param_3 config_param_4 = FieldValue(name="reserveprice") config_param_4.parse_field_value("0.15") params["reserveprice"] = config_param_4 tot_demand = module.proc_module.calculate_requested_quantities( self.bids) self.assertEqual(tot_demand, 85) allocations = module.execute(params, auction_key, start, stop, self.bids) self.assertEqual(len(allocations), 10) qty_allocated = 0 for allocation in allocations: qty_allocated += module.proc_module.get_allocation_quantity( allocation) self.assertEqual(qty_allocated, 40) for allocation in allocations: sell_price = module.proc_module.get_bid_price(allocation) break self.assertEqual(sell_price, 0.456) def test_enough_quantities(self): print('in test_enough_quantities') auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("subsidy_auction") if module: # The following are the required parameters params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth") config_param_1.parse_field_value("90") params["bandwidth"] = config_param_1 config_param_2 = FieldValue(name="subsidy") config_param_2.parse_field_value("1.2") params["subsidy"] = config_param_2 config_param_3 = FieldValue(name="maxvalue01") config_param_3.parse_field_value("0.5") params["maxvalue01"] = config_param_3 config_param_4 = FieldValue(name="reserveprice") config_param_4.parse_field_value("0.15") params["reserveprice"] = config_param_4 allocations = module.execute(params, auction_key, start, stop, self.bids) self.assertEqual(len(allocations), 10) qty_allocated = 0 for allocation in allocations: qty_allocated += module.proc_module.get_allocation_quantity( allocation) self.assertEqual(qty_allocated, 73) for allocation in allocations: sell_price = module.proc_module.get_bid_price(allocation) break self.assertEqual(sell_price, 0.15)
class AgentProcessor(metaclass=Singleton): def __init__(self, domain: int, module_directory: str): self.domain = domain self.requests = {} self.config = Config().get_config() self.field_sets = {} self.logger = log().get_logger() if not module_directory: if 'AGNTProcessor' in self.config: if 'ModuleDir' in self.config['AGNTProcessor']: module_directory = self.config['AGNTProcessor'][ 'ModuleDir'] else: ValueError( 'Configuration file does not have {0} entry within {1}' .format('ModuleDir', 'AGNTProcessor')) else: ValueError( 'Configuration file does not have {0} entry,please include it' .format('AGNTProcessor')) if 'AGNTProcessor' in self.config: if 'Modules' in self.config['AGNTProcessor']: modules = self.config['AGNTProcessor']['Modules'] self.module_loader = ModuleLoader(module_directory, 'AGNTProcessor', modules) else: ValueError( 'Configuration file does not have {0} entry within {1}'. format('Modules', 'AGNTProcessor')) else: ValueError( 'Configuration file does not have {0} entry,please include it'. format('AGNTProcessor')) def create_config_params(self): config_params = {} config_dict = {} config_param = ConfigParam('domainid', DataType.UINT32, str(self.domain)) config_dict[config_param.name] = config_param for config_param_name in config_dict: config_param = config_dict[config_param_name] field_value = FieldValue() field_value.parse_field_value_from_config_param(config_param) config_params[config_param_name] = field_value return config_params def add_request(self, session_id: str, request_params: dict, auction: Auction, server_domain: int, start: datetime, stop: datetime) -> str: """ Adds a new request to the list of request to execute. :param session_id: session Id to be used :param request_params: request parameters for the process :param auction: auction to be used :param server_domain: server domain :param start: start date time :param stop: end date time :return: key for the request process created. """ module_name = auction.get_action().get_name() + "_user" module = self.module_loader.get_module(module_name) key = str(IdSource(True).new_id()) self.logger.info("new request process key {0}".format(key)) request_params = deepcopy(request_params) other_params = self.create_config_params() request_params.update(other_params) request_process = RequestProcess(key, session_id, module, auction, server_domain, request_params, start, stop) module.init_module(request_process.get_request_params()) self.requests[key] = request_process return key def delete_request(self, key: str): """ Deletes a request from the list of request to execute. :param key: :return: """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests.pop(key) self.module_loader.release_module( request_process.get_module().get_module_name()) IdSource().free_id(int(request_process.get_key())) def release_request(self, key: str): """ Releases the module attached to the request :param key: :return: """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests[key] self.module_loader.release_module( request_process.get_module().get_module_name()) def execute_request(self, key: str): """ Executes the algorithm attached to the request. :param key: request process key to execute. :return: new bid created. """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests[key] module = request_process.get_module() bids = module.execute_user(request_process.get_request_params(), request_process.get_auctions(), request_process.get_start(), request_process.get_stop()) # Sets the session for the bid. for bid in bids: bid.set_session(request_process.get_session_id()) return bids def add_auction_request(self, key: str, auction: Auction): """ Adds an auction to auction list in the request. :param key: request process key where the auction is going to be added :param auction: auction to add :return: """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests[key] # Second, look for the auction in the list of auction already inserted, if it exists generates an error if auction.get_key() in request_process.auctions: raise ValueError( "Auction key:{0} is already inserted in the request process {1}" .format(auction.get_key(), key)) # Searches for the module name on those already loaded. module_name = auction.get_action().get_name() + "user" if module_name == request_process.get_module().get_module_name(): request_process.auctions[auction.get_key()] = auction else: raise ValueError( "the module loaded {0} must be the same {1} for request {2}". format(module_name, request_process.get_module().get_module_name(), key)) def delete_auction_request(self, key: str, auction: Auction): """ Deletes an auction to auction list in the request. :param key: request process key where the auction is going to be deletec :param auction: auction to delete :return: """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests[key] if auction.get_key() not in request_process.auctions: raise ValueError( "Auction with key:{0} was not found in request process with key" .format(auction.get_key().key)) else: request_process.auctions.pop(auction.get_key()) def delete_auction(self, auction: Auction): """ Deletes an auction from all requests, normally this code is executed as part of the auction removal. :param auction: auction to be delete :return: """ for request_key in self.requests: try: self.delete_auction_request(request_key, auction) except ValueError as e: pass def get_session_for_request(self, key: str) -> str: """ Gets the sessionId generating a request process :param key: request process key :return: """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests[key] return request_process.get_session_id() def get_server_domain(self, key: str) -> str: """ Gets the sessionId generating a request process :param key: request process key :return: """ if key not in self.requests: raise ValueError("Request key:{0} was not found".format(key)) request_process = self.requests[key] return request_process.get_server_domain() @staticmethod def get_config_group() -> str: """ Gets the config group for the agent. :return: """ return "AGNTProcessor"
class AuctionProcessor(IpapMessageParser, metaclass=Singleton): """ This class manages and executes algorithms for a set of auctions. Attributes ---------- """ def __init__(self, domain: int, module_directory: str=None): super(AuctionProcessor, self).__init__(domain) self.auctions = {} self.config = Config().get_config() self.field_sets = {} self.logger = log().get_logger() self.field_def_manager = FieldDefManager() if not module_directory: if 'AUMProcessor' in self.config: if 'ModuleDir' in self.config['AUMProcessor']: module_directory = self.config['AUMProcessor']['ModuleDir'] else: ValueError( 'Configuration file does not have {0} entry within {1}'.format('ModuleDir', 'AumProcessor')) else: ValueError('Configuration file does not have {0} entry,please include it'.format('AumProcessor')) if 'AUMProcessor' in self.config: if 'Modules' in self.config['AUMProcessor']: modules = self.config['AUMProcessor']['Modules'] self.module_loader = ModuleLoader(module_directory, 'AUMProcessor', modules) else: ValueError( 'Configuration file does not have {0} entry within {1}'.format('Modules', 'AumProcessor')) else: ValueError('Configuration file does not have {0} entry,please include it'.format('AumProcessor')) self.build_field_sets() def read_misc_data(self, ipap_template: IpapTemplate, ipap_record: IpapDataRecord) -> dict: """ read the data given in the data record :param ipap_template: templete followed by data record :param ipap_record: record with the data. :return: dictionary with config values. """ config_params = {} num_fields = ipap_record.get_num_fields() for pos in range(0, num_fields): try: field_key = ipap_record.get_field_at_pos(pos) except ValueError as e: self.logger.error(str(e)) raise e try: field_def = self.field_def_manager.get_field_by_code(field_key.get_eno(), field_key.get_ftype()) field = ipap_template.get_field(field_key.get_eno(), field_key.get_ftype()) config_param = ConfigParam(field_def['name'], field_def['type'], field.write_value(ipap_record.get_field( field_key.get_eno(), field_key.get_ftype()))) config_params[config_param.name.lower()] = config_param except ValueError as e: self.logger.error("Field with eno {0} and ftype {1} was \ not parametrized".format(str(field_key.get_eno()), str(field_key.get_ftype()))) raise e return config_params def build_field_sets(self): """ Builds the field sets required for managing message between the server and the agents. """ # Fill data auctions fields agent_session = set() agent_session.add(IpapFieldKey(self.field_def_manager.get_field('ipversion')['eno'], self.field_def_manager.get_field('ipversion')['ftype'])) agent_session.add(IpapFieldKey(self.field_def_manager.get_field('srcipv4')['eno'], self.field_def_manager.get_field('srcipv4')['ftype'])) agent_session.add(IpapFieldKey(self.field_def_manager.get_field('srcipv6')['eno'], self.field_def_manager.get_field('srcipv6')['ftype'])) agent_session.add(IpapFieldKey(self.field_def_manager.get_field('srcauctionport')['eno'], self.field_def_manager.get_field('srcauctionport')['ftype'])) self.field_sets[AgentFieldSet.SESSION_FIELD_SET_NAME] = agent_session # Fill option auctions fields agent_search_fields = set() agent_search_fields.add(IpapFieldKey(self.field_def_manager.get_field('start')['eno'], self.field_def_manager.get_field('start')['ftype'])) agent_search_fields.add(IpapFieldKey(self.field_def_manager.get_field('stop')['eno'], self.field_def_manager.get_field('stop')['ftype'])) agent_search_fields.add(IpapFieldKey(self.field_def_manager.get_field('resourceid')['eno'], self.field_def_manager.get_field('resourceid')['ftype'])) self.field_sets[AgentFieldSet.REQUEST_FIELD_SET_NAME] = agent_search_fields def add_auction_process(self, auction: Auction): """ adds a Auction to auction process list :param auction: auction to be added """ key = auction.get_key() action = auction.get_action() module_name = action.name module = self.module_loader.get_module(module_name) config_params = deepcopy(action.get_config_params()) if 'domainid' not in config_params: config_params['domainid'] = ConfigParam('domainid', DataType.UINT32, str(self.domain)) action_process = AuctionProcess(key, module, auction, config_params) module.init_module(action_process.get_config_params()) self.auctions[key] = action_process return key def execute_auction(self, key: str, start: datetime, end: datetime) -> List[BiddingObject]: """ Executes the allocation algorithm for the auction :return: """ if key not in self.auctions: raise ValueError("auction process with index:{0} was not found".format(key)) action_process = self.auctions[key] module = action_process.get_module() # Sends for auctioning only active bids bids = action_process.get_bids() active_bids = {} for bidding_object_key in bids: bidding_object: BiddingObject = bids[bidding_object_key] if bidding_object.get_state() == AuctioningObjectState.ACTIVE: active_bids[bidding_object_key] = bidding_object allocations = module.execute(action_process.get_config_params(), key, start, end, active_bids) # Updates the periods executed for the bidding object (include those inactive) for bidding_object_key in bids: bidding_object: BiddingObject = bids[bidding_object_key] bidding_object.increase_execution_periods() self.logger.info("bidding object key: {0} - number of periods:{1}".format( bidding_object.get_key(), str(bidding_object.get_execution_periods()))) return allocations def add_bidding_object_to_auction_process(self, key: str, bidding_object: BiddingObject): """ adds a bidding Object to auction process :param key: key of the auction process :param bidding_object: bidding object to add :return: """ if key not in self.auctions: raise ValueError("auction process with index:{0} was not found".format(key)) action_process = self.auctions[key] self.logger.info("auction process bids already included: {0} - bid to include: {1}".format( str(len(action_process.get_bids())), bidding_object.get_key())) if bidding_object.get_parent_key() != action_process.key: raise ValueError("bidding object given {0} is not for the auction {1}".format( bidding_object.get_key(), key)) if bidding_object.get_type() == AuctioningObjectType.BID: action_process.insert_bid(bidding_object) bidding_object.associate_auction_process(key) else: raise ValueError("bidding object is not BID type") self.logger.info("auction process bids included after: {0} - bid to include: {1}".format( str(len(action_process.get_bids())), bidding_object.get_key())) def delete_bidding_object_from_auction_process(self, key: str, bidding_object: BiddingObject): """ deletes a bidding Object from auction process :param key: key of the auction process :param bidding_object: bidding object to delete :return: """ if key not in self.auctions: raise ValueError("auction process with index:{0} was not found".format(key)) action_process = self.auctions[key] action_process.bids.pop(bidding_object.get_key(), None) bidding_object.disassociate_auction_process(key) def delete_auction_process(self, key: str): """ Deletes an auction process from the list. The caller must to remove all loop entries created for this process auction. :param key key of the auction process :return: """ if key not in self.auctions: raise ValueError("auction process with index:{0} was not found".format(key)) action_process = self.auctions.pop(key, None) if action_process: self.module_loader.release_module(action_process.get_module().get_module_name()) def get_auction_process(self, key: str) -> AuctionProcess: """ Gets the auction process with the key given :param key: key to find :return: auction process """ if key not in self.auctions: raise ValueError("auction process with index:{0} was not found".format(key)) return self.auctions.get(key) def get_set_field(self, set_name: AgentFieldSet): """ Returns teh requested field set :param set_name: set of field to get :return: """ return self.field_sets[set_name] def get_session_information(self, message: IpapMessage) -> dict: """ Gets the session information within the message. If the option template is empty, then the returned auction must be zero. If the option template has more that a record, then we assume all records have the same session information. :param message message from where to extract the session information. :return: map of values that identify the session """ session_info = {} try: templ_opt_auction = self.read_template(message, TemplateType.IPAP_OPTNS_ASK_OBJECT_TEMPLATE) data_records = self.read_data_records(message, templ_opt_auction.get_template_id()) for data_record in data_records: config_items = self.read_misc_data(templ_opt_auction, data_record) field_keys = self.get_set_field(AgentFieldSet.SESSION_FIELD_SET_NAME) for field_key in field_keys: field = self.field_def_manager.get_field_by_code( field_key.get_eno(), field_key.get_ftype()) field_name = field['name'].lower() val = self.get_misc_val(config_items, field_name) session_info[field_name] = val break return session_info except ValueError as e: self.logger.error("Error during processing of get session information - error:{0} ".format(str(e))) def delete_auction(self, auction: Auction): """ deletes an auction :param auction: auction to be deleted :return: """ self.delete_auction_process(auction.get_key()) def insersect(self, start_dttm: datetime, stop_dttm: datetime, resource_id: str) -> list: """ Finds auctions that are executed for the selected resource and for the time range :param start_dttm: start of the time interval :param stop_dttm: end of the time interval :param resource_id: resource for which we want to perform bidding. :return: list of applicable auctions """ auctions_ret = [] for auction_key in self.auctions: auction = self.auctions[auction_key].auction include = True if stop_dttm <= auction.get_start(): include = False if auction.get_stop() <= start_dttm: include = False if (auction.get_resource_key() != resource_id) \ and (resource_id.lower() != "any"): include = False if include: auctions_ret.append(auction) return auctions_ret def get_applicable_auctions(self, message: IpapMessage) -> list: """ Gets the auctions applicable given the options within the message. :rtype: list :param message: message with the options to filter. :return: list of application auctions. """ try: templ_opt_auction = self.read_template(message, TemplateType.IPAP_OPTNS_ASK_OBJECT_TEMPLATE) data_records = self.read_data_records(message, templ_opt_auction.get_template_id()) for data_record in data_records: config_items = self.read_misc_data(templ_opt_auction, data_record) s_start_dttm = self.get_misc_val(config_items, "start") ipap_field = templ_opt_auction.get_field(self.field_def_manager.get_field("start")['eno'], self.field_def_manager.get_field("start")['ftype']) start_dttm = datetime.fromtimestamp(ipap_field.parse(s_start_dttm).get_value_uint64()) s_stop_dttm = self.get_misc_val(config_items, "stop") ipap_field = templ_opt_auction.get_field(self.field_def_manager.get_field("stop")['eno'], self.field_def_manager.get_field("stop")['ftype']) stop_dttm = datetime.fromtimestamp(ipap_field.parse(s_stop_dttm).get_value_uint64()) resource_id = self.get_misc_val(config_items, "resourceid") auctions = self.insersect(start_dttm, stop_dttm, resource_id) return auctions except ValueError as e: self.logger.error("Error during processing of applicable auctions - error:{0} ".format(str(e))) def get_number_auction_processes(self): """ Gets the number of auction processes registered in the container :return: """ return len(self.auctions)
class TwoAuctionGeneralizedTest(unittest.TestCase): def setUp(self): try: self.bids = {} # Load the module module_name = "two_auction_generalized" module_directory = Config('auction_server.yaml').get_config_param( 'AUMProcessor', 'ModuleDir') self.loader = ModuleLoader(module_directory, "AUMProcessor", module_name) domain = 1 manager = BiddingObjectManager(domain) # Parse the bidding objects in file example_bids1.xml, it allocates the memory. filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids1.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid = new_bids[0] self.bids[bid.get_key()] = bid # Parse the bidding objects in file example_bids2.xml, it allocates the memory. domain = 2 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids2.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid2 = new_bids[0] self.bids[bid2.get_key()] = bid2 # Parse the bidding objects in file example_bids3.xml, it allocates the memory. domain = 3 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids3.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid3 = new_bids[0] self.bids[bid3.get_key()] = bid3 # Parse the bidding objects in file example_bids4.xml, it allocates the memory. domain = 4 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids4.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid4 = new_bids[0] self.bids[bid4.get_key()] = bid4 # Parse the bidding objects in file example_bids5.xml, it allocates the memory. domain = 5 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids5.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid5 = new_bids[0] self.bids[bid5.get_key()] = bid5 # Parse the bidding objects in file example_bids6.xml, it allocates the memory. domain = 6 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids6.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid6 = new_bids[0] self.bids[bid6.get_key()] = bid6 # Parse the bidding objects in file example_bids7.xml, it allocates the memory. domain = 7 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids7.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid7 = new_bids[0] self.bids[bid7.get_key()] = bid7 # Parse the bidding objects in file example_bids8.xml, it allocates the memory. domain = 8 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids8.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid8 = new_bids[0] self.bids[bid8.get_key()] = bid8 # Parse the bidding objects in file example_bids9.xml, it allocates the memory. domain = 9 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids9.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid9 = new_bids[0] self.bids[bid9.get_key()] = bid9 # Parse the bidding objects in file example_bids10.xml, it allocates the memory. domain = 10 manager = BiddingObjectManager(domain) filename = "/home/ns3/py_charm_workspace/paper_subastas/auction/xmls/example_generalized_bids10.xml" new_bids = manager.parse_bidding_objects(filename) self.assertEqual(len(new_bids), 1) bid10 = new_bids[0] self.bids[bid10.get_key()] = bid10 except Exception as e: print(str(e)) def test_not_enough_quantities(self): auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("two_auction_generalized") if module: params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth01") config_param_1.parse_field_value("20") params["bandwidth01"] = config_param_1 config_param_2 = FieldValue(name="bandwidth02") config_param_2.parse_field_value("30") params["bandwidth02"] = config_param_2 config_param_3 = FieldValue(name="reserveprice01") config_param_3.parse_field_value("0.15") params["reserveprice01"] = config_param_3 config_param_4 = FieldValue(name="reserveprice02") config_param_4.parse_field_value("0.5") params["reserveprice02"] = config_param_4 config_param_5 = FieldValue(name="maxvalue01") config_param_5.parse_field_value("0.5") params["maxvalue01"] = config_param_5 config_param_6 = FieldValue(name="maxvalue02") config_param_6.parse_field_value("0.9") params["maxvalue02"] = config_param_6 allocations = module.execute(params, auction_key, start, stop, self.bids) print('allocations:', len(allocations)) qty_allocated = 0 for allocation in allocations: qty_allocated += module.proc_module.get_allocation_quantity( allocation) self.assertEqual(qty_allocated, 50) sell_prices = [] qty_allocates = [] bid_ids = [] revenue = 0 for allocation in allocations: qty = module.proc_module.get_allocation_quantity(allocation) sell_price = module.proc_module.get_bid_price(allocation) bid_id = allocation.get_parent_key() revenue = revenue + (qty * sell_price) sell_prices.append(sell_price) qty_allocates.append(qty) bid_ids.append(bid_id) self.assertGreater(revenue, 17.08) print(bid_ids) print(sell_prices) print(qty_allocates) def test_enough_quantities(self): auction_key = "1.1" start = datetime.now() stop = start + timedelta(seconds=100) module = self.loader.get_module("two_auction_generalized") if module: params = {} # The following are the required parameters config_param_1 = FieldValue(name="bandwidth01") config_param_1.parse_field_value("45") params["bandwidth01"] = config_param_1 config_param_2 = FieldValue(name="bandwidth02") config_param_2.parse_field_value("45") params["bandwidth02"] = config_param_2 config_param_3 = FieldValue(name="reserveprice01") config_param_3.parse_field_value("0.15") params["reserveprice01"] = config_param_3 config_param_4 = FieldValue(name="reserveprice02") config_param_4.parse_field_value("0.5") params["reserveprice02"] = config_param_4 config_param_5 = FieldValue(name="maxvalue01") config_param_5.parse_field_value("0.5") params["maxvalue01"] = config_param_5 config_param_6 = FieldValue(name="maxvalue02") config_param_6.parse_field_value("0.9") params["maxvalue02"] = config_param_6 allocations = module.execute(params, auction_key, start, stop, self.bids) self.assertEqual(len(allocations), 10) sell_prices = [] qty_allocates = [] revenue = 0 for allocation in allocations: qty = module.proc_module.get_allocation_quantity(allocation) sell_price = module.proc_module.get_bid_price(allocation) revenue = revenue + (qty * sell_price) sell_prices.append(sell_price) qty_allocates.append(qty) print(sell_prices) print(qty_allocates)