11 using std::runtime_error::runtime_error;
16 for (
const auto& [
key, item] : *o) {
20 if (!known)
throw ParseError(
"Unknown balance field: " +
key);
25 auto it = o.find(
key);
26 if (it == o.end())
throw ParseError(std::string(
"Missing balance field: ") +
key);
30 const auto*
s =
value.getIf<std::string>();
36 if (
const auto*
n =
v.getIf<
double>())
38 else if (
const auto* integer =
v.getIf<std::int64_t>())
45std::uint64_t
integer(
const Value&
v) {
46 if (
const auto*
n =
v.getIf<std::int64_t>()) {
47 if (*
n < 0)
throw ParseError(
"Expected nonnegative integer");
48 return std::uint64_t(*
n);
52 auto parsed = std::from_chars(
s.data(),
s.data() +
s.size(),
n);
53 if (parsed.ec != std::errc{} || parsed.ptr !=
s.data() +
s.size())
throw ParseError(
"Invalid integer");
56std::uint32_t u32(
const Value&
v) {
58 if (
n > std::numeric_limits<std::uint32_t>::max())
throw ParseError(
"Integer overflow");
59 return std::uint32_t(
n);
66Value seedsValue(
const std::vector<std::uint64_t>& seeds) {
68 for (
auto seed : seeds)
a.emplace_back(
std::to_string(
seed));
69 return Value(std::move(
a));
71Value problemValue(
const Problem&
p) {
75 Value::
object({{
"name",
x.name}, {
"initial",
x.initial}, {
"min",
x.minimum}, {
"max",
x.maximum}}));
78 {{
"name",
t.name}, {
"min",
t.minimum}, {
"max",
t.maximum}, {
"scale",
t.scale}, {
"weight",
t.weight}}));
82 {
"targets", std::move(
targets)},
83 {
"trainingSeeds", seedsValue(
p.trainingSeeds)},
84 {
"validationSeeds", seedsValue(
p.validationSeeds)},
85 {
"searchSeed", std::to_string(
p.searchSeed)},
86 {
"candidates", std::int64_t(
p.candidates)},
87 {
"steps", std::int64_t(
p.steps)},
89 {
"changeWeight",
p.changeWeight}});
91Value assessmentValue(
const Problem&
p,
const Assessment&
a) {
93 for (std::size_t i = 0; i <
p.targets.size(); ++i)
94 metrics.emplace(
p.targets[i].name,
95 Value::object({{
"mean", a.metrics[i].mean}, {
"deviation", a.metrics[i].deviation}}));
96 return Value::object({{
"metrics", std::move(metrics)}, {
"loss",
a.loss}, {
"score",
a.score}});
102 const auto& o =
object(
value, {
"schema",
"version",
"parameters",
"targets",
"trainingSeeds",
"validationSeeds",
103 "searchSeed",
"candidates",
"steps",
"dt",
"changeWeight"});
104 if (
text(
required(o,
"schema")) !=
"evengine.balance.problem" || u32(
required(o,
"version")) != 1)
105 throw ParseError(
"Unsupported balance schema/version");
107 for (
const auto& item : array(
required(o,
"parameters"), 64)) {
108 const auto&
x =
object(item, {
"name",
"initial",
"min",
"max"});
112 for (
const auto& item : array(
required(o,
"targets"), 128)) {
113 const auto&
x =
object(item, {
"name",
"min",
"max",
"scale",
"weight"});
116 if (
auto it =
x.find(
"weight"); it !=
x.end())
t.
weight =
number(it->second);
117 p.targets.push_back(std::move(
t));
119 if (
auto it = o.find(
"trainingSeeds"); it != o.end()) {
120 p.trainingSeeds.clear();
121 for (
const auto&
s : array(it->second, 128))
p.trainingSeeds.push_back(integer(
s));
123 if (
auto it = o.find(
"validationSeeds"); it != o.end()) {
124 p.validationSeeds.clear();
125 for (
const auto&
s : array(it->second, 128))
p.validationSeeds.push_back(integer(
s));
127 if (
auto it = o.find(
"searchSeed"); it != o.end())
p.searchSeed = integer(it->second);
128 if (
auto it = o.find(
"candidates"); it != o.end())
p.candidates = u32(it->second);
129 if (
auto it = o.find(
"steps"); it != o.end())
p.steps = u32(it->second);
130 if (
auto it = o.find(
"dt"); it != o.end())
p.dt =
number(it->second);
131 if (
auto it = o.find(
"changeWeight"); it != o.end())
p.changeWeight =
number(it->second);
135 Diagnostic::error(DiagnosticCode::ParseError, e.what(), {}, {},
"agent.balance"));
141 for (std::size_t i = 0; i <
r.problem.parameters.size(); ++i) {
142 const auto&
p =
r.problem.parameters[i];
146 Value::object({{
"before", p.initial}, {
"after", r.parameters[i]}, {
"delta", r.parameters[i] - p.initial}}));
148 return Value::object({{
"schema",
"evengine.balance.report"},
150 {
"problem", problemValue(
r.problem)},
152 {
"changes", std::move(changes)},
153 {
"baseline", assessmentValue(
r.problem,
r.baseline)},
154 {
"tuned", assessmentValue(
r.problem,
r.tuned)},
155 {
"baselineValidation", assessmentValue(
r.problem,
r.baselineValidation)},
156 {
"validation", assessmentValue(
r.problem,
r.validation)},
157 {
"evaluations", std::int64_t(
r.evaluations)},
158 {
"status",
r.verdict == Verdict::WithinTargets ?
"within_targets" :
"outside_targets"},
159 {
"optimizer",
"boundary-seeded-repair-search"}});
std::vector< std::string > fields
std::vector< FieldTarget > targets
std::array< uint32_t, 9 > parameters
Move-only operation result carrying either a value or Status.
The canonical owning dynamic value used by data-facing protocols.
std::map< std::string, Value > Object
std::vector< Value > Array
static Value object(Object value)
Compatibility factory for an object value.
Result< Problem > decodeProblem(const Value &value)
Decode version-1 problem data; reject unknown schema/version/fields and invalid types.
Value encodeBalanceReport(const Report &r)
Project an owning version-1 evidence report, including the full reproduction specification....
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
Owning search specification. Seeds for tuning and holdout must be unique and disjoint.
std::vector< Parameter > parameters
Owning proposal and evidence. No game configuration is modified by the search.
Desired metric interval; scale normalizes units, weight expresses design priority.