Skip to content

Commit 4960fb5

Browse files
authored
Merge pull request #240 from TimKam/238-keep-initial-priorities
Allow keeping belief priorities
2 parents 20bb0c7 + 45a80f0 commit 4960fb5

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,22 @@ After applying the belief update, our agent's belief base is as follows:
630630
631631
Note that in detail, the priorities are interpreted as follows:
632632
633-
* If a belief exists in the update, but not in the agent's belief base, this belief is added.
634-
* If the belief's priority is 0 in the belief base and a belief with the same key exists in the update, the agent's belief is overridden; this behavior is desired for beliefs that are generally defeasible.
635-
* If a belief's priority in the update is higher than the same belief's priority in the agent's belief base, the agent's belief is overridden.
633+
* If a belief exists in the update, but not in the agent's belief base, this belief is added.
634+
* If the belief's priority is 0 in the belief base and a belief with the same key exists in the update, the agent's belief is overridden; this behavior is desired for beliefs that are generally defeasible.
635+
* If a belief's priority in the update is higher than the same belief's priority in the agent's belief base, the agent's belief is overridden.
636+
637+
A potential issue that the belief revision function we use above does not address is that it essentially requires the *inflation* of priorities in case of regular successful revisions of beliefs with a non-zero priority.
638+
For example, in order to update ``the belief \verb|propertyValue: { value: 500000, priority: 1 }``, a new ``propertyValue`` belief can only defeat the belief if its priority is ``2`` or higher; the subsequent defeater will then require a priority of ``3``, and so on.
639+
We can address this issue by defining whether a particular belief (or beliefs in general) should, when defeated, adopt the priority of their defeater.
640+
641+
When using ``JSson.revisionFunctions.revisePriorityStatic`` as our belief revision function, the priority of the initial beliefs are maintained. Alternatively, we can specify whether or not a belief's priority should be updated, on the level of the individual belief:
642+
643+
```JavaScript
644+
const beliefBase = {
645+
isRaining: Belief('isRaining', true, Infinity, true),
646+
temperature: Belief('temperature', 10, Infinity, false)
647+
}
648+
```
636649
637650
## Messaging
638651
JS-son agents can send "private" messages to any other JS-son agent, which the environment will then relay to this agent only.

spec/src/agent/Belief.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe('belief()', () => {
99
expect(Belief('test', 'test')).toEqual({ test: 'test' })
1010
})
1111

12-
it('should create a new belief with the specified key, value (explicitly managed), and priority', () => {
13-
expect(Belief('test', 'test', 1)).toEqual({ test: 'test', value: 'test', priority: 1 })
12+
it('should create a new belief with the specified key, value (explicitly managed), priority, and priority update spec', () => {
13+
expect(Belief('test', 'test', 1)).toEqual({ test: 'test', value: 'test', priority: 1, updatePriority: false })
1414
})
1515

1616
it('should not throw a warning if belief is of a JSON data type', () => {

spec/src/agent/beliefRevision/revisionFunctions.spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const Belief = require('../../../../src/agent/Belief')
33
const {
44
reviseSimpleNonmonotonic,
55
reviseMonotonic,
6-
revisePriority } = require('../../../../src/agent/beliefRevision/revisionFunctions')
6+
revisePriority,
7+
revisePriorityStatic } = require('../../../../src/agent/beliefRevision/revisionFunctions')
78

89
const {
910
beliefs,
@@ -98,4 +99,38 @@ describe('revisionFunctions', () => {
9899
expect(newAgent.beliefs.isRaining.value).toBe(true)
99100
expect(newAgent.beliefs.temperature.value).toEqual(10)
100101
})
102+
103+
it('should allow configuring a belief such that its initial priority is kept', () => {
104+
const beliefBase = { isRaining: Belief('isRaining', true, 1, false) }
105+
106+
const update = { isRaining: Belief('isRaining', false, 2) }
107+
108+
const newAgent = new Agent({
109+
id: 'myAgent',
110+
beliefs: beliefBase,
111+
desires,
112+
plans,
113+
selfUpdatesPossible: false,
114+
reviseBeliefs: revisePriority
115+
})
116+
newAgent.next(update)
117+
expect(newAgent.beliefs.isRaining.priority).toBe(1)
118+
})
119+
120+
it('should allow configuring a priority belief revision function such that the initial priorities of it beliefs are generally kept', () => {
121+
const beliefBase = { isRaining: Belief('isRaining', true, 1) }
122+
123+
const update = { isRaining: Belief('isRaining', false, 2) }
124+
125+
const newAgent = new Agent({
126+
id: 'myAgent',
127+
beliefs: beliefBase,
128+
desires,
129+
plans,
130+
selfUpdatesPossible: false,
131+
reviseBeliefs: revisePriorityStatic
132+
})
133+
newAgent.next(update)
134+
expect(newAgent.beliefs.isRaining.priority).toBe(1)
135+
})
101136
})

src/agent/Belief.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ const warning = 'JS-son: Created belief with non-JSON object, non-JSON data type
55
* @param {string} id the belief's unique identifier
66
* @param {any} value the belief's value
77
* @param {number} priority the belief's priority in case of belief revision; optional
8+
* @param {boolean} updatePriority whether in case of a belief update, the priority of the defeating belief should be adopted; optional, defaults to true
89
* @returns {object} JS-son agent belief
910
*/
10-
const Belief = (id, value, priority) => {
11+
const Belief = (id, value, priority, updatePriority=false) => {
1112
const belief = {}
1213
belief[id] = value
1314
if (priority || priority === 0) {
1415
belief.priority = priority
1516
belief['value'] = value
17+
belief.updatePriority = updatePriority
1618
}
1719
try {
1820
const parsedBelief = JSON.parse(JSON.stringify(belief))

src/agent/beliefRevision/revisionFunctions.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ const reviseMonotonic = (oldBeliefs, newBeliefs) => ({ ...newBeliefs, ...oldBeli
2626
const revisePriority = (oldBeliefs, newBeliefs) => Object.fromEntries(
2727
new Map(
2828
Object.keys(newBeliefs).map(key =>
29-
!key in oldBeliefs || oldBeliefs[key].priority == 0 || oldBeliefs[key].priority < newBeliefs[key].priority ? [key, newBeliefs[key]] : [key, oldBeliefs[key]]
29+
!key in oldBeliefs || oldBeliefs[key].priority == 0 || oldBeliefs[key].priority < newBeliefs[key].priority ? oldBeliefs[key].updatePriority ? [key, newBeliefs[key]] : [key, { ...oldBeliefs[key], value: newBeliefs[key].value }] : [key, oldBeliefs[key]]
3030
)
3131
)
3232
)
3333

34+
/**
35+
* Revises beliefs by merging old and new beliefs such that an old belief overrides a new one in. Does not update belief priorities.
36+
* case of conflict
37+
* @param {object} oldBeliefs Old belief base (JSON object of beliefs)
38+
* @param {object} newBeliefs New belief base (JSON object of beliefs)
39+
* @returns Revised belief base (JSON object of beliefs)
40+
*/
41+
42+
const revisePriorityStatic = (oldBeliefs, newBeliefs) => revisePriority(Object.fromEntries(
43+
new Map(
44+
Object.keys(oldBeliefs).map(key => [
45+
key,
46+
{ ...oldBeliefs[key], updatePriority: false }
47+
])
48+
)
49+
), newBeliefs)
50+
3451
module.exports = {
3552
reviseSimpleNonmonotonic,
3653
reviseMonotonic,
37-
revisePriority
54+
revisePriority,
55+
revisePriorityStatic
3856
}

0 commit comments

Comments
 (0)