diff --git a/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/NeighbourhoodGridExplorer.java b/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/NeighbourhoodGridExplorer.java new file mode 100644 index 000000000..ed80727c3 --- /dev/null +++ b/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/NeighbourhoodGridExplorer.java @@ -0,0 +1,51 @@ +/* + * POSEIDON: an agent-based model of fisheries + * Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package uk.ac.ox.poseidon.agents.behaviours.destination; + +import ec.util.MersenneTwisterFast; +import lombok.RequiredArgsConstructor; +import sim.util.Int2D; +import uk.ac.ox.poseidon.agents.behaviours.choices.Explorer; +import uk.ac.ox.poseidon.agents.vessels.Vessel; +import uk.ac.ox.poseidon.geography.paths.GridPathFinder; + +import java.util.function.IntSupplier; + +import static uk.ac.ox.poseidon.core.MasonUtils.oneOf; + +@RequiredArgsConstructor +public class NeighbourhoodGridExplorer implements Explorer { + + private final Vessel vessel; + private final GridPathFinder pathFinder; + private final IntSupplier neighbourhoodSizeSupplier; + private final MersenneTwisterFast rng; + + @Override + public Int2D explore(final Int2D currentCell) { + return oneOf( + pathFinder.getAccessibleWaterNeighbours( + currentCell == null ? vessel.getCurrentCell() : currentCell, + neighbourhoodSizeSupplier.getAsInt() + ), + rng + ); + } +} diff --git a/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/NeighbourhoodGridExplorerFactory.java b/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/NeighbourhoodGridExplorerFactory.java new file mode 100644 index 000000000..6b06cdb42 --- /dev/null +++ b/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/NeighbourhoodGridExplorerFactory.java @@ -0,0 +1,55 @@ +/* + * POSEIDON: an agent-based model of fisheries + * Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package uk.ac.ox.poseidon.agents.behaviours.destination; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import uk.ac.ox.poseidon.agents.vessels.Vessel; +import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory; +import uk.ac.ox.poseidon.core.Factory; +import uk.ac.ox.poseidon.core.Simulation; +import uk.ac.ox.poseidon.geography.paths.GridPathFinder; + +import java.util.function.IntSupplier; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class NeighbourhoodGridExplorerFactory extends VesselScopeFactory { + + private Factory pathFinder; + private Factory neighbourhoodSizeSupplier; + + @Override + protected NeighbourhoodGridExplorer newInstance( + final Simulation simulation, + final Vessel vessel + ) { + return new NeighbourhoodGridExplorer( + vessel, + pathFinder.get(simulation), + neighbourhoodSizeSupplier.get(simulation), + simulation.random + ); + } +} diff --git a/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/RandomGridExplorerFactory.java b/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/RandomGridExplorerFactory.java index 34da84e57..75bf2f958 100644 --- a/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/RandomGridExplorerFactory.java +++ b/agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/RandomGridExplorerFactory.java @@ -30,7 +30,6 @@ import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory; import uk.ac.ox.poseidon.core.Factory; import uk.ac.ox.poseidon.core.Simulation; -import uk.ac.ox.poseidon.geography.bathymetry.BathymetricGrid; import uk.ac.ox.poseidon.geography.paths.GridPathFinder; @Getter @@ -39,7 +38,6 @@ @AllArgsConstructor public class RandomGridExplorerFactory extends VesselScopeFactory> { - private Factory bathymetricGrid; private Factory pathFinder; @Override diff --git a/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/FixedIntSupplierFactory.java b/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/FixedIntSupplierFactory.java new file mode 100644 index 000000000..61ffddadb --- /dev/null +++ b/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/FixedIntSupplierFactory.java @@ -0,0 +1,43 @@ +/* + * POSEIDON: an agent-based model of fisheries + * Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package uk.ac.ox.poseidon.core.suppliers; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import uk.ac.ox.poseidon.core.GlobalScopeFactory; +import uk.ac.ox.poseidon.core.Simulation; + +import java.util.function.IntSupplier; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class FixedIntSupplierFactory extends GlobalScopeFactory { + + private int value; + + @Override + protected IntSupplier newInstance(final Simulation simulation) { + return () -> value; + } +} diff --git a/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/PoissonIntSupplierFactory.java b/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/PoissonIntSupplierFactory.java new file mode 100644 index 000000000..f245288ed --- /dev/null +++ b/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/PoissonIntSupplierFactory.java @@ -0,0 +1,45 @@ +/* + * POSEIDON: an agent-based model of fisheries + * Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package uk.ac.ox.poseidon.core.suppliers; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import sim.util.distribution.Poisson; +import uk.ac.ox.poseidon.core.Simulation; +import uk.ac.ox.poseidon.core.SimulationScopeFactory; + +import java.util.function.IntSupplier; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class PoissonIntSupplierFactory extends SimulationScopeFactory { + + private double mean; + + @Override + protected IntSupplier newInstance(final Simulation simulation) { + final Poisson poisson = new Poisson(mean, simulation.random); + return poisson::nextInt; + } +} diff --git a/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/ShiftedIntSupplierFactory.java b/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/ShiftedIntSupplierFactory.java new file mode 100644 index 000000000..1d7e27175 --- /dev/null +++ b/core/src/main/java/uk/ac/ox/poseidon/core/suppliers/ShiftedIntSupplierFactory.java @@ -0,0 +1,46 @@ +/* + * POSEIDON: an agent-based model of fisheries + * Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package uk.ac.ox.poseidon.core.suppliers; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import uk.ac.ox.poseidon.core.Factory; +import uk.ac.ox.poseidon.core.GlobalScopeFactory; +import uk.ac.ox.poseidon.core.Simulation; + +import java.util.function.IntSupplier; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class ShiftedIntSupplierFactory extends GlobalScopeFactory { + + private Factory intSupplier; + private int shift; + + @Override + protected IntSupplier newInstance(final Simulation simulation) { + final IntSupplier intSupplier = this.intSupplier.get(simulation); + return () -> intSupplier.getAsInt() + shift; + } +} diff --git a/examples/src/main/java/uk/ac/ox/poseidon/examples/BasicScenario.java b/examples/src/main/java/uk/ac/ox/poseidon/examples/BasicScenario.java index a776d22a8..f8f61b856 100644 --- a/examples/src/main/java/uk/ac/ox/poseidon/examples/BasicScenario.java +++ b/examples/src/main/java/uk/ac/ox/poseidon/examples/BasicScenario.java @@ -46,6 +46,8 @@ import uk.ac.ox.poseidon.core.Simulation; import uk.ac.ox.poseidon.core.schedule.ScheduledRepeatingFactory; import uk.ac.ox.poseidon.core.schedule.SteppableSequenceFactory; +import uk.ac.ox.poseidon.core.suppliers.PoissonIntSupplierFactory; +import uk.ac.ox.poseidon.core.suppliers.ShiftedIntSupplierFactory; import uk.ac.ox.poseidon.core.time.*; import uk.ac.ox.poseidon.core.utils.PrefixedIdSupplierFactory; import uk.ac.ox.poseidon.geography.bathymetry.BathymetricGrid; @@ -73,7 +75,7 @@ public class BasicScenario extends Scenario { private Factory speciesA = new SpeciesFactory("A"); private Factory speciesB = new SpeciesFactory("B"); private Factory biomassDiffusionRule = - new SmoothBiomassDiffusionRuleFactory(0.005, 0.01); + new SmoothBiomassDiffusionRuleFactory(0.01, 0.01); private Factory biomassGrowthRule = new LogisticGrowthRuleFactory(0.1); @@ -183,9 +185,12 @@ public class BasicScenario extends Scenario { ), new ChooseDestinationBehaviourFactory( new EpsilonGreedyDestinationSupplierFactory( - 0.5, + 0.25, new ExponentialMovingAverageOptionValuesFactory<>(0.1), - new RandomGridExplorerFactory(bathymetricGrid, pathFinder), + new NeighbourhoodGridExplorerFactory( + pathFinder, + new ShiftedIntSupplierFactory(new PoissonIntSupplierFactory(5), 1) + ), new TotalBiomassCaughtPerHourDestinationEvaluatorFactory() ), new TravelAlongPathBehaviourFactory( diff --git a/geography/src/main/java/uk/ac/ox/poseidon/geography/paths/CachingGridPathFinder.java b/geography/src/main/java/uk/ac/ox/poseidon/geography/paths/CachingGridPathFinder.java index e6554b028..36fa92605 100644 --- a/geography/src/main/java/uk/ac/ox/poseidon/geography/paths/CachingGridPathFinder.java +++ b/geography/src/main/java/uk/ac/ox/poseidon/geography/paths/CachingGridPathFinder.java @@ -38,6 +38,8 @@ public class CachingGridPathFinder extends CachingPathFinder implements G private final Interner> cellListInterner = Interners.newStrongInterner(); private final LoadingCache> accessibleCells = CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleCells)); + private final LoadingCache, ImmutableList> accessibleNeighbours = + CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleNeighbours)); CachingGridPathFinder( final GridPathFinder pathFinder, @@ -59,7 +61,7 @@ public ImmutableList getAccessibleWaterCells(final Int2D startingCell) { private ImmutableList computeAccessibleNeighbours( final Entry entry ) { - return getAccessibleWaterNeighbours(entry.getKey(), entry.getValue()); + return pathFinder.getAccessibleWaterNeighbours(entry.getKey(), entry.getValue()); } @Override @@ -85,7 +87,4 @@ public boolean isNavigable(final Int2D cell) { return pathFinder.isNavigable(cell); } - private final LoadingCache, ImmutableList> accessibleNeighbours = - CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleNeighbours)); - }