Skip to content

Commit

Permalink
wip on adding legal checks to destination choices
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Jan 21, 2025
1 parent f45c58a commit ed4ae35
Show file tree
Hide file tree
Showing 18 changed files with 20,577 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,32 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import static com.google.common.base.Preconditions.checkNotNull;
import static uk.ac.ox.poseidon.core.MasonUtils.upToOneOf;
import static uk.ac.ox.poseidon.core.MasonUtils.shuffledStream;

public class RandomPicker<O> implements Picker<O> {

private final ImmutableList<O> options;
private final Predicate<O> optionPredicate;
private final MersenneTwisterFast rng;

@SuppressFBWarnings(value = "EI2")
public RandomPicker(
final List<O> options,
final Predicate<O> optionPredicate,
final MersenneTwisterFast rng
) {
this.options = ImmutableList.copyOf(options);
this.optionPredicate = optionPredicate;
this.rng = checkNotNull(rng);
}

@Override
public Optional<O> pick() {
return upToOneOf(options, rng);
return shuffledStream(options, rng)
.filter(optionPredicate)
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

import sim.util.Int2D;

import java.util.Optional;
import java.util.function.Supplier;

@FunctionalInterface
public interface DestinationSupplier extends Supplier<Int2D> {
public interface DestinationSupplier extends Supplier<Optional<Int2D>> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.geography.ports.PortGrid;

import java.util.Optional;

@RequiredArgsConstructor
class HomePortDestinationSupplier implements DestinationSupplier {

private final Vessel vessel;
private final PortGrid portGrid;

@Override
public Int2D get() {
return portGrid.getLocation(vessel.getHomePort());
public Optional<Int2D> get() {
return Optional.of(portGrid.getLocation(vessel.getHomePort()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.lang.Double.NEGATIVE_INFINITY;
import static uk.ac.ox.poseidon.core.MasonUtils.upToOneOf;
import static uk.ac.ox.poseidon.core.MasonUtils.shuffledStream;

/**
* This picker will look for better options than the current option in a list of candidates to
Expand All @@ -46,6 +47,7 @@
public class ImitatingPicker<O> implements Picker<O> {

private final OptionValues<O> optionValues;
private final Predicate<O> optionPredicate;
private final Supplier<OptionValues<O>> candidatesSupplier;
private final MersenneTwisterFast rng;

Expand All @@ -67,8 +69,11 @@ public Optional<O> pick() {
.map(Entry::getKey)
.toList();

return upToOneOf(candidates, rng)
.or(() -> currentBestEntry.map(Entry::getKey));
return shuffledStream(candidates, rng)
.filter(optionPredicate)
.findFirst()
.or(() -> currentBestEntry.map(Entry::getKey))
.filter(optionPredicate);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory;
import uk.ac.ox.poseidon.core.Simulation;

import java.util.function.Predicate;
import java.util.function.Supplier;

@Getter
Expand All @@ -37,6 +38,7 @@
public class ImitatingPickerFactory<O> extends VesselScopeFactory<ImitatingPicker<O>> {

private VesselScopeFactory<? extends OptionValues<O>> optionValues;
private VesselScopeFactory<? extends Predicate<O>> optionPredicate;
private VesselScopeFactory<? extends Supplier<OptionValues<O>>> optionValuesSupplier;

@Override
Expand All @@ -46,6 +48,7 @@ protected ImitatingPicker<O> newInstance(
) {
return new ImitatingPicker<>(
optionValues.get(simulation, vessel),
optionPredicate.get(simulation, vessel),
optionValuesSupplier.get(simulation, vessel),
simulation.random
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,34 @@
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.geography.paths.GridPathFinder;

import java.util.List;
import java.util.Optional;
import java.util.function.IntSupplier;
import java.util.function.Predicate;

import static uk.ac.ox.poseidon.core.MasonUtils.upToOneOf;
import static uk.ac.ox.poseidon.core.MasonUtils.shuffledStream;

@RequiredArgsConstructor
public class NeighbourhoodCellPicker implements Picker<Int2D> {

private final Vessel vessel;
private final OptionValues<Int2D> optionValues;
private final Predicate<Int2D> optionPredicate;
private final GridPathFinder pathFinder;
private final IntSupplier neighbourhoodSizeSupplier;
private final MersenneTwisterFast rng;

@Override
public Optional<Int2D> pick() {
return upToOneOf(
final List<Int2D> candidates =
pathFinder.getAccessibleWaterNeighbours(
optionValues
.getBestOption(rng)
.orElse(vessel.getCurrentCell()),
neighbourhoodSizeSupplier.getAsInt()
),
rng
);
);
return shuffledStream(candidates, rng)
.filter(optionPredicate)
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import uk.ac.ox.poseidon.geography.paths.GridPathFinder;

import java.util.function.IntSupplier;
import java.util.function.Predicate;

@Getter
@Setter
Expand All @@ -40,6 +41,7 @@
public class NeighbourhoodGridExplorerFactory extends VesselScopeFactory<NeighbourhoodCellPicker> {

private VesselScopeFactory<? extends OptionValues<Int2D>> optionValues;
private VesselScopeFactory<? extends Predicate<Int2D>> cellPredicate;
private Factory<? extends GridPathFinder> pathFinder;
private Factory<? extends IntSupplier> neighbourhoodSizeSupplier;

Expand All @@ -51,6 +53,7 @@ protected NeighbourhoodCellPicker newInstance(
return new NeighbourhoodCellPicker(
vessel,
optionValues.get(simulation, vessel),
cellPredicate.get(simulation, vessel),
pathFinder.get(simulation),
neighbourhoodSizeSupplier.get(simulation),
simulation.random
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
import uk.ac.ox.poseidon.core.Simulation;
import uk.ac.ox.poseidon.geography.paths.GridPathFinder;

import java.util.function.Predicate;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RandomGridExplorerFactory extends VesselScopeFactory<Picker<Int2D>> {

private Factory<? extends GridPathFinder> pathFinder;
private VesselScopeFactory<? extends Predicate<Int2D>> cellPredicate;

@Override
protected Picker<Int2D> newInstance(
Expand All @@ -47,6 +50,7 @@ protected Picker<Int2D> newInstance(
) {
return new RandomPicker<>(
pathFinder.get(simulation).getAccessibleWaterCells(vessel.getCurrentCell()),
cellPredicate.get(simulation, vessel),
simulation.random
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2025 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.fishing;

import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.Action;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.biology.Bucket;

import java.time.Duration;
import java.time.LocalDateTime;

public class DummyFishingAction extends FishingAction {
public DummyFishingAction(
final LocalDateTime start,
final Vessel vessel,
final Int2D cell
) {
super(start, Duration.ofSeconds(1), vessel, cell);
}

@Override
public Bucket<?> getFishCaught() {
return Bucket.empty();
}

@Override
protected Action complete(final LocalDateTime dateTime) {
throw new RuntimeException("Dummy actions cannot be completed.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2025 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.regulations;

import lombok.RequiredArgsConstructor;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.Action;
import uk.ac.ox.poseidon.agents.behaviours.fishing.DummyFishingAction;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.geography.distance.Distance;
import uk.ac.ox.poseidon.geography.paths.GridPathFinder;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Supplier;

@RequiredArgsConstructor
public class FishingLocationChecker implements Predicate<Int2D> {

private final Regulations regulations;
private final GridPathFinder pathFinder;
private final Distance distance;
private final Supplier<LocalDateTime> currenDateTimeSupplier;
private final Vessel vessel;

public boolean test(final Int2D fishingLocation) {
return regulations.isPermitted(makeAction(fishingLocation));
}

private Action makeAction(final Int2D fishingLocation) {
final List<Int2D> pathToFishingLocation =
pathFinder.getPath(
vessel.getCurrentCell(),
fishingLocation
).orElseThrow(() -> new RuntimeException(
"No path from current vessel location " +
vessel.getCurrentCell() +
" to fishing location " +
fishingLocation
));
final Duration travelDuration =
distance.travelDuration(
pathToFishingLocation,
vessel.getCruisingSpeed()
);

return new DummyFishingAction(
currenDateTimeSupplier.get().plus(travelDuration),
vessel,
fishingLocation
);
}
}
Loading

0 comments on commit ed4ae35

Please sign in to comment.