Section 3 - Agents and some user facilities
and
last week (and
profit ?profit\
price ?lastPrice)\
notInferred
less ?profit 0
with consequent:
all week (price ?lastPrice)
and a rule called change price with antecedent:
and
time week ?date\
last week (and
profit ?profit\
price ?lastPrice)\
less ?profit 0\
randomChoice [?otherProfit ?otherPrice] ?pair [?date]
(and
last week (producer ?prod)\
notInferred
= ?prod self\
at ?prod (last week (profit ?prof))\
at ?prod (last week (price ?pric))\
= ?pair [?prof ?pric])\
is ?profitDifference ?otherProfit - ?profit\
is ?profitSign ?profitDifference / (absoluteValue ?profitDifference)\
is ?priceDifference ?otherPrice - ?lastPrice\
is ?priceSign ?priceDifference / (absoluteValue ?priceDifference)\
is ?prodSign ?profitSign * ?priceSign\
(or
(and
greater ?prodSign 0\
raiseProportion ?prop)\
(and
less ?prodSign 0\
lowerProportion ?prop))\
is ?newPrice ?lastPrice * ?prop
with consequent:
all week (price ?newPrice)
This is a reasonably complex rule. The first few lines retrieves last week's profit and price and ensures that this rule only fires if last week it made a loss. The randomChoice primitive acts as if it makes a list of all the bindings that satisfy the subclause (which is merely a list of other producers' profits and prices last week) and randomly chooses one profit-price pair. The last bit checks whether the chosen comparison pair did better then itself - if it did it somewhat emulates its price (by raising or lowering its price by the set amount), otherwise it moves its price in the opposite direction. It is thus learning from observing another producer.
Now browse the Consumer type (do this via browse module, Type Hierarchy and browse type). And look at the clause definitions under the categories decisions and parameters for the week.
Notice that these are simpler than before as they do not have to specify the consumer they refer to each time because they will occur in different instances Consumer already.
In the Initial iteration rulebase of this create a category called setup for the week and create a rule called money with antecedent:
and
moneyRange ?minMoney ?maxMoney\
randomNumber ?rn1 [getsMoney]\
is ?money (truncated ?rn1 * (?maxMoney - ?minMoney) + ?minMoney)
and consequent:
all week (getsMoney ?money)
Notice that we do not have to say at model (moneyRange ...) because moneyRange is defined at the ModelAgent type so there is no chance of ambiguity (we can do this because we added a container constraint in Consumer from ModelAgent).
Similarly write a rule to set the requires ?amount clause for the week.
Now in the Content rulebase add a category called transactions. Create a rule called talking with antecedent:
and
time iteration 1\
chatFactor ?no\
inInterval ?i 1 ?no\
time week ?date\
randomChoice ?conversant ?cons [?i ?date]
(and
consumer ?cons\
notInferred
= ?cons self)\
last week (favourite ?prod)
and consequent:
at ?conversant (all week (try ?prod))
There will only be two iterations per week, one for chatting and one for buying. This rule will only fire in iteration 2. The rest of the rule chooses one other consumer and conveys its favourite to that consumer's database using at.
The next rule controls the choice and purchase by the consumer agent. As in the last sections's sessions the consumer randomly samples some producers, but this time it also adds in its own favourite as well as any producers suggested to it during the talking session.
and
time iteration 2\
choice ?no\
time week ?date\
randomList ?prodList ?prod ?date
producer ?prod\
subList ?preChosenProds ?prodList 1 ?no\
sortedList ?chosenProds ?prod
(or
includes ?preChosenProds ?prod\
try ?prod\
last week (favourite ?prod))\
minValue ?minPrice ?price
(and
includes ?chosenProds ?chosenProd\
at ?chosenProd (price ?price))\
fixedRandomChoice ?bestProd ?prod ["buying" ?date]
(and
includes ?chosenProds ?prod\
at ?prod (price ?minPrice))\
requires ?req\
getsMoney ?money\
is ?volBought (min ?req (truncated ?money / ?minPrice))\
is ?spent ?volBought * ?minPrice
and consequent:
all week (and
bought ?volBought ?minPrice\
favourite ?bestProd\
transaction self ?volBought ?bestProd ?minPrice\
at ?bestProd (sold ?volBought ?minPrice))
I have not used backward chaing rules to simplify the rule this time. Work through this rule clause by clause to check you have some idea what it is doing. Note the use of the symbol self in the consequent. There are now only the accounting rules in Producer and the rules to determine the survival of producers to write.
Create an accounting category in the Final iteration rulebase of the Producer type. Write a rule called profit to calculate profit with antecedent:
and
marginalCost ?mc\
fixedCost ?fc\
total ?income ?money
(and
sold ?amount ?price\
is ?money ?amount * ?price)\
total ?vol ?amount
sold ?amount ?price\
is ?profit ?income - ?vol * ?mc - ?fc
and consequent:
profit ?profit
Also create a rule to calculate the new capital with antecedent:
and
(or
initialCapital ?oldCapital\
last week (capital ?oldCapital))\
profit ?profit\
is ?newCapital ?oldCapital + ?profit
and consequent:
capital ?newCapital
Finally we write a rule to carry over surviving producers.
Write a rule in the Initial iteration rulebase of the ModelAgent type to bind with all producers that existed last week and their associated capital (using at ?producer (...)) and assert they exist and are active this week.
Now restart the simulation and see the combined effect.
Generated with CERN WebMaker