-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5882675
commit 6dfe878
Showing
12 changed files
with
630 additions
and
6 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/GridAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* POSEIDON: an agent-based model of fisheries | ||
* Copyright (c) 2024 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 lombok.Getter; | ||
import sim.util.Int2D; | ||
import uk.ac.ox.poseidon.agents.vessels.Vessel; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
public abstract class GridAction extends Action { | ||
private final Int2D cell; | ||
|
||
public GridAction( | ||
final LocalDateTime start, | ||
final Duration duration, | ||
final Vessel vessel, | ||
final Int2D cell | ||
) { | ||
super(start, duration, vessel); | ||
this.cell = cell; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,4 +115,3 @@ public Bucket<Biomass> extract(final Bucket<Biomass> fishToExtract) { | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
examples/src/main/java/uk/ac/ox/poseidon/examples/external/ExternalBiomassGridProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* POSEIDON: an agent-based model of fisheries | ||
* Copyright (c) 2024 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.examples.external; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import fishing.BiomassGridServiceGrpc; | ||
import fishing.Fishing; | ||
import lombok.RequiredArgsConstructor; | ||
import sim.engine.SimState; | ||
import sim.engine.Steppable; | ||
import sim.util.Int2D; | ||
import uk.ac.ox.poseidon.agents.behaviours.fishing.FishingAction; | ||
import uk.ac.ox.poseidon.biology.biomass.BiomassGrid; | ||
import uk.ac.ox.poseidon.core.events.Listener; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class ExternalBiomassGridProcess implements Steppable, Listener<FishingAction> { | ||
|
||
private final BiomassGrid internalBiomassGrid; | ||
private final BiomassGridServiceGrpc.BiomassGridServiceBlockingStub stub; | ||
private ImmutableList.Builder<FishingAction> accumulator = new ImmutableList.Builder<>(); | ||
|
||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
@Override | ||
public void step(final SimState simState) { | ||
// report catches to external service | ||
stub.applyCatches( | ||
Fishing.Catches | ||
.newBuilder() | ||
.addAllCatches(accumulator.build().stream() | ||
.map(fishingAction -> Fishing.Catches.Catch | ||
.newBuilder() | ||
.setX(fishingAction.getCell().x) | ||
.setY(fishingAction.getCell().y) | ||
.setBiomassCaught(fishingAction.getTotalBiomassCaught()) | ||
.build()) | ||
::iterator | ||
) | ||
.build() | ||
); | ||
// reset fishing action accumulator | ||
accumulator = new ImmutableList.Builder<>(); | ||
// apply growth process and update our internal grid | ||
final List<Fishing.Grid.Column> cols = stub | ||
.applyGrowth(Fishing.Void.getDefaultInstance()) | ||
.getColumnsList(); | ||
for (int x = 0; x < cols.size(); x++) { | ||
final List<Double> col = cols.get(x).getValuesList(); | ||
for (int y = 0; y < col.size(); y++) { | ||
internalBiomassGrid.setBiomass(new Int2D(x, y), col.get(y)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void receive(final FishingAction event) { | ||
accumulator.add(event); | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
.../src/main/java/uk/ac/ox/poseidon/examples/external/ExternalBiomassGridProcessFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* POSEIDON: an agent-based model of fisheries | ||
* Copyright (c) 2024 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.examples.external; | ||
|
||
import fishing.BiomassGridServiceGrpc; | ||
import fishing.Fishing; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import sim.util.Int2D; | ||
import uk.ac.ox.poseidon.agents.behaviours.fishing.FishingAction; | ||
import uk.ac.ox.poseidon.biology.biomass.BiomassGrid; | ||
import uk.ac.ox.poseidon.core.Factory; | ||
import uk.ac.ox.poseidon.core.Simulation; | ||
import uk.ac.ox.poseidon.core.SimulationScopeFactory; | ||
import uk.ac.ox.poseidon.geography.grids.GridExtent; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ExternalBiomassGridProcessFactory | ||
extends SimulationScopeFactory<ExternalBiomassGridProcess> { | ||
|
||
private String serverName; | ||
private int serverPort; | ||
private Factory<? extends BiomassGrid> internalBiomassGrid; | ||
private double carryingCapacity; | ||
private double growthRate; | ||
|
||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
@Override | ||
protected ExternalBiomassGridProcess newInstance(final Simulation simulation) { | ||
|
||
final BiomassGrid internalGrid = internalBiomassGrid.get(simulation); | ||
final GridExtent gridExtent = internalGrid.getGridExtent(); | ||
final var gridBuilder = Fishing.Grid.newBuilder(); | ||
for (int x = 0; x < gridExtent.getGridWidth(); x++) { | ||
final var columnBuilder = Fishing.Grid.Column.newBuilder(); | ||
for (int y = 0; y < gridExtent.getGridHeight(); y++) { | ||
columnBuilder.addValues(internalGrid.getValue(new Int2D(x, y))); | ||
} | ||
gridBuilder.addColumns(columnBuilder); | ||
} | ||
|
||
final ManagedChannel channel = ManagedChannelBuilder | ||
.forAddress(serverName, serverPort) | ||
.usePlaintext() | ||
.build(); | ||
|
||
simulation.addFinalProcess(channel::shutdown); | ||
|
||
final BiomassGridServiceGrpc.BiomassGridServiceBlockingStub stub = | ||
BiomassGridServiceGrpc.newBlockingStub( | ||
channel | ||
); | ||
|
||
stub.init( | ||
Fishing.GridDef | ||
.newBuilder() | ||
.setGrid(gridBuilder) | ||
.setCarryingCapacity(carryingCapacity) | ||
.setGrowthRate(growthRate) | ||
.build() | ||
); | ||
|
||
final ExternalBiomassGridProcess externalBiomassGridProcess = | ||
new ExternalBiomassGridProcess( | ||
internalGrid, | ||
stub | ||
); | ||
simulation.getEventManager().addListener( | ||
FishingAction.class, | ||
externalBiomassGridProcess | ||
); | ||
return externalBiomassGridProcess; | ||
} | ||
} |
Oops, something went wrong.