Skip to content

Commit

Permalink
add NeighboorhoodGridExplorer
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Nov 16, 2024
1 parent 964a156 commit b30acc5
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

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<Int2D> {

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
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

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<NeighbourhoodGridExplorer> {

private Factory<? extends GridPathFinder> pathFinder;
private Factory<? extends IntSupplier> neighbourhoodSizeSupplier;

@Override
protected NeighbourhoodGridExplorer newInstance(
final Simulation simulation,
final Vessel vessel
) {
return new NeighbourhoodGridExplorer(
vessel,
pathFinder.get(simulation),
neighbourhoodSizeSupplier.get(simulation),
simulation.random
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +38,6 @@
@AllArgsConstructor
public class RandomGridExplorerFactory extends VesselScopeFactory<Explorer<Int2D>> {

private Factory<? extends BathymetricGrid> bathymetricGrid;
private Factory<? extends GridPathFinder> pathFinder;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

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<IntSupplier> {

private int value;

@Override
protected IntSupplier newInstance(final Simulation simulation) {
return () -> value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

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<IntSupplier> {

private double mean;

@Override
protected IntSupplier newInstance(final Simulation simulation) {
final Poisson poisson = new Poisson(mean, simulation.random);
return poisson::nextInt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

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<IntSupplier> {

private Factory<? extends IntSupplier> intSupplier;
private int shift;

@Override
protected IntSupplier newInstance(final Simulation simulation) {
final IntSupplier intSupplier = this.intSupplier.get(simulation);
return () -> intSupplier.getAsInt() + shift;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +75,7 @@ public class BasicScenario extends Scenario {
private Factory<? extends Species> speciesA = new SpeciesFactory("A");
private Factory<? extends Species> speciesB = new SpeciesFactory("B");
private Factory<? extends BiomassDiffusionRule> biomassDiffusionRule =
new SmoothBiomassDiffusionRuleFactory(0.005, 0.01);
new SmoothBiomassDiffusionRuleFactory(0.01, 0.01);
private Factory<? extends BiomassGrowthRule> biomassGrowthRule =
new LogisticGrowthRuleFactory(0.1);

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class CachingGridPathFinder extends CachingPathFinder<Int2D> implements G
private final Interner<ImmutableList<Int2D>> cellListInterner = Interners.newStrongInterner();
private final LoadingCache<Int2D, ImmutableList<Int2D>> accessibleCells =
CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleCells));
private final LoadingCache<Entry<Int2D, Integer>, ImmutableList<Int2D>> accessibleNeighbours =
CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleNeighbours));

CachingGridPathFinder(
final GridPathFinder pathFinder,
Expand All @@ -59,7 +61,7 @@ public ImmutableList<Int2D> getAccessibleWaterCells(final Int2D startingCell) {
private ImmutableList<Int2D> computeAccessibleNeighbours(
final Entry<Int2D, Integer> entry
) {
return getAccessibleWaterNeighbours(entry.getKey(), entry.getValue());
return pathFinder.getAccessibleWaterNeighbours(entry.getKey(), entry.getValue());
}

@Override
Expand All @@ -85,7 +87,4 @@ public boolean isNavigable(final Int2D cell) {
return pathFinder.isNavigable(cell);
}

private final LoadingCache<Entry<Int2D, Integer>, ImmutableList<Int2D>> accessibleNeighbours =
CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleNeighbours));

}

0 comments on commit b30acc5

Please sign in to comment.