Skip to content

Commit

Permalink
yep more wip on Behaviour refactoring (getting there, though)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Jan 30, 2025
1 parent 378bea7 commit 753c87f
Show file tree
Hide file tree
Showing 27 changed files with 602 additions and 68 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

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);
}

protected abstract Behaviour nextBehaviour(
final Vessel vessel,
final LocalDateTime dateTime
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private Evaluation(final Vessel vessel) {
FishingAction.class,
0.0,
(caughtSoFar, fishingAction) -> caughtSoFar +
fishingAction.getFishCaught().getTotalBiomass().getValue(),
fishingAction.getFishCaught().getTotalBiomass().asKg(),
Action.class,
0.0,
(hoursSoFar, action) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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 @@ -42,8 +41,8 @@
public class DefaultFishingBehaviourFactory<C extends Content<C>>
extends BehaviourFactory<Fishing<C>> {

private Factory<? extends FishingGear<C>> fishingGear;
private Factory<? extends Hold<C>> hold;
private VesselScopeFactory<? extends FishingGear<C>> fishingGear;
private VesselScopeFactory<? extends Hold<C>> hold;
private VesselScopeFactory<? extends Supplier<Fisheable<C>>> fisheableSupplier;
private BehaviourFactory<?> afterFishingBehaviour;

Expand All @@ -53,8 +52,8 @@ protected Fishing<C> newInstance(
final Vessel vessel
) {
return new Fishing<>(
fishingGear.get(simulation),
hold.get(simulation),
fishingGear.get(simulation, vessel),
hold.get(simulation, vessel),
fisheableSupplier.get(simulation, vessel)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected void complete(
final LocalDateTime dateTime
) {
fishCaught = fishingGear.fish(fisheableSupplier.get());
hold.store(fishCaught);
hold.addContent(fishCaught);
vessel.popBehaviour();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,40 @@

import lombok.RequiredArgsConstructor;
import uk.ac.ox.poseidon.agents.behaviours.Behaviour;
import uk.ac.ox.poseidon.agents.behaviours.SteppableAction;
import uk.ac.ox.poseidon.agents.behaviours.BranchingBehaviour;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.agents.vessels.hold.Hold;
import uk.ac.ox.poseidon.geography.ports.PortGrid;

import java.time.LocalDateTime;
import java.util.function.BooleanSupplier;

import static com.google.common.base.Preconditions.checkState;

@RequiredArgsConstructor
public class AtPort implements Behaviour {
public class Home extends BranchingBehaviour {

// TODO: handle hold emptying
private final PortGrid portGrid;
private final Hold<?> hold;

private final BooleanSupplier isReady;
private final Behaviour dockingBehaviour;
private final BooleanSupplier readinessSupplier;
private final Behaviour travelBehaviour;
private final Behaviour landingBehaviour;
private final Behaviour behaviourIfReady;
private final Behaviour behaviourIfNotReady;

@Override
public SteppableAction nextAction(
protected Behaviour nextBehaviour(
final Vessel vessel,
final LocalDateTime dateTime
) {
checkState(vessel.isAtPort());
final Behaviour nextBehaviour;
if (vessel.getCurrentDestination() != null)
nextBehaviour = dockingBehaviour;
else if (isReady.getAsBoolean())
nextBehaviour = behaviourIfReady;
else
nextBehaviour = behaviourIfNotReady;
vessel.pushBehaviour(nextBehaviour);
return nextBehaviour.nextAction(vessel, dateTime);
if (!vessel.isAtPort()) {
vessel.setCurrentDestination(portGrid.getLocation(vessel.getHomePort()));
return travelBehaviour;
} else if (!hold.isEmpty()) {
return landingBehaviour;
} else if (readinessSupplier.getAsBoolean()) {
return behaviourIfReady;
} else {
return behaviourIfNotReady;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.port;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import uk.ac.ox.poseidon.agents.behaviours.BehaviourFactory;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory;
import uk.ac.ox.poseidon.agents.vessels.hold.Hold;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.Simulation;
import uk.ac.ox.poseidon.geography.ports.PortGrid;

import java.util.function.BooleanSupplier;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class HomeBehaviourFactory extends BehaviourFactory<Home> {

private Factory<? extends PortGrid> portGrid;
private VesselScopeFactory<? extends Hold<?>> hold;
private VesselScopeFactory<? extends BooleanSupplier> readinessSupplier;
private BehaviourFactory<?> travelBehaviour;
private BehaviourFactory<?> landingBehaviour;
private BehaviourFactory<?> behaviourIfReady;
private BehaviourFactory<?> behaviourIfNotReady;

@Override
protected Home newInstance(
final Simulation simulation,
final Vessel vessel
) {
return new Home(
portGrid.get(simulation),
hold.get(simulation, vessel),
readinessSupplier.get(simulation, vessel),
travelBehaviour.get(simulation, vessel),
landingBehaviour.get(simulation, vessel),
behaviourIfReady.get(simulation, vessel),
behaviourIfNotReady.get(simulation, vessel)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
import uk.ac.ox.poseidon.agents.behaviours.Behaviour;
import uk.ac.ox.poseidon.agents.behaviours.SteppableAction;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.agents.vessels.hold.Hold;
import uk.ac.ox.poseidon.biology.Content;

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

@RequiredArgsConstructor
public class Docking implements Behaviour {
public class Landing<C extends Content<C>> implements Behaviour {

private final Hold<C> hold;
private final Supplier<Duration> durationSupplier;

@Override
Expand All @@ -43,7 +46,7 @@ public SteppableAction nextAction(
}

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

private Action(
final Vessel vessel,
Expand All @@ -55,6 +58,7 @@ private Action(

@Override
public void complete(final LocalDateTime dateTime) {
hold.removeContent(); // TODO: do something with content!
getVessel().setCurrentDestination(null);
getVessel().popBehaviour();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import lombok.Setter;
import uk.ac.ox.poseidon.agents.behaviours.BehaviourFactory;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory;
import uk.ac.ox.poseidon.agents.vessels.hold.Hold;
import uk.ac.ox.poseidon.biology.Content;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.Simulation;

Expand All @@ -35,15 +38,19 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class DockingBehaviourFactory extends BehaviourFactory<Docking> {
public class LandingBehaviourFactory<C extends Content<C>> extends BehaviourFactory<Landing<C>> {

private VesselScopeFactory<? extends Hold<C>> hold;
private Factory<? extends Supplier<Duration>> durationSupplier;

@Override
protected Docking newInstance(
protected Landing<C> newInstance(
final Simulation simulation,
final Vessel vessel
) {
return new Docking(durationSupplier.get(simulation));
return new Landing<>(
hold.get(simulation, vessel),
durationSupplier.get(simulation)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.strategy;

import lombok.RequiredArgsConstructor;
import uk.ac.ox.poseidon.agents.behaviours.Behaviour;
import uk.ac.ox.poseidon.agents.behaviours.BranchingBehaviour;
import uk.ac.ox.poseidon.agents.vessels.Vessel;

import java.time.LocalDateTime;

@RequiredArgsConstructor
public class ThereAndBack extends BranchingBehaviour {

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

private boolean done = false;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.strategy;

public class ThereAndBackBehaviourFactory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void receive(final FishingAction fishingAction) {
super.receive(fishingAction);
fishingAction.getFishCaught().getMap().forEach((species, content) -> {
speciesCode.append(species.getCode());
biomassCaught.append(content.asBiomass().getValue());
biomassCaught.append(content.asBiomass().asKg());
});
}

Expand Down
Loading

0 comments on commit 753c87f

Please sign in to comment.