Skip to content

Commit

Permalink
store coordinate instead of cell in spatial actions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Feb 8, 2025
1 parent 4ba23db commit ef7249a
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

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

import sim.util.Int2D;
import com.vividsolutions.jts.geom.Coordinate;

public interface GridAction extends Action {
Int2D getCell();
public interface SpatialAction extends Action {
Coordinate getCoordinate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,27 @@

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

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import com.vividsolutions.jts.geom.Coordinate;
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 {
public abstract class SteppableSpatialAction extends SteppableAction implements SpatialAction {

private final Int2D cell;
private final Coordinate coordinate;

@SuppressFBWarnings(
value = "EI2",
justification = "Int2D is immutable even if SpotBugs thinks otherwise."
)
public SteppableGridAction(
public SteppableSpatialAction(
final Vessel vessel,
final LocalDateTime start,
final Duration duration,
final Int2D cell
final Coordinate coordinate
) {
super(vessel, start, duration);
this.cell = cell;
this.coordinate = coordinate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Optional<Int2D> pick() {
pathFinder.getAccessibleWaterNeighbours(
optionValues
.getBestOption(rng)
.orElse(vessel.getCurrentCell()),
.orElse(vessel.getCell()),
neighbourhoodSizeSupplier.getAsInt()
);
return shuffledStream(candidates, rng)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected Picker<Int2D> newInstance(
final Vessel vessel
) {
return new RandomPicker<>(
pathFinder.get(simulation).getAccessibleWaterCells(vessel.getCurrentCell()),
pathFinder.get(simulation).getAccessibleWaterCells(vessel.getCell()),
cellPredicate.get(simulation, vessel),
simulation.random
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

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

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import com.vividsolutions.jts.geom.Coordinate;
import lombok.Getter;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.AbstractAction;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.biology.Bucket;
Expand All @@ -30,18 +29,17 @@
import java.time.LocalDateTime;

@Getter
@SuppressFBWarnings(value = "EI2", justification = "Int2D is actually immutable.")
public class DummyFishingAction extends AbstractAction implements FishingAction {

private final Int2D cell;
private final Coordinate coordinate;

public DummyFishingAction(
final LocalDateTime start,
final Vessel vessel,
final Int2D cell
final Coordinate coordinate
) {
super(vessel, start, Duration.ofSeconds(1));
this.cell = cell;
this.coordinate = coordinate;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
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.behaviours.SteppableSpatialAction;
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;
Expand Down Expand Up @@ -57,7 +57,7 @@ public SteppableAction nextAction(

@Getter
@ToString(callSuper = true)
public class Action extends SteppableGridAction implements FishingAction {
public class Action extends SteppableSpatialAction implements FishingAction {

Bucket<C> fishCaught;

Expand All @@ -66,7 +66,7 @@ public Action(
final LocalDateTime start,
final Duration duration
) {
super(vessel, start, duration, vessel.getCurrentCell());
super(vessel, start, duration, vessel.getCoordinate());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

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

import uk.ac.ox.poseidon.agents.behaviours.GridAction;
import uk.ac.ox.poseidon.agents.behaviours.SpatialAction;
import uk.ac.ox.poseidon.biology.Bucket;

public interface FishingAction extends GridAction {
public interface FishingAction extends SpatialAction {
Bucket<?> getFishCaught();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public SteppableAction nextAction(
} else {
currentPath =
pathFinder
.getPath(vessel.getCurrentCell(), currentDestination)
.getPath(vessel.getCell(), currentDestination)
.filter(path -> path.size() > 1)
.map(path -> path.subList(1, path.size()))
.orElseThrow(() -> new IllegalStateException(
MessageFormat.format(
"No path found from {0} to {1} for vessel {2}.",
vessel.getCurrentCell(),
vessel.getCell(),
currentDestination,
vessel
)
Expand All @@ -85,7 +85,7 @@ private Action(
vessel,
start,
distanceCalculator.travelDuration(
vessel.getCurrentCell(),
vessel.getCell(),
currentPath.getFirst(),
vessel.getCruisingSpeed()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SteppableAction nextAction(
return new Action(
dateTime,
distanceCalculator.travelDuration(
vessel.getCurrentCell(),
vessel.getCell(),
vessel.getCurrentDestination(),
vessel.getCruisingSpeed()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public class CurrentCellFisheable<C extends Content<C>> implements Supplier<Fish

@Override
public Fisheable<C> get() {
return fisheableGrid.getFisheableCell(vessel.getCurrentCell());
return fisheableGrid.getFisheableCell(vessel.getCell());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public boolean test(final Int2D fishingLocation) {
private Action makeAction(final Int2D fishingLocation) {
final List<Int2D> pathToFishingLocation =
pathFinder.getPath(
vessel.getCurrentCell(),
vessel.getCell(),
fishingLocation
).orElseThrow(() -> new RuntimeException(
"No path from current vessel location " +
vessel.getCurrentCell() +
vessel.getCell() +
" to fishing location " +
fishingLocation
));
Expand All @@ -66,7 +66,7 @@ private Action makeAction(final Int2D fishingLocation) {
return new DummyFishingAction(
currenDateTimeSupplier.get().plus(travelDuration),
vessel,
fishingLocation
vessel.getVesselField().getGridExtent().toCoordinate(fishingLocation)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.StringColumn;
import uk.ac.ox.poseidon.agents.behaviours.fishing.FishingAction;
import uk.ac.ox.poseidon.geography.grids.GridExtent;

public class FishingActionListenerTable extends GridActionListenerTable<FishingAction> {
public class FishingActionListenerTable extends SpatialActionListenerTable<FishingAction> {

private final StringColumn speciesCode = StringColumn.create("species_code");
private final DoubleColumn biomassCaught = DoubleColumn.create("biomass_caught");

public FishingActionListenerTable(final GridExtent gridExtent) {
super(FishingAction.class, gridExtent);
public FishingActionListenerTable() {
super(FishingAction.class);
get().addColumns(speciesCode, biomassCaught);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,19 @@

package uk.ac.ox.poseidon.agents.tables;

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.Simulation;
import uk.ac.ox.poseidon.geography.grids.GridExtent;
import uk.ac.ox.poseidon.io.tables.SimulationEventListenerFactory;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FishingActionListenerTableFactory
extends SimulationEventListenerFactory<FishingActionListenerTable> {

private Factory<? extends GridExtent> gridExtent;

@Override
protected FishingActionListenerTable newListener(final Simulation simulation) {
return new FishingActionListenerTable(gridExtent.get(simulation));
return new FishingActionListenerTable();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@

import com.vividsolutions.jts.geom.Coordinate;
import tech.tablesaw.api.DoubleColumn;
import uk.ac.ox.poseidon.agents.behaviours.GridAction;
import uk.ac.ox.poseidon.geography.grids.GridExtent;
import uk.ac.ox.poseidon.agents.behaviours.SpatialAction;

public abstract class GridActionListenerTable<A extends GridAction> extends ActionListenerTable<A> {
private final GridExtent gridExtent;
public abstract class SpatialActionListenerTable<A extends SpatialAction> extends ActionListenerTable<A> {
private final DoubleColumn lon = DoubleColumn.create("lon");
private final DoubleColumn lat = DoubleColumn.create("lat");

public GridActionListenerTable(
final Class<A> eventClass,
final GridExtent gridExtent
public SpatialActionListenerTable(
final Class<A> eventClass
) {
super(eventClass);
get().addColumns(lon, lat);
this.gridExtent = gridExtent;
}

@Override
public void receive(final A action) {
super.receive(action);
final Coordinate coordinate = gridExtent.toCoordinate(action.getCell());
final Coordinate coordinate = action.getCoordinate();
lon.append(coordinate.x);
lat.append(coordinate.y);
}
Expand Down
15 changes: 10 additions & 5 deletions agents/src/main/java/uk/ac/ox/poseidon/agents/vessels/Vessel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package uk.ac.ox.poseidon.agents.vessels;

import com.vividsolutions.jts.geom.Coordinate;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -84,17 +85,21 @@ public void setHeadingTowards(
private void setHeadingTowards(
final Double2D destinationPoint
) {
final Double2D location = getCurrentPoint();
final Double2D location = getPoint();
final double dx = destinationPoint.x - location.x;
final double dy = destinationPoint.y - location.y;
setHeading(Math.atan2(dy, dx));
}

public Double2D getCurrentPoint() {
public Double2D getPoint() {
return vesselField.getPoint(this);
}

public Int2D getCurrentCell() {
public Coordinate getCoordinate() {
return vesselField.getGridExtent().toCoordinate(getPoint());
}

public Int2D getCell() {
return vesselField.getCell(this);
}

Expand All @@ -106,11 +111,11 @@ public void setCurrentCell(

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

public boolean isAtPort() {
return portGrid.anyPortsAt(getCurrentCell());
return portGrid.anyPortsAt(getCell());
}

public void popBehaviour() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public class BasicScenario extends Scenario {
private Factory<? extends CsvTableWriter> catchTableWriter =
new FinalProcessFactory<>(
new CsvTableWriterFactory(
new FishingActionListenerTableFactory(gridExtent),
new FishingActionListenerTableFactory(),
PathFactory.from("temp", "fishing_actions.csv"),
true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ public void step(final SimState simState) {
.addAllCatches(accumulator.build().stream()
.map(fishingAction -> Fishing.Catches.Catch
.newBuilder()
.setX(fishingAction.getCell().x)
.setY(fishingAction.getCell().y)
.setX(
internalBiomassGrid
.getGridExtent()
.toCell(fishingAction.getCoordinate()).x
)
.setY(
internalBiomassGrid
.getGridExtent()
.toCell(fishingAction.getCoordinate()).y
)
.setBiomassCaught(
fishingAction
.getFishCaught()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public class ExternalScenario extends Scenario {
private Factory<? extends CsvTableWriter> catchTableWriter =
new FinalProcessFactory<>(
new CsvTableWriterFactory(
new FishingActionListenerTableFactory(gridExtent),
new FishingActionListenerTableFactory(),
PathFactory.from("fishing_actions.csv"),
true
)
Expand Down
Loading

0 comments on commit ef7249a

Please sign in to comment.