Skip to content

Commit

Permalink
add first draft for tree
Browse files Browse the repository at this point in the history
  • Loading branch information
xsaschako committed Aug 25, 2023
1 parent bb2ab05 commit d9b2c5a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cpp/models/abm/test_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "weighted_decision_tree.h"
#include <vector>

int main(int argc, char const* argv[])
{
auto nodes = std::vector<int>({1, 4, 10});
auto edges = std::vector<std::tuple<int, int, double>>({{1, 4, 0.5}, {4, 10, 0.5}});
auto decision_tree = weighted_decision_tree(nodes, edges);
return 0;
}
54 changes: 54 additions & 0 deletions cpp/models/abm/weighted_decision_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2020-2021 German Aerospace Center (DLR-SC)
*
* Authors: Sascha Korf
*
* Contact: Martin J. Kuehn <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <vector>
#include <map>
#include <cmath>
#include <memory>
#include <algorithm>

struct TreeNode {
int value;
};

struct TreeEdge {
double time_length;
};

class weighted_decision_tree
{
public:
weighted_decision_tree(std::vector<int> nodes, std::vector<std::tuple<int, int, double>> edges)
{
for (auto edge : edges) {
auto parent_node = std::find(nodes.begin(), nodes.end(), std::get<0>(edge));
auto child_node = std::find(nodes.begin(), nodes.end(), std::get<1>(edge));
build_tree(TreeNode{*parent_node}, TreeEdge{std::get<2>(edge)}, TreeNode{*child_node});
}
}

private:
void build_tree(TreeNode node, TreeEdge edge, TreeNode child)
{
m_tree.insert({node, std::make_pair(edge, child)});
}

std::map<TreeNode, std::pair<TreeEdge, TreeNode>> m_tree;
};

0 comments on commit d9b2c5a

Please sign in to comment.