-
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.
along with some refactoring of option suppliers
- Loading branch information
1 parent
23e5a58
commit 5882675
Showing
24 changed files
with
361 additions
and
105 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
69 changes: 69 additions & 0 deletions
69
...main/java/uk/ac/ox/poseidon/agents/behaviours/choices/BestOptionsFromFriendsSupplier.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,69 @@ | ||
/* | ||
* 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.choices; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import ec.util.MersenneTwisterFast; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import uk.ac.ox.poseidon.agents.registers.Register; | ||
import uk.ac.ox.poseidon.agents.vessels.Vessel; | ||
|
||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
import static uk.ac.ox.poseidon.core.MasonUtils.upToNOf; | ||
|
||
@RequiredArgsConstructor | ||
class BestOptionsFromFriendsSupplier<O> implements Supplier<OptionValues<O>> { | ||
|
||
private final Vessel vessel; | ||
private final int maxNumberOfFriends; | ||
private final Register<? extends OptionValues<O>> optionValuesRegister; | ||
private final MersenneTwisterFast rng; | ||
|
||
private final @Getter(lazy = true) ImmutableList<Vessel> friends = chooseFriends(); | ||
|
||
private ImmutableList<Vessel> chooseFriends() { | ||
assert optionValuesRegister != null; | ||
assert this.vessel != null; | ||
return upToNOf( | ||
maxNumberOfFriends, | ||
optionValuesRegister | ||
.getVessels() | ||
.filter(vessel -> vessel.getHomePort() == this.vessel.getHomePort()) | ||
.filter(vessel -> vessel != this.vessel) | ||
.toList(), | ||
rng | ||
); | ||
} | ||
|
||
@Override | ||
public OptionValues<O> get() { | ||
return new ImmutableOptionValues<>( | ||
getFriends() | ||
.stream() | ||
.flatMap(vessel -> optionValuesRegister.get(vessel).stream()) | ||
.flatMap(optionValues -> optionValues.getBestEntries().stream()) | ||
.collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, Math::max)) | ||
); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...va/uk/ac/ox/poseidon/agents/behaviours/choices/BestOptionsFromFriendsSupplierFactory.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,56 @@ | ||
/* | ||
* 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.choices; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import uk.ac.ox.poseidon.agents.registers.Register; | ||
import uk.ac.ox.poseidon.agents.vessels.Vessel; | ||
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory; | ||
import uk.ac.ox.poseidon.core.Factory; | ||
import uk.ac.ox.poseidon.core.Simulation; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class BestOptionsFromFriendsSupplierFactory<O> | ||
extends VesselScopeFactory<Supplier<OptionValues<O>>> { | ||
|
||
private int maxNumberOfFriends; | ||
private Factory<? extends Register<? extends OptionValues<O>>> optionValuesRegister; | ||
|
||
@Override | ||
protected Supplier<OptionValues<O>> newInstance( | ||
final Simulation simulation, | ||
final Vessel vessel | ||
) { | ||
return new BestOptionsFromFriendsSupplier<>( | ||
vessel, | ||
maxNumberOfFriends, | ||
optionValuesRegister.get(simulation), | ||
simulation.random | ||
); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
...s/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/HashMapBasedOptionValues.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,53 @@ | ||
/* | ||
* 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.choices; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Getter | ||
public abstract class HashMapBasedOptionValues<O> | ||
extends MapBasedOptionValues<O> | ||
implements MutableOptionValues<O> { | ||
|
||
protected final Map<O, Double> values = new HashMap<>(); | ||
|
||
@Override | ||
public void observe( | ||
final O option, | ||
final double value | ||
) { | ||
final double oldValue = values.getOrDefault(option, 0.0); | ||
values.put(option, newValue(option, oldValue, value)); | ||
invalidateCache(); | ||
} | ||
|
||
protected void invalidateCache() { | ||
cachedBest = null; | ||
} | ||
|
||
protected abstract double newValue( | ||
O option, | ||
double oldValue, | ||
double observedValue | ||
); | ||
} |
36 changes: 36 additions & 0 deletions
36
agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/ImmutableOptionValues.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,36 @@ | ||
/* | ||
* 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.choices; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import lombok.Getter; | ||
|
||
import java.util.Map; | ||
|
||
@Getter | ||
public class ImmutableOptionValues<O> extends MapBasedOptionValues<O> { | ||
|
||
private final ImmutableMap<O, Double> values; | ||
|
||
public ImmutableOptionValues(final Map<O, Double> values) { | ||
this.values = ImmutableMap.copyOf(values); | ||
} | ||
|
||
} |
Oops, something went wrong.