-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathexperimenting.cpp
More file actions
89 lines (80 loc) · 2.93 KB
/
experimenting.cpp
File metadata and controls
89 lines (80 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2019 The MMapper Authors
// Author: Ulf Hermann <[email protected]> (Alve)
// Author: Marek Krejza <[email protected]> (Caligor)
#include "experimenting.h"
#include "../global/utils.h"
#include "path.h"
#include "pathparameters.h"
#include <memory>
Experimenting::Experimenting(std::shared_ptr<PathList> pat,
const ExitDirEnum in_dirCode,
PathParameters &in_params)
: m_direction(::exitDir(in_dirCode))
, m_dirCode(in_dirCode)
, m_paths(PathList::alloc())
, m_params(in_params)
, m_shortPaths(std::move(pat))
{}
Experimenting::~Experimenting() = default;
void Experimenting::augmentPath(const std::shared_ptr<Path> &path, const RoomHandle &room)
{
auto &p = deref(path);
const Coordinate c = p.getRoom().getPosition() + m_direction;
const auto working = p.fork(room, c, m_params, m_dirCode);
if (m_best == nullptr) {
m_best = working;
} else if (working->getProb() > m_best->getProb()) {
m_paths->push_back(m_best);
m_second = m_best;
m_best = working;
} else {
if (m_second == nullptr || working->getProb() > m_second->getProb()) {
m_second = working;
}
m_paths->push_back(working);
}
++m_numPaths;
}
std::shared_ptr<PathList> Experimenting::evaluate()
{
for (PathList &sp = deref(m_shortPaths); !sp.empty();) {
std::shared_ptr<Path> ppath = utils::pop_front(sp);
Path &path = deref(ppath);
if (!path.hasChildren()) {
path.deny();
}
}
if (m_best != nullptr) {
if (m_second == nullptr
|| m_best->getProb() > m_second->getProb() * m_params.acceptBestRelative
|| m_best->getProb() > m_second->getProb() + m_params.acceptBestAbsolute) {
for (auto &path : *m_paths) {
path->deny();
}
m_paths->clear();
m_paths->push_front(m_best);
} else {
m_paths->push_back(m_best);
for (std::shared_ptr<Path> working = m_paths->front(); working != m_best;) {
m_paths->pop_front();
// throw away if the probability is very low or not
// distinguishable from best. Don't keep paths with equal
// probability at the front, for we need to find a unique
// best path eventually.
if (m_best->getProb() > working->getProb() * m_params.maxPaths / m_numPaths
|| (m_best->getProb() <= working->getProb()
&& m_best->getRoom() == working->getRoom())) {
working->deny();
} else {
m_paths->push_back(working);
}
working = m_paths->front();
}
}
}
m_second = nullptr;
m_shortPaths = nullptr;
m_best = nullptr;
return m_paths;
}