|
26 | 26 | #include <vector> |
27 | 27 |
|
28 | 28 | #include "mt-kahypar/io/sql_plottools_serializer.h" |
| 29 | +#include "mt-kahypar/partition/multilevel.h" |
29 | 30 | #include "mt-kahypar/partition/evolutionary/edge_frequency.h" |
30 | 31 | #include "mt-kahypar/partition/evolutionary/population.h" |
31 | 32 |
|
32 | 33 |
|
33 | | -namespace mt_kahypar { |
34 | | -namespace combine { |
35 | | -static constexpr bool debug = false; |
36 | 34 |
|
37 | | -template<typename Hypergraph> |
38 | | -Individual partitions(Hypergraph& hg, |
39 | | - const Parents& parents, |
40 | | - Context& context) { |
41 | | - const HighResClockTimepoint start = std::chrono::high_resolution_clock::now(); |
42 | | - DBG << V(context.evolutionary.action.decision()); |
43 | | - DBG << "Parent 1: initial" << V(parents.first.fitness()); |
44 | | - DBG << "Parent 2: initial" << V(parents.second.fitness()); |
45 | | - context.evolutionary.parent1 = &parents.first.partition(); |
46 | | - context.evolutionary.parent2 = &parents.second.partition(); |
47 | | -#ifndef NDEBUG |
48 | | - ASSERT(parents.first.fitness() == ([](Hypergraph& hg, const Parents& parents) -> int { |
49 | | - hg.setPartition(parents.first.partition()); |
50 | | - HyperedgeWeight metric = metrics::km1(hg); |
51 | | - hg.reset(); |
52 | | - return metric; |
53 | | - })(hg, parents)); |
54 | | - DBG << "initial" << V(metrics::km1(hg)) << V(metrics::imbalance(hg, context)); |
55 | | - |
56 | | - ASSERT(parents.second.fitness() == ([](Hypergraph& hg, const Parents& parents) -> int { |
57 | | - hg.setPartition(parents.second.partition()); |
58 | | - HyperedgeWeight metric = metrics::km1(hg); |
59 | | - hg.reset(); |
60 | | - return metric; |
61 | | - })(hg, parents)); |
62 | | - |
63 | | -#endif |
| 35 | +namespace mt_kahypar::combine { |
| 36 | +static constexpr bool debug = false; |
64 | 37 |
|
65 | | - hg.reset(); |
66 | | - const HypernodeID original_contraction_limit_multiplier = |
67 | | - context.coarsening.contraction_limit_multiplier; |
68 | | - if (context.evolutionary.unlimited_coarsening_contraction) { |
69 | | - context.coarsening.contraction_limit_multiplier = 1; |
| 38 | + vec<PartitionID> combinePartitions(Population& population, const std::vector<size_t>& ids); |
| 39 | + |
| 40 | + template <typename TypeTraits> |
| 41 | + Individual usingKWaySelection(const typename TypeTraits::Hypergraph& input_hg, TargetGraph* target_graph, Population& population, Context context, std::mt19937* rng) { |
| 42 | + std::vector<size_t> parents; |
| 43 | + //Maybe change to actually use Tournament Selection |
| 44 | + size_t best(population.randomIndividualSafe(context, rng)); |
| 45 | + parents.push_back(best); |
| 46 | + for (int x = 1; x < context.evolutionary.kway_combine; x++) { |
| 47 | + size_t new_parent = population.randomIndividualSafe(context, rng); |
| 48 | + parents.push_back(new_parent); |
| 49 | + if (population.fitnessAtSafe(new_parent) <= population.fitnessAtSafe(best)) { |
| 50 | + best = new_parent; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + std::vector<PartitionID> best_partition = population.partitionCopySafe(best); |
| 55 | + std::unordered_map<PartitionID, int> comm_to_block; |
| 56 | + vec<PartitionID> comms = combinePartitions(population, parents); |
| 57 | + |
| 58 | + typename TypeTraits::Hypergraph hypergraph = input_hg.copy(parallel_tag_t{}); |
| 59 | + typename TypeTraits::PartitionedHypergraph partitioned_hypergraph(context.partition.k, hypergraph); |
| 60 | + |
| 61 | + for (const HypernodeID& hn : hypergraph.nodes()) { |
| 62 | + partitioned_hypergraph.setOnlyNodePart(hn, best_partition[hn]); |
| 63 | + if (comm_to_block.find(comms[hn]) == comm_to_block.end()) { |
| 64 | + comm_to_block[comms[hn]] = best_partition[hn]; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + partitioned_hypergraph.initializePartition(); |
| 69 | + hypergraph.setCommunityIDs(std::move(comms)); |
| 70 | + |
| 71 | + if (context.partition.mode == Mode::direct) { |
| 72 | + Context vc_context(context); |
| 73 | + vc_context.setupPartWeights(hypergraph.totalWeight()); |
| 74 | + Multilevel<TypeTraits>::evolutionPartitionVCycle( |
| 75 | + hypergraph, partitioned_hypergraph, vc_context, comm_to_block, target_graph); |
| 76 | + } else { |
| 77 | + throw InvalidParameterException("Invalid partitioning mode!"); |
| 78 | + } |
| 79 | + |
| 80 | + return Individual(partitioned_hypergraph, context); |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + return Individual(2); |
70 | 85 | } |
71 | 86 |
|
72 | | - Partitioner().partition(hg, context); |
73 | | - |
74 | | - const HighResClockTimepoint end = std::chrono::high_resolution_clock::now(); |
75 | | - Timer::instance().add(context, Timepoint::evolutionary, |
76 | | - std::chrono::duration<double>(end - start).count()); |
77 | | - |
78 | | - context.coarsening.contraction_limit_multiplier = original_contraction_limit_multiplier; |
79 | | - DBG << "Offspring" << V(metrics::km1(hg)) << V(metrics::imbalance(hg, context)); |
80 | | - ASSERT(metrics::km1(hg) <= std::min(parents.first.fitness(), parents.second.fitness())); |
81 | | - io::serializer::serializeEvolutionary(context, hg); |
82 | | - return Individual(hg, context); |
83 | | -} |
84 | | - |
85 | | -template<typename Hypergraph> |
| 87 | +/*template<typename Hypergraph> |
86 | 88 | Individual usingTournamentSelection(Hypergraph& hg, const Context& context, const Population& population) { |
87 | 89 | Context temporary_context(context); |
88 | 90 |
|
@@ -128,6 +130,6 @@ Individual edgeFrequency(Hypergraph& hg, const Context& context, const Populatio |
128 | 130 | DBG << "final result" << V(metrics::km1(hg)) << V(metrics::imbalance(hg, context)); |
129 | 131 | io::serializer::serializeEvolutionary(temporary_context, hg); |
130 | 132 | return Individual(hg, context); |
131 | | -} |
132 | | -} // namespace combine |
133 | | -} // namespace mt_kahypar |
| 133 | +}*/ |
| 134 | +} // namespace mt_kahypar::combine |
| 135 | + |
0 commit comments