819 {
820 FlushTimeMachine T;
821 T.time_machine_start();
822
824
825 auto build_full_map = [&](const std::vector<int>& labels) -> std::vector<int> {
827 for (int i = 0; i < (int)labels.size(); ++i)
828 full_map[used_filaments[i]] = labels[i];
829 return full_map;
830 };
831
832 auto evaluate_labels = [&](const std::vector<int>& labels) -> double {
833 auto full_map = build_full_map(labels);
835 };
836
837 std::vector<int> best_cluster_centers = std::vector<int>(
m_k, 0);
839 double best_cluster_cost = std::numeric_limits<double>::max();
840 int retry_count = 0;
841
842
843 const int retry = std::max(1, budget.max_restarts);
844 auto within_budget = [&]() { return budget.timeout_ms <= 0 || T.time_machine_end() < budget.timeout_ms; };
845
846 while (retry_count < retry && within_budget()) {
849 double curr_cluster_cost = evaluate_labels(curr_cluster_labels);
850
853
854 bool mediods_changed = true;
855 while (mediods_changed && within_budget()) {
856 mediods_changed = false;
857 double best_swap_cost = curr_cluster_cost;
858 int best_swap_cluster = -1;
859 int best_swap_elem = -1;
860
861 for (
size_t cluster_id = 0; cluster_id <
m_k; ++cluster_id) {
862 if (curr_cluster_centers[cluster_id] == -1) continue;
864 if (std::find(curr_cluster_centers.begin(), curr_cluster_centers.end(), elem) != curr_cluster_centers.end() ||
866 continue;
867 std::vector<int> tmp_centers = curr_cluster_centers;
868 tmp_centers[cluster_id] = elem;
870 double tmp_cost = evaluate_labels(tmp_labels);
871
872 if (tmp_cost < best_swap_cost) {
873 best_swap_cost = tmp_cost;
874 best_swap_cluster = cluster_id;
875 best_swap_elem = elem;
876 mediods_changed = true;
877 }
878 }
879 }
880
881 if (mediods_changed) {
882 curr_cluster_centers[best_swap_cluster] = best_swap_elem;
884 curr_cluster_cost = evaluate_labels(curr_cluster_labels);
885
888 }
889 }
890
891 if (curr_cluster_cost < best_cluster_cost) {
892 best_cluster_centers = curr_cluster_centers;
893 best_cluster_cost = curr_cluster_cost;
894 best_cluster_labels = curr_cluster_labels;
895 }
896 retry_count += 1;
897 }
899 }
std::vector< int > init_cluster_center(const std::unordered_map< int, std::vector< int > > &placeable_limits, const std::unordered_map< int, std::vector< int > > &unplaceable_limits, const std::vector< int > &cluster_size, const std::vector< std::pair< std::set< int >, int > > &cluster_group_size, int seed)
Definition FilamentGroup.cpp:694
std::vector< int > assign_cluster_label(const std::vector< int > ¢er, const std::unordered_map< int, std::vector< int > > &placeable_limits, const std::unordered_map< int, std::vector< int > > &unplaceable_limits, const std::vector< int > &group_size, const std::vector< std::pair< std::set< int >, int > > &cluster_group_size)
Definition FilamentGroup.cpp:736
std::vector< std::pair< std::set< int >, int > > m_cluster_group_size
Definition FilamentGroup.hpp:263
MemoryedGroupHeap memoryed_groups
Definition FilamentGroup.hpp:257
std::unordered_map< int, std::vector< int > > m_unplaceable_limits
Definition FilamentGroup.hpp:259
FilamentGroupUtils::MemoryedGroup MemoryedGroup
Definition FilamentGroup.hpp:220
double memory_threshold
Definition FilamentGroup.hpp:270
std::unordered_map< int, std::vector< int > > m_placeable_limits
Definition FilamentGroup.hpp:260
std::vector< int > m_cluster_labels
Definition FilamentGroup.hpp:262
void update_memoryed_groups(const MemoryedGroup &item, const double gap_threshold, MemoryedGroupHeap &groups)
Definition FilamentGroup.cpp:518
static double full_evaluate(const std::vector< unsigned int > &used_filaments, const std::vector< int > &filament_nozzle_map, const FilamentGroupContext &ctx, std::optional< std::function< bool(int, std::vector< int > &)> > get_custom_seq=std::nullopt, int *out_flush=nullptr)
Definition FilamentGroup.cpp:114
std::vector< unsigned int > collect_sorted_used_filaments(const std::vector< std::vector< unsigned int > > &layer_filaments)
Definition FilamentGroup.cpp:563