-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add out-of-the-box belief revision functions
Closes #236 Implements a naive/monotonic and a priority rule-based revision function
- Loading branch information
Showing
9 changed files
with
211 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
spec/src/agent/beliefRevision/revisionFunctions.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const Agent = require('../../../../src/agent/Agent') | ||
const Belief = require('../../../../src/agent/Belief') | ||
const { | ||
reviseSimpleNonmonotonic, | ||
reviseMonotonic, | ||
revisePriority } = require('../../../../src/agent/beliefRevision/revisionFunctions') | ||
|
||
const { | ||
beliefs, | ||
desires, | ||
plans | ||
} = require('../../../mocks/human') | ||
|
||
describe('revisionFunctions', () => { | ||
|
||
it('by default, the belief revision function is simple non-monotonic and updates existing beliefs', () => { | ||
const newAgent = new Agent({ | ||
id: 'myAgent', | ||
beliefs, | ||
desires, | ||
plans, | ||
selfUpdatesPossible: false | ||
}) | ||
newAgent.next({ ...Belief('dogNice', false) }) | ||
|
||
const alternativeAgent = new Agent({ | ||
id: 'alternativeAgent', | ||
beliefs, | ||
desires, | ||
plans, | ||
selfUpdatesPossible: false, | ||
reviseBeliefs: reviseSimpleNonmonotonic | ||
}) | ||
alternativeAgent.next({ ...Belief('dogNice', false) }) | ||
expect(alternativeAgent.beliefs.dogNice).toBe(false) | ||
expect(alternativeAgent.beliefs.dogNice).toEqual(newAgent.beliefs.dogNice) | ||
}) | ||
|
||
it('should support a monotonic belief revision function that rejects updates to existing beliefs', () => { | ||
const newAgent = new Agent({ | ||
id: 'myAgent', | ||
beliefs, | ||
desires, | ||
plans, | ||
selfUpdatesPossible: false, | ||
reviseBeliefs: reviseMonotonic | ||
}) | ||
newAgent.next({ ...Belief('dogNice', false), ...Belief('weather', 'sunny') }) | ||
expect(newAgent.beliefs.dogNice).toBe(true) | ||
expect(newAgent.beliefs.weather).toBe('sunny') | ||
}) | ||
|
||
it('should support a nonmonotonic belief revision function based on priority rules', () => { | ||
const beliefBase = { | ||
isRaining: Belief('isRaining', true, 0), | ||
temperature: Belief('temperature', 10, 0), | ||
propertyValue: Belief('propertyValue', 500000, 1) | ||
} | ||
|
||
const update = { | ||
isRaining: Belief('isRaining', false, 0), | ||
temperature: Belief('temperature', 15, 1), | ||
propertyValue: Belief('propertyValue', 250000, 0) | ||
} | ||
const newAgent = new Agent({ | ||
id: 'myAgent', | ||
beliefs: beliefBase, | ||
desires, | ||
plans, | ||
selfUpdatesPossible: false, | ||
reviseBeliefs: revisePriority | ||
}) | ||
newAgent.next(update) | ||
expect(newAgent.beliefs.isRaining.value).toBe(false) | ||
expect(newAgent.beliefs.temperature.value).toEqual(15) | ||
expect(newAgent.beliefs.propertyValue.value).toEqual(500000) | ||
}) | ||
|
||
it('should not allow overriding beliefs of infinite high priority', () => { | ||
const beliefBase = { | ||
isRaining: Belief('isRaining', true, Infinity), | ||
temperature: Belief('temperature', 10, Infinity) | ||
} | ||
|
||
const update = { | ||
isRaining: Belief('isRaining', false, Infinity), | ||
temperature: Belief('temperature', 15, undefined) | ||
} | ||
const newAgent = new Agent({ | ||
id: 'myAgent', | ||
beliefs: beliefBase, | ||
desires, | ||
plans, | ||
selfUpdatesPossible: false, | ||
reviseBeliefs: revisePriority | ||
}) | ||
newAgent.next(update) | ||
expect(newAgent.beliefs.isRaining.value).toBe(true) | ||
expect(newAgent.beliefs.temperature.value).toEqual(10) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Revises beliefs by merging old and new beliefs such that a new belief overwrites an old one in | ||
* case of conflict. | ||
* @param {object} oldBeliefs Old belief base (JSON object of beliefs) | ||
* @param {object} newBeliefs New belief base (JSON object of beliefs) | ||
* @returns Revised belief base (JSON object of beliefs) | ||
*/ | ||
const reviseSimpleNonmonotonic = (oldBeliefs, newBeliefs) => ({ ...oldBeliefs, ...newBeliefs }) | ||
|
||
/** | ||
* Revises beliefs by merging old and new beliefs such that an old belief overrides a new one in | ||
* case of conflict. | ||
* @param {object} oldBeliefs Old belief base (JSON object of beliefs) | ||
* @param {object} newBeliefs New belief base (JSON object of beliefs) | ||
* @returns Revised belief base (JSON object of beliefs) | ||
*/ | ||
const reviseMonotonic = (oldBeliefs, newBeliefs) => ({ ...newBeliefs, ...oldBeliefs }) | ||
|
||
/** | ||
* Revises beliefs by merging old and new beliefs such that an old belief overrides a new one in | ||
* case of conflict | ||
* @param {object} oldBeliefs Old belief base (JSON object of beliefs) | ||
* @param {object} newBeliefs New belief base (JSON object of beliefs) | ||
* @returns Revised belief base (JSON object of beliefs) | ||
*/ | ||
const revisePriority = (oldBeliefs, newBeliefs) => Object.fromEntries( | ||
new Map( | ||
Object.keys(newBeliefs).map(key => | ||
!key in oldBeliefs || oldBeliefs[key].priority == 0 || oldBeliefs[key].priority < newBeliefs[key].priority ? [key, newBeliefs[key]] : [key, oldBeliefs[key]] | ||
) | ||
) | ||
) | ||
|
||
module.exports = { | ||
reviseSimpleNonmonotonic, | ||
reviseMonotonic, | ||
revisePriority | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters