Skip to content

Commit

Permalink
make sure not fishing actions are taken during closures
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Feb 6, 2025
1 parent a3838c9 commit e2cf11c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

import uk.ac.ox.poseidon.agents.vessels.Vessel;

import javax.annotation.Nullable;
import java.time.LocalDateTime;

public interface Behaviour {
@Nullable
SteppableAction nextAction(
Vessel vessel,
LocalDateTime dateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,20 @@
import uk.ac.ox.poseidon.agents.vessels.Vessel;

import java.time.LocalDateTime;
import java.util.Optional;

public abstract class BranchingBehaviour implements Behaviour {
@Override
public final SteppableAction nextAction(
final Vessel vessel,
final LocalDateTime dateTime
) {
return Optional
.ofNullable(nextBehaviour(vessel, dateTime))
.map(behaviour -> {
vessel.pushBehaviour(behaviour);
return behaviour.nextAction(vessel, dateTime);
})
.or(() -> {
vessel.popBehaviour();
return Optional
.ofNullable(vessel.currentBehaviour())
.map(behaviour -> behaviour.nextAction(vessel, dateTime));
})
.orElse(null);
final var nextBehaviour = nextBehaviour(vessel, dateTime);
if (nextBehaviour != null) {
vessel.pushBehaviour(nextBehaviour);
return nextBehaviour.nextAction(vessel, dateTime);
} else {
return null;
}
}

protected abstract Behaviour nextBehaviour(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@

package uk.ac.ox.poseidon.agents.behaviours.fishing;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import uk.ac.ox.poseidon.agents.behaviours.BehaviourFactory;
import uk.ac.ox.poseidon.agents.regulations.Regulations;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory;
import uk.ac.ox.poseidon.agents.vessels.gears.FishingGear;
import uk.ac.ox.poseidon.agents.vessels.hold.Hold;
import uk.ac.ox.poseidon.biology.Content;
import uk.ac.ox.poseidon.biology.Fisheable;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.Simulation;

import java.util.function.Supplier;
Expand All @@ -41,9 +40,10 @@
public class DefaultFishingBehaviourFactory<C extends Content<C>>
extends BehaviourFactory<Fishing<C>> {

private VesselScopeFactory<? extends FishingGear<C>> fishingGear;
private VesselScopeFactory<? extends Hold<C>> hold;
private VesselScopeFactory<? extends Supplier<Fisheable<C>>> fisheableSupplier;
@NonNull private VesselScopeFactory<? extends FishingGear<C>> fishingGear;
@NonNull private VesselScopeFactory<? extends Hold<C>> hold;
@NonNull private VesselScopeFactory<? extends Supplier<Fisheable<C>>> fisheableSupplier;
@NonNull private Factory<? extends Regulations> regulations;

@Override
protected Fishing<C> newInstance(
Expand All @@ -53,7 +53,8 @@ protected Fishing<C> newInstance(
return new Fishing<>(
fishingGear.get(simulation, vessel),
hold.get(simulation, vessel),
fisheableSupplier.get(simulation, vessel)
fisheableSupplier.get(simulation, vessel),
regulations.get(simulation)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import uk.ac.ox.poseidon.agents.behaviours.Behaviour;
import uk.ac.ox.poseidon.agents.behaviours.SteppableAction;
import uk.ac.ox.poseidon.agents.behaviours.SteppableGridAction;
import uk.ac.ox.poseidon.agents.regulations.Regulations;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.agents.vessels.gears.FishingGear;
import uk.ac.ox.poseidon.agents.vessels.hold.Hold;
Expand All @@ -39,16 +41,18 @@
@AllArgsConstructor
public class Fishing<C extends Content<C>> implements Behaviour {

protected final FishingGear<C> fishingGear;
protected final Hold<C> hold;
private final Supplier<Fisheable<C>> fisheableSupplier;
@NonNull protected final FishingGear<C> fishingGear;
@NonNull protected final Hold<C> hold;
@NonNull private final Supplier<Fisheable<C>> fisheableSupplier;
@NonNull private final Regulations regulations;

@Override
public SteppableAction nextAction(
final Vessel vessel,
final LocalDateTime dateTime
) {
return new Action(vessel, dateTime, fishingGear.getDurationSupplier().get());
final var action = new Action(vessel, dateTime, fishingGear.getDurationSupplier().get());
return regulations.isPermitted(action) ? action : null;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import javax.measure.quantity.Speed;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Optional;

@Getter
@Setter
Expand Down Expand Up @@ -127,13 +126,16 @@ public Behaviour currentBehaviour() {
}

public void scheduleNextAction(final TemporalSchedule schedule) {
Optional
.ofNullable(currentBehaviour())
.map(behaviour -> behaviour.nextAction(this, schedule.getDateTime()))
.ifPresent(action -> {
while (currentBehaviour() != null) {
final var action = currentBehaviour().nextAction(this, schedule.getDateTime());
if (action != null) {
action.init();
schedule.scheduleOnceIn(action.getDuration(), action);
});
break;
} else {
popBehaviour();
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import uk.ac.ox.poseidon.core.GlobalScopeFactory;
import uk.ac.ox.poseidon.core.Simulation;

import java.time.Month;
import java.time.MonthDay;

@Getter
Expand All @@ -37,6 +38,13 @@ public class MonthDayFactory extends GlobalScopeFactory<MonthDay> {
private int month;
private int dayOfMonth;

public MonthDayFactory(
final Month month,
final int dayOfMonth
) {
this(month.getValue(), dayOfMonth);
}

public static MonthDayFactory parse(final CharSequence text) {
final var monthDay = MonthDay.parse(text);
return new MonthDayFactory(monthDay.getMonthValue(), monthDay.getDayOfMonth());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import uk.ac.ox.poseidon.agents.registers.RegisterFactory;
import uk.ac.ox.poseidon.agents.registers.RegisteringFactory;
import uk.ac.ox.poseidon.agents.regulations.FishingLocationLegalityCheckerFactory;
import uk.ac.ox.poseidon.agents.regulations.Regulations;
import uk.ac.ox.poseidon.agents.tables.FishingActionListenerTableFactory;
import uk.ac.ox.poseidon.agents.vessels.RandomHomePortFactory;
import uk.ac.ox.poseidon.agents.vessels.VesselFactory;
Expand All @@ -63,10 +64,7 @@
import uk.ac.ox.poseidon.core.suppliers.PoissonIntSupplierFactory;
import uk.ac.ox.poseidon.core.suppliers.RandomBooleanSupplierFactory;
import uk.ac.ox.poseidon.core.suppliers.ShiftedIntSupplierFactory;
import uk.ac.ox.poseidon.core.time.DateFactory;
import uk.ac.ox.poseidon.core.time.DateTimeAfterFactory;
import uk.ac.ox.poseidon.core.time.DurationFactory;
import uk.ac.ox.poseidon.core.time.ExponentiallyDistributedDurationSupplierFactory;
import uk.ac.ox.poseidon.core.time.*;
import uk.ac.ox.poseidon.core.utils.PrefixedIdSupplierFactory;
import uk.ac.ox.poseidon.geography.bathymetry.BathymetricGrid;
import uk.ac.ox.poseidon.geography.bathymetry.RoughCoastalBathymetricGridFactory;
Expand All @@ -82,11 +80,12 @@
import uk.ac.ox.poseidon.io.ScenarioWriter;
import uk.ac.ox.poseidon.io.tables.CsvTableWriter;
import uk.ac.ox.poseidon.io.tables.CsvTableWriterFactory;
import uk.ac.ox.poseidon.regulations.PermittedIfFactory;
import uk.ac.ox.poseidon.regulations.predicates.AlwaysTrueFactory;
import uk.ac.ox.poseidon.regulations.ForbiddenIfFactory;
import uk.ac.ox.poseidon.regulations.predicates.temporal.BetweenYearlyDatesFactory;

import java.nio.file.Path;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -173,12 +172,6 @@ public class BasicScenario extends Scenario {
portGrid,
distance
);
private VesselScopeFactory<? extends Predicate<Int2D>> fishingLocationChecker =
new FishingLocationLegalityCheckerFactory(
new PermittedIfFactory(new AlwaysTrueFactory()),
pathFinder,
distance
);
private BehaviourFactory<?> travellingBehaviour =
new TravellingAlongPathBehaviourFactory(
pathFinder,
Expand Down Expand Up @@ -245,6 +238,20 @@ public class BasicScenario extends Scenario {
),
0
);
@SuppressWarnings("MagicNumber")
private Factory<? extends Regulations> regulations =
new ForbiddenIfFactory(
new BetweenYearlyDatesFactory(
new MonthDayFactory(Month.MARCH, 1),
new MonthDayFactory(Month.MAY, 31)
)
);
private VesselScopeFactory<? extends Predicate<Int2D>> fishingLocationChecker =
new FishingLocationLegalityCheckerFactory(
regulations,
pathFinder,
distance
);
private VesselScopeFactory<? extends Hold<Biomass>> hold = new StandardBiomassHoldFactory(
MassFactory.of(VESSEL_HOLD_CAPACITY),
MassFactory.of("1 kg"),
Expand Down Expand Up @@ -304,7 +311,8 @@ public class BasicScenario extends Scenario {
new BiomassGridsFactory(
List.of(biomassGridA, biomassGridB)
)
)
),
regulations
),
travellingBehaviour
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import uk.ac.ox.poseidon.agents.registers.RegisterFactory;
import uk.ac.ox.poseidon.agents.registers.RegisteringFactory;
import uk.ac.ox.poseidon.agents.regulations.FishingLocationLegalityCheckerFactory;
import uk.ac.ox.poseidon.agents.regulations.Regulations;
import uk.ac.ox.poseidon.agents.tables.FishingActionListenerTableFactory;
import uk.ac.ox.poseidon.agents.vessels.RandomHomePortFactory;
import uk.ac.ox.poseidon.agents.vessels.VesselFactory;
Expand Down Expand Up @@ -82,7 +83,7 @@
import uk.ac.ox.poseidon.io.tables.CsvTableWriter;
import uk.ac.ox.poseidon.io.tables.CsvTableWriterFactory;
import uk.ac.ox.poseidon.regulations.PermittedIfFactory;
import uk.ac.ox.poseidon.regulations.predicates.AlwaysFalseFactory;
import uk.ac.ox.poseidon.regulations.predicates.temporal.InYearFactory;

import java.nio.file.Path;
import java.time.LocalDate;
Expand Down Expand Up @@ -153,12 +154,6 @@ public class ExternalScenario extends Scenario {
portGrid,
distance
);
private VesselScopeFactory<? extends Predicate<Int2D>> fishingLocationChecker =
new FishingLocationLegalityCheckerFactory(
new PermittedIfFactory(new AlwaysFalseFactory()),
pathFinder,
distance
);
private BehaviourFactory<?> travellingBehaviour =
new TravellingAlongPathBehaviourFactory(
pathFinder,
Expand Down Expand Up @@ -217,6 +212,16 @@ public class ExternalScenario extends Scenario {
optionValuesRegister,
new ExponentialMovingAverageOptionValuesFactory<>(0.5)
);
private Factory<? extends Regulations> regulations =
new PermittedIfFactory(
new InYearFactory(LocalDate.now().getYear())
);
private VesselScopeFactory<? extends Predicate<Int2D>> fishingLocationChecker =
new FishingLocationLegalityCheckerFactory(
regulations,
pathFinder,
distance
);
private Factory<? extends Fleet> fleet =
new DefaultFleetFactory(
500,
Expand Down Expand Up @@ -261,7 +266,8 @@ public class ExternalScenario extends Scenario {
new BiomassGridsFactory(
List.of(biomassGridA)
)
)
),
regulations
),
travellingBehaviour
),
Expand Down

0 comments on commit e2cf11c

Please sign in to comment.