Skip to content

Commit 520cdfe

Browse files
wip
1 parent 23e5a58 commit 520cdfe

17 files changed

+262
-67
lines changed

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/AverageOptionValues.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424

25-
class AverageOptionValues<T> extends MapBasedOptionValues<T> {
25+
class AverageOptionValues<T> extends HashMapBasedOptionValues<T> {
2626

2727
private final Map<T, Integer> counts = new HashMap<>();
2828

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/AverageOptionValuesFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import uk.ac.ox.poseidon.core.Factory;
2323
import uk.ac.ox.poseidon.core.Simulation;
2424

25-
public class AverageOptionValuesFactory<O> implements Factory<OptionValues<O>> {
25+
public class AverageOptionValuesFactory<O> implements Factory<MutableOptionValues<O>> {
2626
@Override
27-
public OptionValues<O> get(final Simulation simulation) {
27+
public MutableOptionValues<O> get(final Simulation simulation) {
2828
return new AverageOptionValues<>();
2929
}
3030
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* POSEIDON: an agent-based model of fisheries
3+
* Copyright (c) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package uk.ac.ox.poseidon.agents.behaviours.choices;
21+
22+
import lombok.RequiredArgsConstructor;
23+
import uk.ac.ox.poseidon.agents.registers.Register;
24+
import uk.ac.ox.poseidon.agents.vessels.Vessel;
25+
26+
import java.util.Map;
27+
import java.util.function.Supplier;
28+
29+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
30+
31+
@RequiredArgsConstructor
32+
public class BestOptionsSupplier<O> implements Supplier<OptionValues<O>> {
33+
34+
private final Vessel vessel;
35+
private final Register<? extends OptionValues<O>> optionValuesRegister;
36+
37+
@Override
38+
public OptionValues<O> get() {
39+
return new ImmutableOptionValues<>(
40+
optionValuesRegister
41+
.getOtherEntries(vessel)
42+
.map(Map.Entry::getValue)
43+
.flatMap(optionValues -> optionValues.getBestEntries().stream())
44+
.collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))
45+
);
46+
}
47+
}

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/BestOptionsRegisterFactory.java agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/BestOptionsSupplierFactory.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@
2323
import lombok.Getter;
2424
import lombok.NoArgsConstructor;
2525
import lombok.Setter;
26-
import one.util.streamex.EntryStream;
2726
import uk.ac.ox.poseidon.agents.registers.Register;
28-
import uk.ac.ox.poseidon.agents.registers.TransformedRegister;
27+
import uk.ac.ox.poseidon.agents.vessels.Vessel;
28+
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory;
2929
import uk.ac.ox.poseidon.core.Factory;
3030
import uk.ac.ox.poseidon.core.Simulation;
31-
import uk.ac.ox.poseidon.core.SimulationScopeFactory;
3231

33-
import java.util.Map.Entry;
32+
import java.util.function.Supplier;
3433

3534
@Getter
3635
@Setter
3736
@NoArgsConstructor
3837
@AllArgsConstructor
39-
public class BestOptionsRegisterFactory<O>
40-
extends SimulationScopeFactory<Register<Entry<O, Double>>> {
38+
public class BestOptionsSupplierFactory<O>
39+
extends VesselScopeFactory<Supplier<OptionValues<O>>> {
4140

4241
Factory<? extends Register<? extends OptionValues<O>>> optionValuesRegister;
4342

4443
@Override
45-
protected Register<Entry<O, Double>> newInstance(final Simulation simulation) {
46-
return new TransformedRegister<>(
47-
optionValuesRegister.get(simulation),
48-
entryStream -> EntryStream
49-
.of(entryStream)
50-
.flatMapValues(optionValues -> optionValues.getBestEntries().stream())
44+
protected Supplier<OptionValues<O>> newInstance(
45+
final Simulation simulation,
46+
final Vessel vessel
47+
) {
48+
return new BestOptionsSupplier<>(
49+
vessel,
50+
optionValuesRegister.get(simulation)
5151
);
5252
}
5353
}

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/EpsilonGreedyChooser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class EpsilonGreedyChooser<O> implements Supplier<O> {
3131

3232
private final double epsilon;
33-
private final OptionValues<O> optionValues;
33+
private final MutableOptionValues<O> optionValues;
3434
private final Picker<O> explorer;
3535
private final Picker<O> exploiter;
3636
private final Evaluator<O> evaluator;
@@ -40,7 +40,7 @@ public class EpsilonGreedyChooser<O> implements Supplier<O> {
4040

4141
public EpsilonGreedyChooser(
4242
final double epsilon,
43-
final OptionValues<O> optionValues,
43+
final MutableOptionValues<O> optionValues,
4444
final Picker<O> explorer,
4545
final Picker<O> exploiter,
4646
final Evaluator<O> evaluator,

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/ExponentialMovingAverageOptionValues.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import static uk.ac.ox.poseidon.core.utils.Preconditions.checkUnitRange;
2323

24-
class ExponentialMovingAverageOptionValues<T> extends MapBasedOptionValues<T> {
24+
class ExponentialMovingAverageOptionValues<T> extends HashMapBasedOptionValues<T> {
2525

2626
private final double alpha;
2727

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/ExponentialMovingAverageOptionValuesFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
@Setter
3232
@NoArgsConstructor
3333
@AllArgsConstructor
34-
public class ExponentialMovingAverageOptionValuesFactory<O> extends VesselScopeFactory<OptionValues<O>> {
34+
public class ExponentialMovingAverageOptionValuesFactory<O> extends VesselScopeFactory<MutableOptionValues<O>> {
3535

3636
private double alpha;
3737

3838
@Override
39-
protected OptionValues<O> newInstance(
39+
protected MutableOptionValues<O> newInstance(
4040
final Simulation simulation,
4141
final Vessel vessel
4242
) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* POSEIDON: an agent-based model of fisheries
3+
* Copyright (c) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package uk.ac.ox.poseidon.agents.behaviours.choices;
21+
22+
import lombok.Getter;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
@Getter
28+
public abstract class HashMapBasedOptionValues<O>
29+
extends MapBasedOptionValues<O>
30+
implements MutableOptionValues<O> {
31+
32+
protected final Map<O, Double> values = new HashMap<>();
33+
34+
@Override
35+
public void observe(
36+
final O option,
37+
final double value
38+
) {
39+
final double oldValue = values.getOrDefault(option, 0.0);
40+
values.put(option, newValue(option, oldValue, value));
41+
invalidateCache();
42+
}
43+
44+
protected void invalidateCache() {
45+
cachedBest = null;
46+
}
47+
48+
protected abstract double newValue(
49+
O option,
50+
double oldValue,
51+
double observedValue
52+
);
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* POSEIDON: an agent-based model of fisheries
3+
* Copyright (c) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package uk.ac.ox.poseidon.agents.behaviours.choices;
21+
22+
import com.google.common.collect.ImmutableMap;
23+
import lombok.Getter;
24+
25+
import java.util.Map;
26+
import java.util.Map.Entry;
27+
28+
public class ImmutableOptionValues<O> extends MapBasedOptionValues<O> {
29+
30+
@Getter
31+
private final ImmutableMap<O, Double> values;
32+
33+
public ImmutableOptionValues(final Map<O, Double> values) {
34+
this.values = ImmutableMap.copyOf(values);
35+
}
36+
37+
public ImmutableOptionValues(final Iterable<Entry<O, Double>> values) {
38+
this.values = ImmutableMap.copyOf(values);
39+
}
40+
41+
}

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/MapBasedOptionValues.java

+7-25
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import ec.util.MersenneTwisterFast;
2323

24-
import java.util.HashMap;
2524
import java.util.List;
2625
import java.util.Map;
2726
import java.util.Optional;
@@ -32,12 +31,14 @@
3231
import static uk.ac.ox.poseidon.core.MasonUtils.oneOf;
3332

3433
public abstract class MapBasedOptionValues<O> implements OptionValues<O> {
35-
protected final Map<O, Double> values = new HashMap<>();
36-
private List<Map.Entry<O, Double>> cachedBest = null;
34+
35+
protected List<Map.Entry<O, Double>> cachedBest = null;
36+
37+
protected abstract Map<O, Double> getValues();
3738

3839
@Override
3940
public Optional<Double> getValue(final O option) {
40-
return Optional.ofNullable(values.get(option));
41+
return Optional.ofNullable(getValues().get(option));
4142
}
4243

4344
@Override
@@ -58,7 +59,7 @@ public Optional<Double> getBestValue() {
5859
@Override
5960
public List<Map.Entry<O, Double>> getBestEntries() {
6061
if (cachedBest == null) {
61-
cachedBest = values
62+
cachedBest = getValues()
6263
.entrySet()
6364
.stream()
6465
.collect(maxAll(comparingByValue(), toImmutableList()));
@@ -73,24 +74,5 @@ public Optional<Map.Entry<O, Double>> getBestEntry(final MersenneTwisterFast rng
7374
? Optional.empty()
7475
: Optional.of(oneOf(bestEntries, rng));
7576
}
76-
77-
protected void invalidateCache() {
78-
cachedBest = null;
79-
}
80-
81-
@Override
82-
public void observe(
83-
final O option,
84-
final double value
85-
) {
86-
final double oldValue = values.getOrDefault(option, 0.0);
87-
values.put(option, newValue(option, oldValue, value));
88-
invalidateCache();
89-
}
90-
91-
protected abstract double newValue(
92-
O option,
93-
double oldValue,
94-
double observedValue
95-
);
77+
9678
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* POSEIDON: an agent-based model of fisheries
3+
* Copyright (c) 2024 CoHESyS Lab [email protected]
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package uk.ac.ox.poseidon.agents.behaviours.choices;
21+
22+
public interface MutableOptionValues<O> extends OptionValues<O> {
23+
void observe(
24+
O option,
25+
double value
26+
);
27+
}

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/choices/OptionValues.java

-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727

2828
public interface OptionValues<O> {
2929

30-
void observe(
31-
O option,
32-
double value
33-
);
34-
3530
Optional<Double> getValue(O option);
3631

3732
List<O> getBestOptions();

agents/src/main/java/uk/ac/ox/poseidon/agents/behaviours/destination/EpsilonGreedyDestinationSupplierFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sim.util.Int2D;
2727
import uk.ac.ox.poseidon.agents.behaviours.choices.EpsilonGreedyChooser;
2828
import uk.ac.ox.poseidon.agents.behaviours.choices.Evaluator;
29-
import uk.ac.ox.poseidon.agents.behaviours.choices.OptionValues;
29+
import uk.ac.ox.poseidon.agents.behaviours.choices.MutableOptionValues;
3030
import uk.ac.ox.poseidon.agents.behaviours.choices.Picker;
3131
import uk.ac.ox.poseidon.agents.vessels.Vessel;
3232
import uk.ac.ox.poseidon.agents.vessels.VesselScopeFactory;
@@ -39,7 +39,7 @@
3939
public class EpsilonGreedyDestinationSupplierFactory extends VesselScopeFactory<DestinationSupplier> {
4040

4141
private double epsilon;
42-
private VesselScopeFactory<? extends OptionValues<Int2D>> optionValues;
42+
private VesselScopeFactory<? extends MutableOptionValues<Int2D>> optionValues;
4343
private VesselScopeFactory<? extends Picker<Int2D>> explorer;
4444
private VesselScopeFactory<? extends Picker<Int2D>> exploiter;
4545
private VesselScopeFactory<? extends Evaluator<Int2D>> destinationEvaluator;

0 commit comments

Comments
 (0)