Skip to content

Commit

Permalink
Add Weight
Browse files Browse the repository at this point in the history
  • Loading branch information
davidstone committed Nov 14, 2024
1 parent aa0ce56 commit a894750
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 66 deletions.
1 change: 1 addition & 0 deletions source/tm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ target_sources(tm_common PUBLIC
visible_hp.cpp
visible_state.cpp
weather.cpp
weight.cpp
wish.cpp
write_bytes.cpp
)
Expand Down
3 changes: 2 additions & 1 deletion source/tm/strategy/max_damage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import tm.contact_ability_effect;
import tm.environment;
import tm.generation;
import tm.team;
import tm.weight;

import bounded;
import containers;
Expand Down Expand Up @@ -114,7 +115,7 @@ constexpr auto max_damage(
}
),
[](SelectionAndDamage const element) {
return WeightedSelection(element.selection, 1.0);
return WeightedSelection(element.selection, Weight(1.0));
}
));
}
Expand Down
3 changes: 2 additions & 1 deletion source/tm/strategy/net_hp_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import tm.strategy.weighted_selection;
import tm.environment;
import tm.generation;
import tm.team;
import tm.weight;

import containers;
import tv;
Expand Down Expand Up @@ -130,7 +131,7 @@ constexpr auto max_net_hp(
}
),
[](ScoredSelection const element) {
return WeightedSelection(element.selection, 1.0);
return WeightedSelection(element.selection, Weight(1.0));
}
));
}
Expand Down
29 changes: 15 additions & 14 deletions source/tm/strategy/random_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import tm.environment;
import tm.generation;
import tm.probability;
import tm.team;
import tm.weight;

import bounded;
import containers;
Expand All @@ -37,31 +38,31 @@ constexpr auto random_selection(LegalSelections const selections, Probability co
return WeightedSelection(
selection,
is_switch(selection) ?
double(switch_probability) :
double(Probability(1.0) - switch_probability)
Weight(switch_probability) :
Weight(Probability(1.0) - switch_probability)
);
}
));
}

static_assert(
random_selection(LegalSelections({MoveName::Tackle}), Probability(0.2)) ==
WeightedSelections({{MoveName::Tackle, 0.8}})
WeightedSelections({{MoveName::Tackle, Weight(0.8)}})
);

static_assert(
random_selection(LegalSelections({MoveName::Tackle, MoveName::Thunder}), Probability(0.2)) ==
WeightedSelections({
{MoveName::Tackle, 0.8},
{MoveName::Thunder, 0.8}
{MoveName::Tackle, Weight(0.8)},
{MoveName::Thunder, Weight(0.8)}
})
);

static_assert(
random_selection(LegalSelections({MoveName::Tackle, Switch(0_bi)}), Probability(0.2)) ==
WeightedSelections({
{MoveName::Tackle, 0.8},
{Switch(0_bi), 0.2}
{MoveName::Tackle, Weight(0.8)},
{Switch(0_bi), Weight(0.2)}
})
);

Expand All @@ -71,9 +72,9 @@ static_assert(
Probability(0.2)
) ==
WeightedSelections({
{MoveName::Tackle, 0.8},
{MoveName::Thunder, 0.8},
{Switch(0_bi), 0.2}
{MoveName::Tackle, Weight(0.8)},
{MoveName::Thunder, Weight(0.8)},
{Switch(0_bi), Weight(0.2)}
})
);

Expand All @@ -83,15 +84,15 @@ static_assert(
Probability(0.2)
) ==
WeightedSelections({
{MoveName::Tackle, 0.8},
{Switch(0_bi), 0.2},
{Switch(1_bi), 0.2}
{MoveName::Tackle, Weight(0.8)},
{Switch(0_bi), Weight(0.2)},
{Switch(1_bi), Weight(0.2)}
})
);

static_assert(
random_selection(LegalSelections({Switch(0_bi)}), Probability(0.2)) ==
WeightedSelections({{Switch(0_bi), 0.2}})
WeightedSelections({{Switch(0_bi), Weight(0.2)}})
);

