Skip to content
Snippets Groups Projects
Commit dadc0e9a authored by Franck Dary's avatar Franck Dary
Browse files

Entropy is divided by states

parent 7be95e0f
No related branches found
No related tags found
No related merge requests found
......@@ -188,9 +188,30 @@ Action Action::sumToHypothesis(const std::string & colName, std::size_t lineInde
{
auto apply = [colName, lineIndex, addition](Config & config, Action &)
{
std::string totalStr = config.getLastNotEmptyHypConst(colName, lineIndex).get();
if (totalStr.empty() || totalStr == "_")
totalStr = fmt::format("{}={}|{}", config.getState(), 0.0, 0);
auto byStates = util::split(totalStr, ',');
int index = -1;
for (unsigned int i = 0; i < byStates.size(); i++)
{
auto state = util::split(byStates[i], '=')[0];
if (state == config.getState())
{
index = i;
break;
}
}
if (index == -1)
{
byStates.emplace_back(fmt::format("{}={}|{}", config.getState(), 0.0, 0));
index = byStates.size()-1;
}
// Knuth’s algorithm for online mean
auto curStr = config.getLastNotEmptyHypConst(colName, lineIndex).get();
auto splited = util::split(curStr, '|');
auto splited = util::split(util::split(byStates[index], '=')[1], '|');
float curVal = 0.0;
int curNb = 0;
if (splited.size() == 2)
......@@ -203,14 +224,37 @@ Action Action::sumToHypothesis(const std::string & colName, std::size_t lineInde
float delta = addition - curVal;
curVal += delta / curNb;
config.getLastNotEmptyHyp(colName, lineIndex) = fmt::format("{}|{}", curVal, curNb);
byStates[index] = fmt::format("{}={}|{}", config.getState(), curVal, curNb);
config.getLastNotEmptyHyp(colName, lineIndex) = util::join(",", byStates);
};
auto undo = [colName, lineIndex, addition](Config & config, Action &)
{
std::string totalStr = config.getLastNotEmptyHypConst(colName, lineIndex).get();
if (totalStr.empty() || totalStr == "_")
totalStr = fmt::format("{}={}|{}", config.getState(), 0.0, 0);
auto byStates = util::split(totalStr, ',');
int index = -1;
for (unsigned int i = 0; i < byStates.size(); i++)
{
auto state = util::split(byStates[i], '=')[0];
if (state == config.getState())
{
index = i;
break;
}
}
if (index == -1)
{
byStates.emplace_back(fmt::format("{}={}|{}", config.getState(), 0.0, 0));
index = byStates.size()-1;
}
// Knuth’s algorithm for online mean
auto curStr = config.getLastNotEmptyHypConst(colName, lineIndex).get();
auto splited = util::split(curStr, '|');
auto splited = util::split(util::split(byStates[index], '=')[1], '|');
float curVal = 0.0;
int curNb = 0;
if (splited.size() == 2)
......@@ -222,7 +266,9 @@ Action Action::sumToHypothesis(const std::string & colName, std::size_t lineInde
curNb -= 1;
curVal = (curNb*curVal - addition) / (curNb - 1);
config.getLastNotEmptyHyp(colName, lineIndex) = fmt::format("{}|{}", curVal, curNb);
byStates[index] = fmt::format("{}={}|{}", config.getState(), curVal, curNb);
config.getLastNotEmptyHyp(colName, lineIndex) = util::join(",", byStates);
};
auto appliable = [colName, lineIndex, addition](const Config & config, const Action &)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment