-
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.
introduce GridPathFinder & make it the locus of accessible cell querying
- Loading branch information
1 parent
1876528
commit 964a156
Showing
15 changed files
with
336 additions
and
68 deletions.
There are no files selected for viewing
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
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
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
47 changes: 47 additions & 0 deletions
47
geography/src/main/java/uk/ac/ox/poseidon/geography/paths/AbstractGridPathFinder.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,47 @@ | ||
/* | ||
* POSEIDON: an agent-based model of fisheries | ||
* Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com | ||
* | ||
* 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.geography.paths; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import sim.util.Int2D; | ||
import uk.ac.ox.poseidon.geography.bathymetry.BathymetricGrid; | ||
import uk.ac.ox.poseidon.geography.grids.GridExtent; | ||
import uk.ac.ox.poseidon.geography.ports.PortGrid; | ||
|
||
@RequiredArgsConstructor | ||
abstract class AbstractGridPathFinder implements GridPathFinder { | ||
private final BathymetricGrid bathymetricGrid; | ||
private final PortGrid portGrid; | ||
|
||
@Override | ||
public boolean isNavigable(final Int2D cell) { | ||
return isWater(cell) || portGrid.anyPortsAt(cell); | ||
} | ||
|
||
@Override | ||
public boolean isWater(final Int2D cell) { | ||
return bathymetricGrid.isWater(cell); | ||
} | ||
|
||
@Override | ||
public GridExtent getGridExtent() { | ||
return bathymetricGrid.getGridExtent(); | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
geography/src/main/java/uk/ac/ox/poseidon/geography/paths/CachingGridPathFinder.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,91 @@ | ||
/* | ||
* POSEIDON: an agent-based model of fisheries | ||
* Copyright (c) 2024 CoHESyS Lab cohesys.lab@gmail.com | ||
* | ||
* 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.geography.paths; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Interner; | ||
import com.google.common.collect.Interners; | ||
import sim.util.Int2D; | ||
import uk.ac.ox.poseidon.geography.grids.GridExtent; | ||
|
||
import java.util.Map.Entry; | ||
|
||
import static java.util.Map.entry; | ||
|
||
public class CachingGridPathFinder extends CachingPathFinder<Int2D> implements GridPathFinder { | ||
|
||
private final GridPathFinder pathFinder; | ||
private final Interner<ImmutableList<Int2D>> cellListInterner = Interners.newStrongInterner(); | ||
private final LoadingCache<Int2D, ImmutableList<Int2D>> accessibleCells = | ||
CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleCells)); | ||
|
||
CachingGridPathFinder( | ||
final GridPathFinder pathFinder, | ||
final PathCache<Int2D> cache | ||
) { | ||
super(pathFinder, cache); | ||
this.pathFinder = pathFinder; | ||
} | ||
|
||
private ImmutableList<Int2D> computeAccessibleCells(final Int2D startingCell) { | ||
return pathFinder.getAccessibleWaterCells(startingCell); | ||
} | ||
|
||
@Override | ||
public ImmutableList<Int2D> getAccessibleWaterCells(final Int2D startingCell) { | ||
return cellListInterner.intern(accessibleCells.getUnchecked(startingCell)); | ||
} | ||
|
||
private ImmutableList<Int2D> computeAccessibleNeighbours( | ||
final Entry<Int2D, Integer> entry | ||
) { | ||
return getAccessibleWaterNeighbours(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
@Override | ||
public ImmutableList<Int2D> getAccessibleWaterNeighbours( | ||
final Int2D startingCell, | ||
final int neighbourhoodSize | ||
) { | ||
return accessibleNeighbours.getUnchecked(entry(startingCell, neighbourhoodSize)); | ||
} | ||
|
||
@Override | ||
public GridExtent getGridExtent() { | ||
return pathFinder.getGridExtent(); | ||
} | ||
|
||
@Override | ||
public boolean isWater(final Int2D cell) { | ||
return pathFinder.isWater(cell); | ||
} | ||
|
||
@Override | ||
public boolean isNavigable(final Int2D cell) { | ||
return pathFinder.isNavigable(cell); | ||
} | ||
|
||
private final LoadingCache<Entry<Int2D, Integer>, ImmutableList<Int2D>> accessibleNeighbours = | ||
CacheBuilder.newBuilder().build(CacheLoader.from(this::computeAccessibleNeighbours)); | ||
|
||
} |
Oops, something went wrong.