-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemand-satisfaction.C
More file actions
212 lines (161 loc) · 4.82 KB
/
Copy pathdemand-satisfaction.C
File metadata and controls
212 lines (161 loc) · 4.82 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
90
91
92
93
94
95
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# include <demand-satisfaction.H>
Net::Arc * DemandSatisfaction::search_net_arc(ArcsIndex & net_arcs, Uid id)
{
Net::Arc test;
test.get_info().arco_id = id;
Net::Arc ** result = net_arcs.search(&test);
if (result == nullptr)
return nullptr;
return *result;
}
double DemandSatisfaction::infer_increase(const Tuple & t)
{
double q = get<2>(t);
Planta * plant = map->tabla_plantas(get<1>(t));
double cap = plant->cap;
// Si está en cap 0, no tiene sentido calcular
if (cap == 0.0)
return 0.0;
// Calculo el 100% posible de producción
double possible_prod = 100.0 * q / cap;
// Retorno lo que podría aumentar
return possible_prod - q;
}
DemandSatisfaction::Result
DemandSatisfaction::simple_aproach(MetaProducto * product, double quantity,
size_t max_threshold)
{
if (map == nullptr)
throw logic_error("map == nullptr");
Result r;
get<0>(r) = true;
ArcsIndex arcs;
Net & net = map->net;
if (::verbose)
cout << "Indexing arcs...\n";
for (Net::Arc_Iterator it(net); it.has_curr(); it.next())
arcs.insert(it.get_curr());
if (::verbose)
cout << "Done!\n";
if (::verbose)
cout << "Computing demand satisfaction...\n";
DynListQueue<QTuple> queue;
double desired_quantity = quantity;
product->productores.for_each([&](const string & rif) {
// Busco el productor con el rif actual.
Productor * producer = map->tabla_productores(rif);
// Busco en el productor la tupla del producto
auto t = producer->productos[product->id];
// Con la tupla, infiero la cantidad de producción que se puede aumentar
double possible_increase = infer_increase(t);
double increase = 0.0;
if (possible_increase > desired_quantity)
{
increase = desired_quantity;
desired_quantity = 0.0;
}
else
{
increase = possible_increase;
desired_quantity -= increase;
}
if (increase > 0)
queue.put(make_tuple(product, increase));
});
if (desired_quantity > 0) // No se satisface la demanda aguas abajo
{
get<0>(r) = false;
get<1>(r).append(make_pair(product, quantity));
if (::verbose)
cout << "Done!\n";
return r;
}
while (not queue.is_empty())
{
// Proceso de analizar hacia atrás
// Extraigo de la cola
auto qt = queue.get();
double req_quantity = get<1>(qt);
// Veo la dependencia del producto para calcular cuánto se requiere.
get<0>(qt)->comb.for_each([&](auto factor) {
Uid input_id = get<0>(factor);
string cod_aran = get<1>(factor);
double reqq = get<2>(factor);
Uid arc_id = get<3>(factor);
if (reqq == 0)
return;
MetaInsumo * input = map->tabla_insumos(input_id);
double quan = reqq * req_quantity;
assert(input != nullptr);
Net::Arc * arc_in_net = search_net_arc(arcs, arc_id);
// Si no existe arco, agrego déficit en los insumos y salgo
if (arc_in_net == nullptr) // Creo nodo insumo y no encolo
{
get<0>(r) = false;
get<2>(r).append(make_pair(input, quan));
return;
}
// Existe arco, obtengo el productor que provee el insumo
Productor * producer = map->net.get_src_node(arc_in_net)->get_info();
assert(producer != nullptr);
/* Extraigo los productos con el código arancelario igual al insumo
con los nombres que más se parezcan según el umbral */
DynList<Productor::Prod> filtered_list =
producer->productos.filter([&](auto item) {
// Si no es el cod aranc, entonces no va.
if (get<0>(item.second) != cod_aran)
return false;
// Busco el producto con id dado
auto pr = map->tabla_productos(item.first);
assert(pr != nullptr);
size_t d = levenshtein(pr->nombre, input->nombre);
return d <= max_threshold;
});
// Si no hay productos, inserto en insumos y salgo.
if (filtered_list.is_empty())
{
get<0>(r) = false;
get<2>(r).append(make_pair(input, quan));
return;
}
// Busco el de distancia menor
MetaProducto * prod = nullptr;
size_t min_dist = max_threshold + 1;
for (auto item : filtered_list)
{
auto pr = map->tabla_productos(item.first);
size_t d = levenshtein(pr->nombre, input->nombre);
if (d < min_dist)
{
min_dist = d;
prod = pr;
}
}
assert(prod != nullptr);
// Busco en el productor la tupla correspondiente a prod
auto t = producer->productos[prod->id];
double possible_increase = infer_increase(t);
double increase = 0.0;
if (possible_increase > desired_quantity)
{
increase = quan;
quan = 0.0;
}
else
{
increase = possible_increase;
quan -= increase;
}
if (quan == 0)
queue.put(make_tuple(prod, increase));
else
{
get<0>(r) = false;
get<1>(r).append(make_pair(prod, quan));
}
});
}
if (::verbose)
cout << "Done!\n";
return r;
}