export auto make_random_selection(Probability const switch_probability) -> Strategy {
Expand Down
44 changes: 21 additions & 23 deletions source/tm/strategy/statistical_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import tm.read_bytes;
import tm.team;
import tm.to_index;
import tm.usage_for;
import tm.weight;

import bounded;
import containers;
Expand All @@ -40,23 +41,23 @@ namespace technicalmachine {
namespace {
using namespace bounded::literal;

using MoveData = containers::flat_map<MoveName, double>;
using MoveData = containers::flat_map<MoveName, Weight<double>>;

struct PerMatchup {
constexpr auto operator[](MoveName const move) const -> double {
constexpr auto operator[](MoveName const move) const -> Weight<double> {
auto const ptr = containers::lookup(m_data, move);
return ptr ? *ptr : 0.2;
return ptr ? *ptr : Weight(0.2);
}
constexpr auto switch_out() const -> double {
constexpr auto switch_out() const -> Weight<double> {
return m_switch_out;
}
constexpr auto switch_in() const -> double {
constexpr auto switch_in() const -> Weight<double> {
return m_switch_in;
}

constexpr auto set(
double const switch_out_weight,
double const switch_in_multiplier,
Weight<double> const switch_out_weight,
Weight<double> const switch_in_multiplier,
MoveData move_data
) -> void {
m_data = std::move(move_data);
Expand All @@ -65,14 +66,14 @@ struct PerMatchup {
}
private:
MoveData m_data;
double m_switch_out;
double m_switch_in;
Weight<double> m_switch_out{0.0};
Weight<double> m_switch_in{0.0};
};

using PerOther = UsageFor<Species, PerMatchup>;

constexpr auto with_minimum_weight(double const weight) -> double {
return weight == 0.0 ? 1.0 / 1'000'000.0 : weight;
constexpr auto with_minimum_weight(Weight<double> const weight) -> Weight<double> {
return weight == Weight(0.0) ? Weight(1.0 / 1'000'000.0) : weight;
}

struct SelectionWeights {
Expand All @@ -83,17 +84,14 @@ struct SelectionWeights {
while (stream.peek() != std::char_traits<char>::eof()) {
auto const other = read_bytes<Species>(stream);
auto const user = read_bytes<Species>(stream);
auto const switch_out_weight = read_bytes<double>(stream);
BOUNDED_ASSERT(switch_out_weight >= 0.0);
auto const switch_in_multiplier = read_bytes<double>(stream);
BOUNDED_ASSERT(switch_in_multiplier >= 0.0);
auto const switch_out_weight = Weight(read_bytes<double>(stream));
auto const switch_in_multiplier = Weight(read_bytes<double>(stream));
using MoveCount = bounded::integer<0, bounded::normalize<bounded::number_of<MoveName>>>;
auto const move_count = read_bytes<MoveCount>(stream);
auto moves = MoveData(containers::generate_n(move_count, [&] {
auto const name = read_bytes<MoveName>(stream);
auto const weight = read_bytes<double>(stream);
BOUNDED_ASSERT(weight >= 0.0);
return containers::map_value_type<MoveName, double>(
auto const weight = Weight(read_bytes<double>(stream));
return containers::map_value_type<MoveName, Weight<double>>(
name,
with_minimum_weight(weight)
);
Expand All @@ -108,12 +106,12 @@ struct SelectionWeights {
}
}

constexpr auto move_weight(Species const other, Species const user, MoveName const move) const -> double {
constexpr auto move_weight(Species const other, Species const user, MoveName const move) const -> Weight<double> {
auto const & per_other = m_data[to_index(other)];
auto const & per_matchup = per_other[to_index(user)];
return per_matchup[move];
}
constexpr auto switch_weight(Species const other, Species const user, Species const switch_) const -> double {
constexpr auto switch_weight(Species const other, Species const user, Species const switch_) const -> Weight<double> {
auto const & per_other = m_data[to_index(other)];
auto const & per_matchup = per_other[to_index(user)];
return per_matchup.switch_out() * per_other[to_index(switch_)].switch_in();
Expand All @@ -123,8 +121,8 @@ struct SelectionWeights {
constexpr auto set(
Species const other,
Species const user,
double const switch_out_weight,
double const switch_in_multiplier,
Weight<double> const switch_out_weight,
Weight<double> const switch_in_multiplier,
MoveData move_data
) -> void {
auto & per_other = m_data[to_index(other)];
Expand Down Expand Up @@ -179,7 +177,7 @@ struct Statistical {
);
},
[](Pass) -> WeightedSelection {
return WeightedSelection(pass, 1.0);
return WeightedSelection(pass, Weight(1.0));
}
));
}
Expand Down
6 changes: 5 additions & 1 deletion source/tm/strategy/strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import tm.generation_generic;
import tm.probability;
import tm.team;
import tm.team_is_empty;
import tm.weight;

import bounded;
import containers;
Expand Down Expand Up @@ -56,7 +57,10 @@ constexpr auto to_selection_probabilities(WeightedSelections const weighted) ->
weighted,
&WeightedSelection::weight
));
BOUNDED_ASSERT(cummulative_weight > 0.0);
BOUNDED_ASSERT(containers::none(
weighted,
[](WeightedSelection const element) { return element.weight == Weight(0.0); }
));

return SelectionProbabilities(containers::transform(
weighted,
Expand Down
6 changes: 4 additions & 2 deletions source/tm/strategy/weighted_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export module tm.strategy.weighted_selection;
import tm.move.legal_selections;
import tm.move.selection;

import tm.weight;

import bounded;
import containers;

namespace technicalmachine {

export struct WeightedSelection {
Selection selection;
double weight;
Weight<double> weight;
friend auto operator==(WeightedSelection, WeightedSelection) -> bool = default;
};

Expand All @@ -28,7 +30,7 @@ struct bounded::tombstone_traits<technicalmachine::WeightedSelection> {
static constexpr auto make(auto const index) noexcept -> technicalmachine::WeightedSelection {
return technicalmachine::WeightedSelection(
tombstone_traits<technicalmachine::Selection>::make(index),
0.0
technicalmachine::Weight(0.0)
);
}
static constexpr auto index(technicalmachine::WeightedSelection const & value) noexcept {
Expand Down
9 changes: 5 additions & 4 deletions source/tm/team_predictor/estimate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import tm.team_predictor.usage_stats;

import tm.ability;
import tm.item;
import tm.weight;

import bounded;
import containers;
Expand All @@ -40,7 +41,7 @@ constexpr auto update_per_species(auto & data, auto const & per_species, auto co
// If users do something very surprising, we don't want to lower the
// odds of everything to 0.
auto const multiplier = bounded::max(
base == 0.0F ? 0.0 : static_cast<double>(per_species(element.key) / base),
base == Weight(0.0F) ? 0.0 : static_cast<double>(per_species(element.key) / base),
1.0 / 1'000'000.0
);
element.mapped *= multiplier;
Expand Down Expand Up @@ -208,17 +209,17 @@ export struct Estimate {
update_per_species(
per_species.mapped.moves,
species_estimate->moves,
[=](MoveName const move) { return base_per_species ? base_per_species->moves(move) : 0.0F; }
[=](MoveName const move) { return base_per_species ? base_per_species->moves(move) : Weight(0.0F); }
);
update_per_species(
per_species.mapped.items,
species_estimate->items,
[=](Item const item) { return base_per_species ? base_per_species->items(item) : 0.0F; }
[=](Item const item) { return base_per_species ? base_per_species->items(item) : Weight(0.0F); }
);
update_per_species(
per_species.mapped.abilities,
species_estimate->abilities,
[=](Ability const ability) { return base_per_species ? base_per_species->abilities(ability) : 0.0F; }
[=](Ability const ability) { return base_per_species ? base_per_species->abilities(ability) : Weight(0.0F); }
);
per_species.mapped.usage =
per_species.mapped.usage == 0.0 ?
Expand Down
17 changes: 9 additions & 8 deletions source/tm/team_predictor/usage_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import tm.ability;
import tm.generation;
import tm.item;
import tm.read_bytes;
import tm.weight;

import bounded;
import containers;
Expand Down Expand Up @@ -116,15 +117,15 @@ auto checked_read(FileReader & reader) {
}

template<typename T>
auto read_inner_probabilities(FileReader & reader, double const species_weight) {
auto read_inner_probabilities(FileReader & reader, Weight<double> const species_weight) {
using Data = UsageStatsProbabilities::Data<T>;
using Map = Data::Map;
auto const elements = read_map<T>(reader);
return Data(containers::transform(elements, [&](T const key) {
auto const relative_weight = checked_read<double>(reader);
auto const relative_weight = Weight(checked_read<double>(reader));
return containers::range_value_t<Map>{
key,
static_cast<float>(species_weight * relative_weight)
Weight(float(species_weight * relative_weight))
};
}));
}
Expand All @@ -133,7 +134,7 @@ template<std::same_as<UsageStatsProbabilities::Map>>
auto checked_read(FileReader & reader) {
auto const all_species = read_map<Species>(reader);
return UsageStatsProbabilities::Map(containers::transform(all_species, [&](Species const species) {
auto const weight = checked_read<double>(reader);
auto const weight = Weight(checked_read<double>(reader));
return containers::range_value_t<UsageStatsProbabilities::Map>{
species,
{
Expand All @@ -157,7 +158,7 @@ export struct UsageStats {
auto probabilities = UsageStatsProbabilities::Map();

for (auto const species : read_map<Species>(reader)) {
auto const species_weight = checked_read<double>(reader);
auto const species_weight = Weight(checked_read<double>(reader));
checked_read<SpeedDistribution>(reader);
auto per_species_probabilities = checked_read<UsageStatsProbabilities::Map>(reader);
auto used_moves = UsedMoves();
Expand All @@ -182,19 +183,19 @@ export struct UsageStats {
bounded::construct<UsageStatsProbabilities::Inner>
);
for (auto const & move_name : read_map<MoveName>(reader)) {
auto const move_weight = checked_read<double>(reader);
auto const move_weight = Weight(checked_read<double>(reader));
checked_insert(
for_this_species.moves,
move_name,
bounded::value_to_function(static_cast<float>(move_weight))
bounded::value_to_function(Weight(float(move_weight)))
);
[[maybe_unused]] auto const speed_distribution = checked_read<SpeedDistribution>(reader);
auto teammates = checked_read<UsageStatsProbabilities::Map>(reader);
auto const weight = species_weight * move_weight;
checked_insert(
probabilities_assuming_species.moves,
move_name,
bounded::value_to_function(static_cast<float>(weight))
bounded::value_to_function(Weight(float(weight)))
);
auto moves = read_inner_probabilities<MoveName>(reader, species_weight);
auto items = read_items();
Expand Down
Loading

0 comments on commit a894750

Please sign in to comment.