FactoryManager¶
-
class
scml.scml2019.
FactoryManager
(name=None, simulator_type=<class 'scml.scml2019.simulators.FastFactorySimulator'>)[source]¶ Bases:
scml.scml2019.SCMLAgent
,abc.ABC
Base factory manager class that will be inherited by participant negmas in ANAC 2019.
The agent can access the world simulation in one of two ways:
Attributes and methods available in the Agent-SCML2020World-Interface (See
SCMLAWI
documentation for those).Attributes and methods in the
FactoryManager
object itself. All factory managers will have the following attributes and methods that simplify the interaction with the world simulation. Some of these attributes/methods are convenient ways to access functionality already available in the agent’s internalSCMLAWI
.
Attributes
Agent information
id
: The unique ID assigned to this agent. This is unique system-wide and is what is used in contracts, CFPs, etc.name
: A name of the agent used for display purposes only. The simulator never accesses or uses this name except in printing and logging.type_name
: A string giving the type of the agent (as a fully qualified python class name).
Capabilities/Profiles
line_profiles
: A mapping specifying for each line index, all the profiles that can be run on itprocess_profiles
: A mapping specifying for eachProcess
index, all the profiles used to run it in the factoryproducing
: Mapping from a product index to all manufacturing processes that can generate itconsuming
: Mapping from a product index to all manufacturing processes that can consume itcompiled_profiles
: All the profiles to be used by the factory belonging to this agent compiled to use process indicesmax_storage
: Maximum storage available to the agent. Zero, None or float(‘inf’) all indicate unlimited storage.
Production Graph (also accessible through awi)
Helper Objects
awi
: TheSCMLAWI
instance assigned to this agent. It can be used to interact with the simulation (SeeSCMLAWI
documentation).simulator
: AFactorySimulator
object that can be used to simulate what happens in theFactory
assigned to this agent when given operations are conducted (e.g. production, paying money, etc).
Negotiations/Contracts
requested_negotiations
: A dynamic list of negotiations currently requested by the agent but not started. Correct management of this list is only possible if the agent **always* usesrequest_negotiation
method of this class (see methods later) rather than directly calling request_method on theSCMLAWI
(awi
) member.running_negotiations
: A dynamic list of negotiations currently running involving this agent. Correct management of this list is only possible if the agent **always* usesrequest_negotiation
method of this class (see methods later) rather than directly calling request_method on theSCMLAWI
(awi
) member.unsigned_contracts
: A dynamic list of negotiations contracts concluded involving this agent but not yet signed. Correct management of this list is only possible if the agent **always* usesrequest_negotiation
method of this class (see methods later) rather than directly calling request_method on theSCMLAWI
(awi
) member.
Simulation attributes (also accessible through awi)
transportation_delay
: The transportation delay in the system.current_step
: Current simulation step.immediate_negotiations
: Whether or not negotiations start immediately upon registration (default is to start on the next production step)negotiation_speed_multiple
: The number of negotiation rounds (steps) conducted in a single production steptransportation_delay
: Transportation delay in the system. Default is zero
Methods (Callable by the agent)
Actions on the world
request_negotiation
: Called to request a negotiation based on aCFP
.
Scheduling and simulation helpers
can_expect_agreement
: Checks if it is possible in principle to get an agreement on this CFP by the time it becomes executable.
Callbacks (Callable by the simulation)
Decision callbacks (Called to make decisions)
Negotiation and Contracts
respond_to_negotiation_request
: Decide whether or not to engage in a negotiation on aCFP
that was published earlier by this factory manager. If accepted, the agent should return aSAONegotiator
object.sign_contract
: Decide whether or not to sign the contract. If accepted, the agent should return its own ID.confirm_contract_execution
: Decide whether or not to go on with executing a contract that the agent already signed. If rejected (by returningFalse
), a refusal-to-execute breach will be recorded.
Breach related
confirm_partial_execution
: Decide whether the agent agrees to partial execution. Called only when the the partner of this agent commits a partial breach (of level < 1) and this agent commits no breaches.set_renegotiation_agenda
: Decide what are the issues and ranges of acceptable values to re-negotiate about. Called only in case of breaches.respond_to_renegotiation_request
: Decide whether or not to engage in a re-negotiation.
Financial
confirm_loan
: Decide whether or not to accept an offered loan. *In ANAC 2019 league, loans are not allowed and this callback will never be called by the simulator.
Time-dependent callbacks (Information callback called at predefined times)
Information callbacks (Called to inform the agent about events)
CFP related
on_new_cfp
: Called whenever aCFP
on aProduct
for which the agent has already registered interest (usingregister_interest
method of itsawi
) is published. By default all agents register interest in the products they can consume or produce according to their profiles.on_remove_cfp
: Called whenever aCFP
on aProduct
for which the agent has already registered interest (usingregister_interest
method of itsawi
) is removed from the bulletin-board.
Negotiation related
on_neg_request_accepted
: Called when a negotiation request of the agent is acceptedon_neg_request_rejected
: Called when a negotiation request of the agent is rejectedon_negotiation_success
: Called when a negotiation of which the agent is a party succeeds with an agreement.on_negotiation_failure
: Called when a negotiation of which the agent is a party ends without agreement.
Contract related
on_contract_cancelled
: Called whenever aContract
of which the agent is a party is cancelled because the other party refused to sign it.on_contract_signed
: Called whenever aContract
of which the agent is a party is signed by both patners.on_contract_nullified
: Called whenever aContract
of which the agent is a party is nullified by the simulator as a part of bankruptcy processing.on_contract_executed
: Called when a contract executes completely and successfully.on_contract_breached
: Called when a contract is breached after complete contract processing.
Production and factory related
on_production_failure
: Called whenever a scheduled production (seeSCMLAWI
for production commands) failson_inventory_change
: Called whenever there is a change in the inventory (something is moved in or out or out of storage due to an event other than production (e.g. contract execution).on_cash_transfer
: Called whenever cash is transferred to or from the factory’s wallet.
About other agents
on_agent_bankrupt
: Called whenever another agent goes bankrupton_new_report
: Called whenever a new report of another agent for which this agent has registered interest is published. Interest is registered using the agent’sawi
‘sreceive_financial_reports
method.
Attributes Summary
A list of negotiation requests sent to this agent that are already accepted by it.
Returns the Agent-SCML2020World-Interface through which the agent does all of its actions in the world.
The unique ID of this entity
Was the agent initialized (i.e.
A convenient name of the entity (intended primarily for printing/logging/debugging).
A list of the negotiation requests sent by this agent that are not yet accepted or rejected.
The negotiations currently requested by the agent.
The negotiations currently requested by the agent.
Returns a short name of the type of this entity
Returns the name of the type of this entity
All contracts that are not yet signed.
The unique ID of this entity
Methods Summary
can_expect_agreement
(cfp, margin)Checks if it is possible in principle to get an agreement on this CFP by the time it becomes executable
checkpoint
(path[, file_name, info, …])Saves a checkpoint of the current object at the given path.
checkpoint_info
(file_name)Returns the information associated with a dump of the object saved in the given file
confirm_contract_execution
(contract)Called before executing any agreement
confirm_loan
(loan, bankrupt_if_rejected)called by the world manager to confirm a loan if needed by the buyer of a contract that is about to be breached
confirm_partial_execution
(contract, breaches)Will be called whenever a contract cannot be fully executed due to breaches by the other partner.
create
(*args, **kwargs)Creates an object and returns a proxy to it.
create_negotiation_request
(issues, partners, …)Creates a new
NegotiationRequestInfo
record and returns its IDfrom_checkpoint
(file_name[, return_info])Creates an object from a saved checkpoint
from_config
(config[, section, …])Creates an object of this class given the configuration info
init
()Called to initialize the agent after the world is initialized.
init_
()The initialization function called by the world directly.
notify
(notifiable, notification)on_agent_bankrupt
(agent_id)Will be called whenever any agent goes bankrupt
on_cash_transfer
(amount, cause)Received whenever money is transferred to the factory or from it.
on_contract_breached
(contract, breaches, …)Called after complete processing of a contract that involved a breach.
on_contract_cancelled
(contract, rejectors)Called whenever at least a partner did not sign the contract
on_contract_cancelled_
(contract, rejectors)Called whenever at least a partner did not sign the contract
on_contract_executed
(contract)Called after successful contract execution for which the agent is one of the partners.
on_contract_nullified
(contract, …)Will be called whenever a contract the agent is involved in is nullified because another partner went bankrupt
on_contract_signed
(contract)Called whenever a contract is signed by all partners
on_contract_signed_
(contract)Called whenever a contract is signed by all partners
on_contracts_finalized
(signed, cancelled, …)Called for all contracts in a single step to inform the agent about which were finally signed and which were rejected by any agents (including itself)
on_event
(event, sender)on_inventory_change
(product, quantity, cause)Received whenever something moves in or out of the factory’s storage
on_neg_request_accepted
(req_id, mechanism)Called when a requested negotiation is accepted
on_neg_request_accepted_
(req_id, mechanism)Called when a requested negotiation is accepted
on_neg_request_rejected
(req_id, by)Called when a requested negotiation is rejected
on_neg_request_rejected_
(req_id, by)Called when a requested negotiation is rejected
on_negotiation_failure
(partners, annotation, …)Called whenever a negotiation ends without agreement
on_negotiation_failure_
(partners, …)Called whenever a negotiation ends without agreement
on_negotiation_success
(contract, mechanism)Called whenever a negotiation ends with agreement
on_negotiation_success_
(contract, mechanism)Called whenever a negotiation ends with agreement
on_new_cfp
(cfp)Called when a new CFP for a product for which the agent registered interest is published
on_new_report
(report)Called whenever a financial report is published.
on_production_failure
(failures)Called with a list of
ProductionFailure
records on production failure.on_production_success
(reports)Called with a list of
ProductionReport
records on production successon_remove_cfp
(cfp)Called when a new CFP for a product for which the agent registered interest is removed
read_config
(config[, section])Reads the configuration from a file or a dict and prepares it for parsing
request_negotiation
(cfp[, negotiator, ufun])Requests a negotiation from the AWI while keeping track of available negotiation requests
respond_to_negotiation_request
(cfp, partner)Called when a prospective partner requests a negotiation to start
respond_to_negotiation_request_
(initiator, …)Called when a negotiation request is received
respond_to_renegotiation_request
(contract, …)Called to respond to a renegotiation request
set_renegotiation_agenda
(contract, breaches)Received by partners in ascending order of their total breach levels in order to set the renegotiation agenda when contract execution fails
sign_all_contracts
(contracts)Called to sign all contracts concluded in a single step by this agent
sign_contract
(contract)Called after the signing delay from contract conclusion to sign the contract.
step
()Called by the simulator at every simulation step
step_
()Called at every time-step.
Attributes Documentation
-
accepted_negotiation_requests
¶ A list of negotiation requests sent to this agent that are already accepted by it.
- Remarks:
These negotiations did not start yet as they are still not accepted by all partners. Once that happens, they will be moved to
running_negotiations
- Return type
List
[NegotiationInfo
]
-
awi
¶ Returns the Agent-SCML2020World-Interface through which the agent does all of its actions in the world.
A single excption is request_negotiation for which it is recommended to actually call the helper method on the agent itself instead of directly calling the AWI version.
- Return type
-
id
¶ The unique ID of this entity
-
initialized
¶ Was the agent initialized (i.e. was init_() called)
- Return type
bool
-
name
¶ A convenient name of the entity (intended primarily for printing/logging/debugging).
-
negotiation_requests
¶ A list of the negotiation requests sent by this agent that are not yet accepted or rejected.
- Remarks:
These negotiations did not start yet as they are still not accepted by all partners. Once that happens, they will be moved to
running_negotiations
- Return type
List
[NegotiationInfo
]
-
requested_negotiations
¶ The negotiations currently requested by the agent.
- Return type
List
[NegotiationRequestInfo
]- Returns
A list of negotiation request information objects (
NegotiationRequestInfo
)
-
running_negotiations
¶ The negotiations currently requested by the agent.
- Return type
List
[RunningNegotiationInfo
]- Returns
A list of negotiation information objects (
RunningNegotiationInfo
)
-
short_type_name
¶ Returns a short name of the type of this entity
-
type_name
¶ Returns the name of the type of this entity
-
unsigned_contracts
¶ All contracts that are not yet signed.
- Return type
List
[Contract
]
-
uuid
¶ The unique ID of this entity
Methods Documentation
-
can_expect_agreement
(cfp, margin)¶ Checks if it is possible in principle to get an agreement on this CFP by the time it becomes executable
- Parameters
margin (
int
) –cfp (
CFP
) –
Returns:
-
checkpoint
(path, file_name=None, info=None, exist_ok=False, single_checkpoint=True, step_attribs=('current_step', '_current_step', '_Entity__current_step', '_step'))¶ Saves a checkpoint of the current object at the given path.
- Parameters
path (
Union
[Path
,str
]) – Full path to a directory to store the checkpointfile_name (
Optional
[str
]) – Name of the file to dump into. If not given, a unique name is createdinfo (
Optional
[Dict
[str
,Any
]]) – Information to save with the checkpoint (must be json serializable)exist_ok (
bool
) – If true, override existing dumpsingle_checkpoint (
bool
) – If true, keep a single checkpoint for the last stepstep_attribs (
Tuple
[str
]) – Attributes to represent the time-step of the object. Any of the given attributes will be used in the file name generated if single_checkpoint is False. If single_checkpoint is True, the filename will not contain time-step information
- Return type
Path
- Returns
full path to the file used to save the checkpoint
-
classmethod
checkpoint_info
(file_name)¶ Returns the information associated with a dump of the object saved in the given file
- Parameters
file_name (
Union
[str
,Path
]) – Name of the object
Returns:
- Return type
Dict
[str
,Any
]
-
abstract
confirm_contract_execution
(contract)¶ Called before executing any agreement
- Return type
bool
-
abstract
confirm_loan
(loan, bankrupt_if_rejected)¶ called by the world manager to confirm a loan if needed by the buyer of a contract that is about to be breached
- Return type
bool
-
abstract
confirm_partial_execution
(contract, breaches)¶ Will be called whenever a contract cannot be fully executed due to breaches by the other partner.
- Parameters
contract (
Contract
) – The contract that was breachedbreaches (
List
[Breach
]) – A list of all the breaches committed.
Remarks:
Will not be called if both partners committed breaches.
- Return type
bool
-
classmethod
create
(*args, **kwargs)¶ Creates an object and returns a proxy to it.
-
create_negotiation_request
(issues, partners, annotation, negotiator, extra)¶ Creates a new
NegotiationRequestInfo
record and returns its ID- Parameters
issues (
List
[Issue
]) – negotiation issuespartners (
List
[str
]) – partnersannotation (
Optional
[Dict
[str
,Any
]]) – annotationnegotiator (
Optional
[Negotiator
]) – the negotiator to useextra (
Optional
[Dict
[str
,Any
]]) – any extra information
- Return type
str
- Returns
A unique identifier for this negotiation info structure
-
classmethod
from_checkpoint
(file_name, return_info=False)¶ Creates an object from a saved checkpoint
- Parameters
file_name (
Union
[str
,Path
]) –return_info – If True, tbe information saved when the file was dumped are returned
- Return type
Union
[NamedObject
,Tuple
[NamedObject
,Dict
[str
,Any
]]]- Returns
Either the object or the object and dump-info as a dict (if return_info was true)
Remarks:
- If info is returned, it is guaranteed to have the following members:
time: Dump time
type: Type of the dumped object
id: ID
name: name
-
classmethod
from_config
(config, section=None, ignore_children=True, try_parsing_children=True, scope=None)¶ Creates an object of this class given the configuration info
- Parameters
config (
Union
[str
,dict
]) – Either a file name or a dictionarysection (
Optional
[str
]) – A section in the file or a key in the dictionary to use for loading paramsignore_children (
bool
) – If true then children will be ignored and there will be a single returntry_parsing_children (
bool
) – If true the children will first be parsed asConfigReader
classes if they are nottypes (e.g. int, str, float, Iterable[int|str|float] (simple) –
scope – The scope at which to evaluate any child classes. This MUST be passed as scope=globals() if you are
any children that are to be parsed. (having) –
- Returns
An object of cls if ignore_children is True or a tuple with an object of cls and a dictionary with children that were not parsed.
Remarks:
This function will return an object of its class after passing the key-value pairs found in the config to the init function.
Requiring passing scope=globals() to this function is to get around the fact that in python eval() will be called with a globals dictionary based on the module in which the function is defined not called. This means that in general when eval() is called to create the children, it will not have access to the class definitions of these children (except if they happen to be imported in this file). To avoid this problem causing an undefined_name exception, the caller must pass her globals() as the scope.
-
abstract
init
()¶ Called to initialize the agent after the world is initialized. the AWI is accessible at this point.
-
init_
()[source]¶ The initialization function called by the world directly.
It does the following actions by default:
copies some of the static world settings to the agent to make them available without calling the AWI.
prepares production related properties like producing, consuming, line_profiles, compiled_profiles, etc.
registers interest in all products that the agent can produce or consume in its factory.
finally it calls any custom initialization logic implemented in `init`()
-
notify
(notifiable, notification)¶
-
abstract
on_agent_bankrupt
(agent_id)¶ Will be called whenever any agent goes bankrupt
- Parameters
agent_id (
str
) – The ID of the agent that went bankrupt
Remarks:
Agents can go bankrupt in two cases:
Failing to pay one installments of a loan they bought and refusing (or being unable to) get another loan to pay it.
Failing to pay a penalty on a sell contract they failed to honor (and refusing or being unable to get a loan to pay for it).
All built-in agents ignore this call and they use the bankruptcy list ONLY to decide whether or not to negotiate in their
on_new_cfp
andrespond_to_negotiation_request
callbacks by pulling the bulletin-board using the helper functionis_bankrupt
of their AWI.
- Return type
None
-
abstract
on_cash_transfer
(amount, cause)¶ Received whenever money is transferred to the factory or from it.
- Parameters
amount (
float
) – Amount of money (negative for transfers out of the factory, positive for transfers to it).cause (
str
) –The cause of the change. Possibilities include:
contract: Contract execution
insurance: Received from insurance company
bankruptcy: Liquidated due to bankruptcy
transfer: Arrival of transferred money (when transfer delay in the system is > 0).
- Return type
None
-
abstract
on_contract_breached
(contract, breaches, resolution)¶ Called after complete processing of a contract that involved a breach.
- Parameters
contract (
Contract
) – The contractbreaches (
List
[Breach
]) – All breaches committed (even if they were resolved)resolution (
Optional
[Contract
]) – The resolution contract if re-negotiation was successful. None if not.
- Return type
None
-
on_contract_cancelled
(contract, rejectors)¶ Called whenever at least a partner did not sign the contract
- Return type
None
-
on_contract_cancelled_
(contract, rejectors)¶ Called whenever at least a partner did not sign the contract
- Return type
None
-
abstract
on_contract_executed
(contract)¶ Called after successful contract execution for which the agent is one of the partners.
- Return type
None
-
abstract
on_contract_nullified
(contract, bankrupt_partner, compensation)¶ Will be called whenever a contract the agent is involved in is nullified because another partner went bankrupt
- Return type
None
-
on_contract_signed
(contract)¶ Called whenever a contract is signed by all partners
- Return type
None
-
on_contract_signed_
(contract)¶ Called whenever a contract is signed by all partners
- Return type
None
-
on_contracts_finalized
(signed, cancelled, rejectors)¶ Called for all contracts in a single step to inform the agent about which were finally signed and which were rejected by any agents (including itself)
- Parameters
signed (
List
[Contract
]) – A list of signed contracts. These are bindingcancelled (
List
[Contract
]) – A list of cancelled contracts. These are not bindingrejectors (
List
[List
[str
]]) – A list of lists where each of the internal lists gives the rejectors of one of the cancelled contracts. Notice that it is possible that this list is empty which means that the contract other than being rejected by any agents (if that was possible in the specific world).
Remarks:
The default implementation is to call
on_contract_signed
for singed contracts andon_contract_cancelled
for cancelled contracts- Return type
None
-
on_event
(event, sender)¶
-
abstract
on_inventory_change
(product, quantity, cause)¶ Received whenever something moves in or out of the factory’s storage
- Parameters
product (
int
) – Product index.quantity (
int
) – Negative value for products moving out and positive value for products moving incause (
str
) –The cause of the change. Possibilities include:
contract: Contract execution
insurance: Received from insurance company
bankruptcy: Liquidated due to bankruptcy
transport: Arrival of goods (when transportation delay in the system is > 0).
- Return type
None
-
abstract
on_neg_request_accepted
(req_id, mechanism)¶ Called when a requested negotiation is accepted
-
on_neg_request_accepted_
(req_id, mechanism)¶ Called when a requested negotiation is accepted
-
abstract
on_neg_request_rejected
(req_id, by)¶ Called when a requested negotiation is rejected
- Parameters
req_id (
str
) – The request ID passed to _request_negotiationby (
Optional
[List
[str
]]) – A list of agents that refused to participate or None if the failure was for another reason
-
on_neg_request_rejected_
(req_id, by)¶ Called when a requested negotiation is rejected
- Parameters
req_id (
str
) – The request ID passed to _request_negotiationby (
Optional
[List
[str
]]) – A list of agents that refused to participate or None if the failure was for another reason
-
abstract
on_negotiation_failure
(partners, annotation, mechanism, state)¶ Called whenever a negotiation ends without agreement
- Return type
None
-
on_negotiation_failure_
(partners, annotation, mechanism, state)¶ Called whenever a negotiation ends without agreement
- Return type
None
-
abstract
on_negotiation_success
(contract, mechanism)¶ Called whenever a negotiation ends with agreement
- Return type
None
-
on_negotiation_success_
(contract, mechanism)¶ Called whenever a negotiation ends with agreement
- Return type
None
-
abstract
on_new_cfp
(cfp)¶ Called when a new CFP for a product for which the agent registered interest is published
-
abstract
on_new_report
(report)¶ Called whenever a financial report is published.
- Parameters
report (
FinancialReport
) – The financial report giving details of the standing of an agent at some time (seeFinancialReport
)
Remarks:
Agents must opt-in to receive these calls by calling
receive_financial_reports
on their AWI
-
abstract
on_production_failure
(failures)[source]¶ Called with a list of
ProductionFailure
records on production failure.- Return type
None
-
abstract
on_production_success
(reports)[source]¶ Called with a list of
ProductionReport
records on production success- Return type
None
-
abstract
on_remove_cfp
(cfp)¶ Called when a new CFP for a product for which the agent registered interest is removed
-
classmethod
read_config
(config, section=None)¶ Reads the configuration from a file or a dict and prepares it for parsing
- Parameters
config (
Union
[str
,dict
]) – Either a file name or a dictionarysection (
Optional
[str
]) – A section in the file or a key in the dictionary to use for loading params
- Return type
Dict
[str
,Any
]- Returns
A dict ready to be parsed by from_config
Remarks:
-
request_negotiation
(cfp, negotiator=None, ufun=None)¶ Requests a negotiation from the AWI while keeping track of available negotiation requests
- Parameters
cfp (
CFP
) –negotiator (
Optional
[Negotiator
]) –ufun (
Optional
[UtilityFunction
]) –
- Return type
bool
- Returns
Whether the negotiation request was successful indicating that the partner accepted the negotiation
-
abstract
respond_to_negotiation_request
(cfp, partner)¶ Called when a prospective partner requests a negotiation to start
- Return type
Optional
[Negotiator
]
-
respond_to_negotiation_request_
(initiator, partners, issues, annotation, mechanism, role, req_id)¶ Called when a negotiation request is received
- Return type
Optional
[Negotiator
]
-
abstract
respond_to_renegotiation_request
(contract, breaches, agenda)¶ Called to respond to a renegotiation request
- Parameters
agenda (
RenegotiationRequest
) –contract (
Contract
) –breaches (
List
[Breach
]) –
Returns:
- Return type
Optional
[Negotiator
]
-
abstract
set_renegotiation_agenda
(contract, breaches)¶ Received by partners in ascending order of their total breach levels in order to set the renegotiation agenda when contract execution fails
- Parameters
contract (
Contract
) – The contract being breachedbreaches (
List
[Breach
]) – All breaches oncontract
- Return type
Optional
[RenegotiationRequest
]- Returns
Renegotiation agenda (issues to negotiate about to avoid reporting the breaches).
-
sign_all_contracts
(contracts)¶ Called to sign all contracts concluded in a single step by this agent
- Remarks:
default implementation calls
sign_contract
for each contract returning the results
- Return type
List
[Optional
[str
]]
-
sign_contract
(contract)¶ Called after the signing delay from contract conclusion to sign the contract. Contracts become binding only after they are signed.
- Return type
Optional
[str
]
-
abstract
step
()¶ Called by the simulator at every simulation step