lp_generator.cpp 4.08 KB
Newer Older
Jonas Seidel's avatar
Jonas Seidel committed
1
2
#include "lp_generator.h"

jonasseidel's avatar
jonasseidel committed
3
std::unordered_map<Variable*, Coefficient> lp_generator::lhs_from_data(
Jonas Seidel's avatar
Jonas Seidel committed
4
5
6
7
8
9
10
11
12
13
14
15
  lhs_constraint_data& data,

  const std::map<
    std::pair<Edge*, std::string>,
    std::pair<Variable*, size_t>
  >& edge_variable_lookup,

  const std::map<
    std::pair<Node*, std::string>,
    std::pair<Variable*, size_t>
  >& node_variable_lookup
){
jonasseidel's avatar
jonasseidel committed
16
  std::unordered_map<Variable*, Coefficient> lhs;
Jonas Seidel's avatar
Jonas Seidel committed
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
90
91
92
93
94

  for(std::pair<std::pair<Node*, std::string>, double> w : data.first){
    auto search_result = node_variable_lookup.find(w.first);
    if(search_result == node_variable_lookup.end()) throw std::range_error("lp_generator::lhs_from_data use of undeclared variable in constraint generation! (node)");
    lhs.insert({search_result->second.first, {w.second}});
  }
  for(std::pair<std::pair<Edge*, std::string>, double> w : data.second){
    auto search_result = edge_variable_lookup.find(w.first);
    if(search_result == edge_variable_lookup.end()) throw std::range_error("lp_generator::lhs_from_data use of undeclared variable in constraint generation! (edge)");
    lhs.insert({search_result->second.first, {w.second}});
  }

  return lhs;
}


std::pair<
  std::map<
    std::pair<Node*, std::string>,
    std::pair<Variable*, size_t>
  >,
  std::map<
    std::pair<Edge*, std::string>,
    std::pair<Variable*, size_t>
  >
>
  lp_generator::grow_from_graph
(
  Linear_Program& lp,

  Graph& g,

  edge_constraint_data_generator edge_generator,

  node_constraint_data_generator node_generator,

  const std::vector<
    std::pair<
      std::string,
      std::tuple<
        integrality,
        std::pair<bool, double>,
        std::pair<bool, double>
      >
    >
  > edge_var_data,

  const std::vector<
    std::pair<
      std::string,
      std::tuple<
        integrality,
        std::pair<bool, double>,
        std::pair<bool, double>
      >
    >
  > node_var_data,

  std::string name_appendix
){
  Polyeder& p = lp.polyeder();
  std::map<std::pair<Node*, std::string>, std::pair<Variable*, size_t>> node_lookup;
  std::map<std::pair<Edge*, std::string>, std::pair<Variable*, size_t>> edge_lookup;

  // put inside conditional_bfs_all_components call as parameters;
  std::function<void(Node* from, Edge* via, bool used_in_traversal)> edge_function = [&p, &edge_lookup, &edge_var_data, name_appendix](Node* from, Edge* via, bool used_in_traversal) -> void {
    for(std::pair<std::string, std::tuple<integrality, std::pair<bool, double>, std::pair<bool, double>>> edge_prop : edge_var_data){
      std::stringstream name;
      name << "edge_" << via->description() << "_" << edge_prop.first << "_" << name_appendix;
      edge_lookup.insert({{via, edge_prop.first}, p.add_variable(Variable(name.str(), std::get<0>(edge_prop.second), std::get<1>(edge_prop.second), std::get<2>(edge_prop.second)))});
    }
  };
  std::function<bool(Edge*, Node*)> node_function = [&p, &node_lookup, &node_var_data, name_appendix](Edge* via, Node* n) -> bool {
    for(std::pair<std::string, std::tuple<integrality, std::pair<bool, double>, std::pair<bool, double>>> node_prop : node_var_data){
      std::stringstream name;
      name << "node_" << n->description() << "_" << node_prop.first << "_" << name_appendix;
      node_lookup.insert({{n,node_prop.first}, p.add_variable(Variable(name.str(), std::get<0>(node_prop.second), std::get<1>(node_prop.second), std::get<2>(node_prop.second)))});
    }
95
    return false;
Jonas Seidel's avatar
Jonas Seidel committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  };

  g.conditional_bfs_all_components(
    edge_function,
    node_function
  );

  for(Node* n : g.nodes()){
    for(auto data : node_generator(n)){
      p.add_constraint(
        Constraint(
          std::get<0>(data),
          std::get<1>(data),
          lp_generator::lhs_from_data(std::get<2>(data), edge_lookup, node_lookup),
          std::get<3>(data)
        )
      );
    }
  }

  for(Edge* e : g.edges()){
    for(auto data : edge_generator(e)){
      p.add_constraint(
        Constraint(
          std::get<0>(data),
          std::get<1>(data),
          lp_generator::lhs_from_data(std::get<2>(data), edge_lookup, node_lookup),
          std::get<3>(data)
        )
      );
    }
  }
  return {node_lookup, edge_lookup};
}