Rule Agent

The module holding RuleAgent, an agent which evaluates artifacts using its rules.

class creamas.rules.agent.RuleAgent(*args, **kwargs)[source]

Base class for agents using rules to evaluate artifacts.

In addition to common attributes inherited from CreativeAgent, rule agents have following attributes:

Variables
  • R (list) – rules agent uses to evaluate artifacts

  • W (list) – Weight for each rule in R, in [-1,1].

add_rule(rule, weight)[source]

Add rule to R with initial weight.

Parameters
  • rule (~creamas.core.rule.Rule) – rule to be added

  • weight (float) – initial weight for the rule

Raises

TypeError – if rule is not subclass of Rule

Returns

True if rule was successfully added, otherwise False.

Rtype bool

evaluate(artifact)[source]

Evaluate artifact with agent’s current rules and weights.

Parameters

artifact (Artifact) – Artifact to be evaluated

Returns

Agent’s evaluation of the artifact, in [-1,1], and framing. In this basic implementation framing is always None.

Return type

tuple

Actual evaluation formula in this basic implementation is:

\[e(A) = \frac{\sum_{i=1}^{n} r_{i}(A)w_i} {\sum_{i=1}^{n} \lvert w_i \rvert},\]

where \(r_{i}(A)\) is the \(i\) th rule’s evaluation on artifact \(A\), and \(w_i\) is the weight for rule \(r_i\).

get_weight(rule)[source]

Get weight for rule.

If rule is not in R, returns None.

remove_rule(rule)[source]

Remove rule from R and its corresponding weight from W.

Parameters

rule (Rule or RuleLeaf) – rule to remove

Raises

TypeError – If rule is not derived from Rule or RuleLeaf.

Returns

True if the rule was successfully removed, otherwise False.

Rtype bool

set_weight(rule, weight)[source]

Set weight for rule in R.

Adds the rule if it is not in R.

property R

Rules agent uses to evaluate artifacts. Each rule in R is expected to be a callable with a single parameter, the artifact to be evaluated. Callable should return a float in [-1,1]; where 1 means that rule is very prominent in the artifact; 0 means that there is none of that rule in the artifact; -1 means that the artifact shows traits opposite to the rule.

property W

Weights for the rules.

Each weight should be in [-1,1].