Using PenaltyModel Factories¶
penaltymodel provides functionality for accessing PenaltyModel factories.
Accessing Factories¶
Any factories that have been identified through the FACTORY_ENTRYPOINT entrypoint
and installed on the python path can be accessed through the get_penalty_model()
function.
Examples
>>> import networkx as nx
>>> import dimod
>>> graph = nx.path_graph(5)
>>> decision_variables = (0, 4) # the ends of the path
>>> feasible_configurations = {(-1, -1), (1, 1)} # we want the ends of the path to agree
>>> spec = pm.Specification(graph, decision_variables, feasible_configurations, dimod.SPIN)
>>> widget = pm.get_penalty_model(spec)
Functions and Utilities¶
-
FACTORY_ENTRYPOINT= 'penaltymodel_factory'¶ constant used when assigning entrypoints for factories.
Type: str
-
CACHE_ENTRYPOINT= 'penaltymodel_cache'¶ constant used when assigning entrypoints for caches.
Type: str
-
get_penalty_model(specification)[source]¶ Retrieve a PenaltyModel from one of the available factories.
Parameters: specification ( Specification) – The specification for the desired PenaltyModel.Returns: A PenaltyModel as returned by the highest priority factory, or None if no factory could produce it. Return type: PenaltyModel/NoneRaises: ImpossiblePenaltyModel– If the specification describes a penalty model that cannot be built by any factory.
-
penaltymodel_factory(priority)[source]¶ Decorator to assign a priority attribute to the decorated function.
Parameters: priority (int) – The priority of the factory. Factories are queried in order of decreasing priority. Examples
Decorate penalty model factories like:
>>> @pm.penaltymodel_factory(105) ... def factory_function(spec): ... pass >>> factory_function.priority 105
-
iter_factories()[source]¶ Iterate through all factories identified by the factory entrypoint.
Yields: function – A function that accepts a Specificationand returns aPenaltyModel.