Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Belief and BeliefSet #21

Merged
merged 24 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
92f9063
feat: add Belief, IBelief and Beliefset
JensSteenmetz Mar 18, 2024
1c015f9
docs: add documentation
JensSteenmetz Mar 18, 2024
4e74455
test: add tests for Belief
JensSteenmetz Mar 18, 2024
87b1a94
test: add tests for Beliefset
JensSteenmetz Mar 18, 2024
e7d52fc
fix: fix Reflection bug in Beliefset
JensSteenmetz Mar 18, 2024
78f41a1
feat: merge main into feat/100-beliefset-necessities
JensSteenmetz Mar 18, 2024
85313f7
fix: make Beliefset constructor protected
JensSteenmetz Mar 18, 2024
335d767
docs: change summaries to remarks
JensSteenmetz Mar 18, 2024
a5c2b7b
fix: adhere to naming conventions for unit tests
JensSteenmetz Mar 18, 2024
768cffd
chore: change name Beliefset to BeliefSet
JensSteenmetz Mar 20, 2024
87c11cd
chore: change name Beliefset to BeliefSet
JensSteenmetz Mar 20, 2024
e701cef
chore: change name Beliefset to BeliefSet in tests
JensSteenmetz Mar 21, 2024
255ce7a
fix: change name TResource to TObservation and add more tests
JensSteenmetz Mar 21, 2024
e7a80e2
fix: improve documentation and namings
JensSteenmetz Mar 21, 2024
bc9b91a
fix: improve IBelief and Belief summaries
JensSteenmetz Mar 21, 2024
76245f8
fix: more improvements to docs and tests
JensSteenmetz Mar 21, 2024
6dbd151
chore: correct test comments
JensSteenmetz Mar 24, 2024
82aafb8
chore: rename Beliefset.cs to BeliefSet.cs
JensSteenmetz Mar 24, 2024
611d874
chore: rename BeliefsetTests.cs to BeliefSetTests.cs
JensSteenmetz Mar 24, 2024
2f4830d
chore: merge main into feat/100-beliefset-necessities
JensSteenmetz Mar 25, 2024
ab98d40
chore: remove Believe folder
JensSteenmetz Mar 25, 2024
24b63da
fix: use actual BeliefSet in Goals
JensSteenmetz Mar 25, 2024
adfe5c7
feat: add IBeliefSet
JensSteenmetz Mar 25, 2024
22e4986
chore: rename believe to belief in comments
JensSteenmetz Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Aplib.Core/Belief/Belief.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;

