Skip to content

Commit

Permalink
yet more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Jan 31, 2025
1 parent 42ac771 commit 4cf5b51
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Getter;
import lombok.ToString;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.vessels.Vessel;

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

@Getter
@ToString(callSuper = true)
public abstract class SteppableGridAction extends SteppableAction implements GridAction {

private final Int2D cell;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package uk.ac.ox.poseidon.agents.behaviours.destination;

import lombok.RequiredArgsConstructor;
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.vessels.Vessel;
Expand Down Expand Up @@ -47,6 +48,7 @@ public SteppableAction nextAction(
);
}

@ToString(callSuper = true)
private class Action extends SteppableAction {

private Action(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,37 @@

import java.time.LocalDateTime;

import static uk.ac.ox.poseidon.agents.behaviours.strategy.ThereAndBack.Status.*;

@RequiredArgsConstructor
public class ThereAndBack extends BranchingBehaviour {

private final Behaviour fishingDestinationBehaviour;
private final Behaviour fishingBehaviour;
private final Behaviour travellingBehaviour;

private boolean done = false;
private Status status = READY;

@Override
protected Behaviour nextBehaviour(
final Vessel vessel,
final LocalDateTime dateTime
) {
if (done) {
if (status == DONE) {
status = READY;
return null;
} else if (vessel.isAtPort()) {
} else if (status == READY) {
status = ACTIVE;
return fishingDestinationBehaviour;
} else if (vessel.isAtCurrentDestination()) {
done = true;
status = DONE;
return fishingBehaviour;
} else {
return travellingBehaviour;
}
}

enum Status {
READY, ACTIVE, DONE
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public Int2D getCurrentCell() {
}

public boolean isAtCurrentDestination() {
return getCurrentCell().equals(getCurrentDestination());
final Int2D currentDestination = getCurrentDestination();
return currentDestination != null && getCurrentCell().equals(currentDestination);
}

public boolean isAtPort() {
Expand Down Expand Up @@ -139,4 +140,9 @@ public void scheduleNextAction(final TemporalSchedule schedule) {
schedule.scheduleOnceIn(action.getDuration(), action);
});
}

@Override
public String toString() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.Simulation;
import uk.ac.ox.poseidon.core.events.ForwardingEventManager;
import uk.ac.ox.poseidon.core.schedule.TemporalSchedule;
import uk.ac.ox.poseidon.core.utils.IdSupplier;
import uk.ac.ox.poseidon.geography.ports.Port;
import uk.ac.ox.poseidon.geography.ports.PortGrid;
Expand Down Expand Up @@ -58,12 +59,15 @@ public final Vessel get(final Simulation simulation) {
vesselField,
new ForwardingEventManager(simulation.getEventManager())
);
final TemporalSchedule temporalSchedule = simulation.getTemporalSchedule();
vessel.setCurrentCell(
portGrid.get(simulation).getLocation(vessel.getHomePort()),
simulation.getTemporalSchedule().getDateTime()
temporalSchedule.getDateTime()
);
vessel.pushBehaviour(initialBehaviour.get(simulation, vessel));
vessel.scheduleNextAction(simulation.getTemporalSchedule());
temporalSchedule.scheduleOnce(__ ->
vessel.scheduleNextAction(temporalSchedule)
);
return vessel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.core.suppliers;

import ec.util.MersenneTwisterFast;
import lombok.RequiredArgsConstructor;

import java.util.function.BooleanSupplier;

@RequiredArgsConstructor
public class RandomBooleanSupplier implements BooleanSupplier {

private final double probability;
private final MersenneTwisterFast rng;

@Override
public boolean getAsBoolean() {
return rng.nextBoolean(probability);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.core.suppliers;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import uk.ac.ox.poseidon.core.Simulation;
import uk.ac.ox.poseidon.core.SimulationScopeFactory;

import java.util.function.BooleanSupplier;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RandomBooleanSupplierFactory extends SimulationScopeFactory<BooleanSupplier> {

private double probability;

@Override
protected BooleanSupplier newInstance(final Simulation simulation) {
return new RandomBooleanSupplier(probability, simulation.random);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
import uk.ac.ox.poseidon.agents.registers.Register;
import uk.ac.ox.poseidon.agents.registers.RegisterFactory;
import uk.ac.ox.poseidon.agents.registers.RegisteringFactory;
import uk.ac.ox.poseidon.agents.regulations.AlwaysPermittedFactory;
import uk.ac.ox.poseidon.agents.regulations.FishingLocationCheckerFactory;
import uk.ac.ox.poseidon.agents.regulations.NeverPermittedFactory;
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 @@ -59,6 +59,7 @@
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.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;
Expand Down Expand Up @@ -87,7 +88,6 @@
import java.util.List;
import java.util.function.Predicate;

import static uk.ac.ox.poseidon.core.suppliers.ConstantBooleanSupplierFactory.ALWAYS_TRUE;
import static uk.ac.ox.poseidon.core.suppliers.ConstantDurationSuppliers.ONE_DAY_DURATION_SUPPLIER;
import static uk.ac.ox.poseidon.core.suppliers.ConstantDurationSuppliers.ONE_HOUR_DURATION_SUPPLIER;
import static uk.ac.ox.poseidon.core.time.PeriodFactory.DAILY;
Expand Down Expand Up @@ -154,7 +154,7 @@ public class BasicScenario extends Scenario {
);
private VesselScopeFactory<? extends Predicate<Int2D>> fishingLocationChecker =
new FishingLocationCheckerFactory(
new NeverPermittedFactory(),
new AlwaysPermittedFactory(),
pathFinder,
distance
);
Expand Down Expand Up @@ -237,7 +237,7 @@ public class BasicScenario extends Scenario {
new HomeBehaviourFactory(
portGrid,
hold,
new VesselScopeFactoryDecorator<>(ALWAYS_TRUE),
new VesselScopeFactoryDecorator<>(new RandomBooleanSupplierFactory(0.1)),
travellingBehaviour,
new LandingBehaviourFactory<>(hold, ONE_HOUR_DURATION_SUPPLIER),
new ThereAndBackBehaviourFactory(
Expand Down
1 change: 0 additions & 1 deletion fishing_actions.csv

This file was deleted.

0 comments on commit 4cf5b51

Please sign in to comment.