17 boost::get(boost::vertex_bundle, this->
main_graph);
19 boost::get(boost::edge_bundle, this->
main_graph);
20 std::pair<T, T> pair_energy;
21 T energy = 0, smoothedObservation, smoothedValue;
25 for (uint32_t i_dim = 0; i_dim < this->
dim;
31 vertex_attribute_map(*i_ver).observation[i_dim];
33 this->parameter.smoothing / this->dim +
34 (1 - this->parameter.smoothing) *
35 vertex_attribute_map(*i_ver).value[i_dim];
36 energy += smoothedObservation *
37 (log(smoothedObservation) - log(smoothedValue)) *
38 vertex_attribute_map(*i_ver).weight;
41 pair_energy.first = energy;
45 i_edg != i_edg_end; ++i_edg) {
46 if (!edge_attribute_map(*i_edg).realEdge) {
49 energy += .5 * edge_attribute_map(*i_edg).isActive *
50 this->parameter.reg_strenth *
51 edge_attribute_map(*i_edg).weight;
53 pair_energy.second = energy;
72 uint32_t nb_comp =
static_cast<uint32_t
>(this->
components.size());
74 boost::get(boost::vertex_bundle, this->
main_graph);
79 std::vector<bool> binary_label(this->
nVertex);
85 for (uint32_t i_step = 1; i_step <= this->
parameter.flow_steps;
93 boost::boykov_kolmogorov_max_flow(
102 for (uint32_t i_com = 0; i_com < nb_comp; i_com++) {
106 for (uint32_t i_ver = 0; i_ver < this->
components[i_com].size();
108 binary_label[vertex_index_map(
110 (vertex_attribute_map(
113 vertex_attribute_map(this->
sink).color);
131 boost::get(boost::vertex_bundle, this->
main_graph);
134 std::vector<std::vector<T>> kernels(2, std::vector<T>(this->
dim));
135 std::vector<std::vector<T>> smooth_kernels(2,
136 std::vector<T>(this->
dim));
138 uint32_t nb_comp =
static_cast<uint32_t
>(this->
components.size());
139 T best_energy, current_energy;
143 for (uint32_t i_com = 0; i_com < nb_comp; i_com++) {
145 static_cast<uint32_t
>(this->
components[i_com].size());
146 std::vector<bool> potential_label(comp_size);
147 std::vector<T> energy_array(comp_size);
148 std::vector<T> constant_part(comp_size);
149 std::vector<std::vector<T>> smooth_obs(comp_size,
150 std::vector<T>(2, 0));
158 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
159 constant_part[i_ver] = 0;
160 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
161 smooth_obs[i_ver][i_dim] = 0;
163 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
164 smooth_obs[i_ver][i_dim] =
167 vertex_attribute_map(
170 constant_part[i_ver] +=
171 smooth_obs[i_ver][i_dim] *
172 log(smooth_obs[i_ver][i_dim]) *
173 vertex_attribute_map(this->
components[i_com][i_ver])
177 for (uint32_t init_kmeans = 0;
178 init_kmeans < this->
parameter.kmeans_resampling;
182 uint32_t first_kernel = std::rand() % comp_size,
184 for (uint32_t i_dim = 0; i_dim < this->
dim;
187 vertex_attribute_map(
190 smooth_kernels[0][i_dim] =
192 (1 - this->
parameter.smoothing) * kernels[0][i_dim];
199 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
200 energy_array[i_ver] = constant_part[i_ver];
201 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
202 energy_array[i_ver] -=
203 smooth_obs[i_ver][i_dim] *
204 log(smooth_kernels[0][i_dim]) *
205 vertex_attribute_map(
209 energy_array[i_ver] = pow(energy_array[i_ver], 2);
210 best_energy += energy_array[i_ver];
215 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
216 binary_label[vertex_index_map(
225 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
226 current_energy -= energy_array[i_ver];
229 second_kernel = i_ver;
233 for (uint32_t i_dim = 0; i_dim < this->
dim;
236 vertex_attribute_map(
239 smooth_kernels[1][i_dim] =
241 (1 - this->
parameter.smoothing) * kernels[1][i_dim];
244 for (uint32_t ite_kmeans = 0;
245 ite_kmeans < this->
parameter.kmeans_ite; ite_kmeans++) {
249 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
251 std::vector<T> distance_kernels(2,
252 constant_part[i_ver]);
253 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
254 distance_kernels[0] -=
255 smooth_obs[i_ver][i_dim] *
256 log(smooth_kernels[0][i_dim]) *
257 vertex_attribute_map(
260 distance_kernels[1] -=
261 smooth_obs[i_ver][i_dim] *
262 log(smooth_kernels[1][i_dim]) *
263 vertex_attribute_map(
267 potential_label[i_ver] =
268 distance_kernels[0] > distance_kernels[1];
272 total_weight[0] = 0.;
273 total_weight[1] = 0.;
274 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
275 kernels[0][i_dim] = 0;
276 kernels[1][i_dim] = 0;
278 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
279 if (vertex_attribute_map(this->
components[i_com][i_ver])
283 if (potential_label[i_ver]) {
285 vertex_attribute_map(
288 for (uint32_t i_dim = 0; i_dim < this->
dim;
291 vertex_attribute_map(
293 .observation[i_dim] *
294 vertex_attribute_map(
300 vertex_attribute_map(
303 for (uint32_t i_dim = 0; i_dim < this->
dim;
306 vertex_attribute_map(
308 .observation[i_dim] *
309 vertex_attribute_map(
315 if ((total_weight[0] == 0) || (total_weight[1] == 0)) {
316 std::cout <<
"kmeans error" <<
std::endl;
318 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
319 kernels[0][i_dim] = kernels[0][i_dim] / total_weight[0];
320 kernels[1][i_dim] = kernels[1][i_dim] / total_weight[1];
321 smooth_kernels[0][i_dim] =
325 smooth_kernels[1][i_dim] =
333 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
334 current_energy += constant_part[i_ver];
335 if (potential_label[i_ver]) {
336 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
338 smooth_obs[i_ver][i_dim] *
339 log(smooth_kernels[0][i_dim]) *
340 vertex_attribute_map(
345 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
347 smooth_obs[i_ver][i_dim] *
348 log(smooth_kernels[1][i_dim]) *
349 vertex_attribute_map(
355 if (current_energy < best_energy) {
356 best_energy = current_energy;
357 for (uint32_t i_ver = 0; i_ver < comp_size; i_ver++) {
358 binary_label[vertex_index_map(
360 potential_label[i_ver];
372 const std::vector<bool>& binary_label) {
375 boost::get(boost::vertex_bundle, this->
main_graph);
378 uint32_t nb_comp =
static_cast<uint32_t
>(this->
components.size());
381 for (uint32_t i_com = 0; i_com < nb_comp; i_com++) {
386 total_weight[0] = 0.;
387 total_weight[1] = 0.;
388 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
392 for (uint32_t i_ver = 0; i_ver < this->
components[i_com].size();
394 if (vertex_attribute_map(this->
components[i_com][i_ver])
398 if (binary_label[vertex_index_map(
401 vertex_attribute_map(this->
components[i_com][i_ver])
403 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
405 vertex_attribute_map(
407 .observation[i_dim] *
408 vertex_attribute_map(
414 vertex_attribute_map(this->
components[i_com][i_ver])
416 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
418 vertex_attribute_map(
420 .observation[i_dim] *
421 vertex_attribute_map(
427 if ((total_weight[0] == 0) || (total_weight[1] == 0)) {
430 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
432 vertex_attribute_map(this->
components[i_com].back())
435 vertex_attribute_map(this->
components[i_com].back())
439 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
457 boost::get(boost::vertex_bundle, this->
main_graph);
459 boost::get(boost::edge_bundle, this->
main_graph);
462 uint32_t nb_comp =
static_cast<uint32_t
>(this->
components.size());
463 T cost_B, cost_notB, smoothedValueB, smoothedValueNotB,
469 for (uint32_t i_com = 0; i_com < nb_comp; i_com++) {
473 for (uint32_t i_ver = 0; i_ver < this->
components[i_com].size();
482 edge_attribute_map(desc_v2source)
488 if (vertex_attribute_map(desc_v).weight == 0) {
489 edge_attribute_map(desc_source2v).capacity = 0;
490 edge_attribute_map(desc_v2sink).capacity = 0;
493 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
494 smoothedObservation =
497 vertex_attribute_map(desc_v)
499 smoothedValueB = this->
parameter.smoothing / this->dim +
503 this->parameter.smoothing / this->dim +
504 (1 - this->parameter.smoothing) *
506 cost_B += smoothedObservation *
507 (log(smoothedObservation) - log(smoothedValueB));
509 smoothedObservation *
510 (log(smoothedObservation) - log(smoothedValueNotB));
512 if (cost_B > cost_notB) {
513 edge_attribute_map(desc_source2v).capacity =
515 edge_attribute_map(desc_v2sink).capacity = 0.;
517 edge_attribute_map(desc_source2v).capacity = 0.;
518 edge_attribute_map(desc_v2sink).capacity =
526 for (boost::tie(i_edg, i_edg_end) = boost::edges(this->
main_graph);
527 i_edg != i_edg_end; ++i_edg) {
528 if (!edge_attribute_map(*i_edg).realEdge) {
531 if (!edge_attribute_map(*i_edg).isActive) {
532 edge_attribute_map(*i_edg).capacity =
533 edge_attribute_map(*i_edg).weight *
534 this->parameter.reg_strenth;
536 edge_attribute_map(*i_edg).capacity = 0;
545 std::pair<std::vector<T>, T>
compute_value(
const uint32_t& i_com)
override {
547 boost::get(boost::vertex_bundle, this->
main_graph);
549 std::vector<T> compValue(this->
dim);
550 std::fill((compValue.begin()), (compValue.end()), 0);
551 for (uint32_t ind_ver = 0; ind_ver < this->
components[i_com].size();
554 vertex_attribute_map(this->
components[i_com][ind_ver])
556 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
558 vertex_attribute_map(this->
components[i_com][ind_ver])
559 .observation[i_dim] *
560 vertex_attribute_map(this->
components[i_com][ind_ver])
563 vertex_attribute_map(this->
components[i_com][ind_ver])
564 .in_component = i_com;
566 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
567 compValue[i_dim] = compValue[i_dim] / total_weight;
569 for (uint32_t ind_ver = 0; ind_ver < this->
components[i_com].size();
571 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
572 vertex_attribute_map(this->
components[i_com][ind_ver])
573 .value[i_dim] = compValue[i_dim];
576 return std::pair<std::vector<T>, T>(compValue, total_weight);
588 std::vector<T> merge_value(this->
dim);
589 T gain = 0, smoothedValue1, smoothedValue2, smoothedValueMerged;
591 for (uint32_t i_dim = 0; i_dim < this->
dim; i_dim++) {
593 (reduced_vertex_attribute_map(comp1).weight *
594 reduced_vertex_attribute_map(comp1).value[i_dim] +
595 reduced_vertex_attribute_map(comp2).weight *
596 reduced_vertex_attribute_map(comp2).value[i_dim]) /
597 (reduced_vertex_attribute_map(comp1).weight +
598 reduced_vertex_attribute_map(comp2).weight);
602 reduced_vertex_attribute_map(comp1).value[i_dim];
606 reduced_vertex_attribute_map(comp2).value[i_dim];
607 smoothedValueMerged =
609 (1 - this->
parameter.smoothing) * merge_value[i_dim];
610 gain -= reduced_vertex_attribute_map(comp1).weight *
612 (log(smoothedValue1) - log(smoothedValueMerged)) +
613 reduced_vertex_attribute_map(comp2).weight *
615 (log(smoothedValue2) - log(smoothedValueMerged));
617 return std::pair<std::vector<T>, T>(merge_value, gain);
std::vector< std::vector< std::vector< T > > > centroids
boost::vec_adj_list_vertex_property_map< Graph< T >, Graph< T > *, VertexAttribute< T >, VertexAttribute< T > &, boost::vertex_bundle_t > VertexAttributeMap
typename boost::graph_traits< Graph< T > >::edge_iterator EdgeIterator
typename boost::graph_traits< Graph< T > >::vertex_iterator VertexIterator
boost::graph_traits< boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS > >::edge_descriptor EdgeDescriptor
typename boost::graph_traits< CP::Graph< T > >::vertex_descriptor VertexDescriptor
typename boost::property_map< Graph< T >, boost::vertex_index_t >::type VertexIndexMap
boost::adj_list_edge_property_map< boost::directed_tag, EdgeAttribute< T >, EdgeAttribute< T > &, uint64_t, CP::EdgeAttribute< T >, boost::edge_bundle_t > EdgeAttributeMap
QTextStream & endl(QTextStream &stream)
Matrix< T > random_sample(Matrix< T > &srcMatrix, size_t size, bool remove=false)
void init_labels(std::vector< bool > &binary_label)
void set_capacities(const VectorOfCentroids< T > ¢ers)
std::pair< std::vector< T >, T > compute_value(const uint32_t &i_com) override
std::pair< T, T > compute_energy() override
std::pair< std::vector< T >, T > compute_merge_gain(const VertexDescriptor< T > &comp1, const VertexDescriptor< T > &comp2) override
void compute_centers(VectorOfCentroids< T > ¢ers, const std::vector< bool > &binary_label)
CPparameter< T > parameter
CP::VertexIterator< T > lastIterator
std::vector< std::vector< VertexDescriptor< T > > > components
VertexDescriptor< T > sink
VertexDescriptor< T > source
size_t activate_edges(bool allows_saturation=true)
std::vector< bool > saturated_components
void saturateComponent(const uint32_t &ind_com)