namespace Aplib.Core.Belief
{
/// <summary>
/// The <see cref="Belief{TReference, TResource}"/> class represents a belief.
/// Some <i>object reference</i> is used to generate/update a <i>resource</i>
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// (i.e., some piece of information on the game state as perceived by an agent).
/// </summary>
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// <remarks>
/// It implements the <see cref="IBelief"/> interface.
/// It supports implicit conversion to <typeparamref name="TResource"/>.
/// </remarks>
/// <typeparam name="TReference">The type of the reference used to generate/update the resource.</typeparam>
/// <typeparam name="TResource">The type of the resource the belief represents.</typeparam>
public class Belief<TReference, TResource> : IBelief
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// The reference used to generate/update the resource.
/// </summary>
private readonly TReference _reference;
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// A function that takes a reference and generates/updates a resource.
/// </summary>
private readonly Func<TReference, TResource> _getResourceFromReference;
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// A function that sets a condition on when the resource should be updated.
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
private readonly Func<bool> _updateIf = () => true;
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The resource represented by the belief (i.e., some piece of information on the game state as perceived by an agent).
/// </summary>
private TResource _resource;

/// <summary>
/// Initializes a new instance of the <see cref="Belief{TReference, TResource}"/> class with a reference,
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// and a function to generate/update the resource using the reference.
/// </summary>
/// <param name="reference">The reference used to generate/update the resource.</param>
/// <param name="getResourceFromReference">A function that takes a reference and generates/updates a resource.</param>
public Belief(TReference reference, Func<TReference, TResource> getResourceFromReference)
{
_reference = reference;
_getResourceFromReference = getResourceFromReference;
_resource = _getResourceFromReference(_reference);
}

/// <summary>
/// Initializes a new instance of the <see cref="Belief{TReference, TResource}"/>
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="reference">The reference used to generate/update the resource.</param>
/// <param name="getResourceFromReference">A function that takes a reference and generates/updates a resource.</param>
/// <param name="updateIf">A function that sets a condition on when the resource should be updated.</param>
public Belief(TReference reference, Func<TReference, TResource> getResourceFromReference, Func<bool> updateIf)
: this(reference, getResourceFromReference)
{
_updateIf = updateIf;
}

/// <summary>
/// Implicit conversion operator to allow a <see cref="Belief{TReference, TResource}"/> object
/// to be used where a <typeparamref name="TResource"/> is expected.
/// </summary>
/// <param name="belief">The <see cref="Belief{TReference, TResource}"/> object to convert.</param>
public static implicit operator TResource(Belief<TReference, TResource> belief) => belief._resource;

/// <summary>
/// Generates/updates the resource if the updateIf condition is satisfied.
/// The resource is then updated by calling the getResourceFromReference function.
/// </summary>
public void UpdateBelief()
joachimdekker marked this conversation as resolved.
Show resolved Hide resolved
{
if (_updateIf()) _resource = _getResourceFromReference(_reference);
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
40 changes: 40 additions & 0 deletions Aplib.Core/Belief/Beliefset.cs
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Linq;

namespace Aplib.Core.Belief
{
/// <summary>
/// The <see cref="BeliefSet"/> class can be inherited to define a set of beliefs for an agent.
/// </summary>
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// <remarks>
/// All <i>public fields</i> of type <see cref="IBelief"/> that are defined in the inheriting class
/// are automatically updated when calling <see cref="UpdateBeliefs"/>.
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// </remarks>
public abstract class BeliefSet
{
private readonly IBelief[] _beliefs;

/// <summary>
/// Initializes a new instance of the <see cref="BeliefSet"/> class.
/// </summary>
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// <remarks>
/// All <i>public fields</i> of type <see cref="IBelief"/> that are defined in the inheriting class
/// are automatically updated when calling <see cref="UpdateBeliefs"/>.
/// </remarks>
JoenvandeVorle marked this conversation as resolved.
Show resolved Hide resolved
protected BeliefSet()
{
_beliefs =
GetType().GetFields()
.Where(field => typeof(IBelief).IsAssignableFrom(field.FieldType))
.Select(field => (IBelief)field.GetValue(this))
.ToArray();
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Updates all objects of type <see cref="IBelief"/> that are defined as <i>public fields</i> in <see cref="BeliefSet"/>.
/// </summary>
public void UpdateBeliefs()
joachimdekker marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (IBelief belief in _beliefs) belief.UpdateBelief();
}
}
}
16 changes: 16 additions & 0 deletions Aplib.Core/Belief/IBelief.cs
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Aplib.Core.Belief
{
/// <summary>
/// The <see cref="IBelief"/> interface defines a contract for classes that represent a belief.
/// </summary>
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// <remarks>
/// A belief is some piece of information on the game state as perceived by an agent.
/// </remarks>
public interface IBelief
{
/// <summary>
/// Updates the belief based on information of the game state.
/// </summary>
void UpdateBelief();
}
}
93 changes: 93 additions & 0 deletions Aplib.Tests/Core/Belief/BeliefTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Aplib.Core.Belief;

namespace Aplib.Tests.Core.Belief;
public class BeliefTests
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
{
private static TReference ID<TReference>(TReference reference) => reference;

private static int GetCount<T>(IEnumerable<T> reference) => reference.Count();

private static bool TrueUpdateIf() => true;

private static bool FalseUpdateIf() => false;
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved


/// <summary>
/// Given a Belief instance,
/// When it is assigned to a variable of its resource type,
/// Then it is implicitly converted to its resource type.
/// </summary>
[Fact]
public void Belief_AssignedToResourceType_IsImplicitlyConvertedToResourceType()
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
string reference = "def";
Belief<string, string> belief = new(reference, ID);

// Act
string resource = belief;

// Assert
Assert.Equal("def", resource);
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Given a Belief instance with an updateIf condition that is satisfied,
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// When UpdateBelief is called,
/// Then the resource is updated.
/// </summary>
[Fact]
public void UpdateBelief_UpdateIfConditionIsSatisfied_UpdatesResource()
{
// Arrange
List<int> list = [];
Belief<List<int>, int> belief = new(list, GetCount, TrueUpdateIf);

// Act
list.Add(69);
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
belief.UpdateBelief();

// Assert
Assert.Equal(1, belief);
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Given a Belief instance with an updateIf condition that is not satisfied,
/// When UpdateBelief is called,
/// Then the resource is not updated.
/// </summary>
[Fact]
public void UpdateBelief_UpdateIfConditionIsNotSatisfied_DoesNotUpdateResource()
{
// Arrange
List<int> list = [];
Belief<List<int>, int> belief = new(list, GetCount, FalseUpdateIf);

// Act
list.Add(69);
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
belief.UpdateBelief();

// Assert
Assert.Equal(0, belief);
}

/// <summary>
/// Given a Belief instance with a reference,
/// When the reference is assigned to and UpdateBelief is called,
/// Then the resource is not updated.
/// </summary>
[Fact]
public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateResource()
{
// Arrange
string def = "def";
Belief<string, string> belief = new(def, ID, TrueUpdateIf);

// Act
def = "abc";
belief.UpdateBelief();

// Assert
Assert.NotEqual(def, belief);
}
}
43 changes: 43 additions & 0 deletions Aplib.Tests/Core/Belief/BeliefsetTests.cs
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Aplib.Core.Belief;

namespace Aplib.Tests.Core.Belief;
public class BeliefsetTests
{
/// <summary>
/// Given a Beliefset instance with multiple beliefs,
/// When UpdateBeliefs is called,
/// Then all beliefs are updated.
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
[Fact]
public void UpdateBeliefs_Called_UpdatesAllBeliefs()
{
// Arrange
TestBeliefset beliefset = new();

// Act
beliefset.UpdateBeliefs();

// Assert
Assert.True(beliefset.Belief1.Updated);
Assert.True(beliefset.Belief2.Updated);
}

private class TestBeliefset : Beliefset
{
public SimpleBelief Belief1 = new();
public SimpleBelief Belief2 = new();
}

/// <summary>
/// A simple belief that can be used to test whether <see cref="UpdateBelief"/> has been called.
/// </summary>
private class SimpleBelief : IBelief
{
public bool Updated { get; private set; } = false;

public void UpdateBelief()
{
Updated = true;
}
}
}
Loading