Skip to content

Commit

Permalink
make sure FAD carrying capacities are never all zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Dec 14, 2023
1 parent e1ebd81 commit e5c2622
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uk.ac.ox.oxfish.utility.parameters.DoubleParameter;

import java.util.Map;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableMap.toImmutableMap;

Expand All @@ -19,16 +20,24 @@ public PerSpeciesCarryingCapacityInitializer(
this.carryingCapacities = carryingCapacities;
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Override
public PerSpeciesCarryingCapacity apply(final MersenneTwisterFast rng) {
return new PerSpeciesCarryingCapacity(
carryingCapacities
.entrySet()
.stream()
.collect(toImmutableMap(
Map.Entry::getKey,
entry -> entry.getValue().applyAsDouble(rng)
))
);
// generate maps of carrying capacities per species until we find one where there
// is at least one species for which the carrying capacity is greater than zero
// and use that map to construct the `PerSpeciesCarryingCapacity` object
return Stream.<Map<Species, Double>>generate(() ->
carryingCapacities
.entrySet()
.stream()
.collect(toImmutableMap(
Map.Entry::getKey,
entry -> entry.getValue().applyAsDouble(rng)
))
)
.filter(carryingCapacityMap -> carryingCapacityMap.values().stream().anyMatch(cc -> cc > 0))
.map(PerSpeciesCarryingCapacity::new)
.findFirst()
.get();
}
}

0 comments on commit e5c2622

Please sign in to comment.