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

Parametrised database reinitialisation #144

Merged
merged 10 commits into from
Mar 27, 2023
Merged

Parametrised database reinitialisation #144

merged 10 commits into from
Mar 27, 2023

Conversation

james-whiteside
Copy link
Contributor

@james-whiteside james-whiteside commented Mar 10, 2023

What is the goal of this PR?

Running multiple simulations on the same database without reinitialising it allows simulations to be run incrementally, which is very useful for exploring the way the data changes throughout the simulation for both research and bug fixing. This PR makes that possible.

What are the changes implemented in this PR?

The Config class has been modified to include a new recreateDatabase parameter. The Context and Simulation classes have been updated accordingly. In order to ensure that repeated runs of the same simulation are deterministic, the Agent class has been modified so that each spawned agent now receives its own RandomSource. The Simulation class has also recieved a small modification to allow this, by ensuring that the master random source is incremented regardless of whether the database is recreated or not. Finally, the Simulation class has also been modified to produce a slightly clearer output by including the number of times each agent action is run.

Agent.kt Show resolved Hide resolved
Comment on lines +39 to +40
val randomSource2 = randomSource.nextSource()
if (context.recreateDatabase) init(randomSource2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So when recreateDatabase is false, we generate a new random source but just discard it?

While this is probably harmless, it reads awkwardly. We should not do that.

Suggested change
val randomSource2 = randomSource.nextSource()
if (context.recreateDatabase) init(randomSource2)
if (context.recreateDatabase) init(randomSource.nextSource())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed the comment in the PR description about incrementing the master random source whether the DB is recreated or not.

If that's the behaviour we want, then fine - I'd say it has the potential to blindside the reader though! Could we add a comment in the code itself clarifying that yes, this is really what we want to do here (and why)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is the best way to ensure the random selections remain deterministic. Resolved as discussed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following change was tested (lines 39 and 40):

if (context.recreateDatabase) init(randomSource.nextSource())
else randomSource.nextSource()

however this did not produce deterministic results. The following unit test was run:

@Test
fun testSource() {
    val seed: Long = 0
    val randomSourceA = RandomSource(seed)
    val randomSourceB = RandomSource(seed)
    assert(randomSourceA.nextInt(1000000) == randomSourceB.nextInt(1000000))

    val randomSourceA2 = randomSourceA.nextSource()
    repeat(1000000) {
        randomSourceA2.nextBoolean()
    }

    if (false) {}
    else randomSourceB.nextSource()

    assert(randomSourceA.nextInt(1000000) == randomSourceB.nextInt(1000000))
}

which passed. It is unknown why these changes to lines 39 and 40 affect the determinism of the simulation. An issue has been opened here.

@alexjpwalker
Copy link
Member

@james-whiteside

There are good use cases for running multiple simulations on the same database without reinitialising it.

What kind of use cases? Could we add an example to the PR description?

@james-whiteside
Copy link
Contributor Author

@james-whiteside

There are good use cases for running multiple simulations on the same database without reinitialising it.

What kind of use cases? Could we add an example to the PR description?

Have edited the description a bit to be more descriptive.

@james-whiteside james-whiteside merged commit 0937bfa into typedb:master Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants