ENH: add tool order function

1.Use min cost max flow to solve the tool order

jira:NEW

Signed-off-by: xun.zhang <xun.zhang@bambulab.com>
Change-Id: I909845039b67c7fe3ddd42580ad3f1d71d52262d
This commit is contained in:
xun.zhang 2024-08-08 11:23:29 +08:00 committed by lane.wei
parent 0f70c81a7d
commit 0716b8518e
4 changed files with 159 additions and 18 deletions

View File

@ -52,6 +52,7 @@ namespace Slic3r
std::optional<std::function<bool(int, std::vector<int>&)>> get_custom_seq;
};
class KMediods
{
enum INIT_TYPE
@ -83,5 +84,6 @@ namespace Slic3r
private:
std::vector<int>m_filament_labels;
};
}
#endif // !FILAMENT_GROUP_HPP

View File

@ -8,6 +8,124 @@
namespace Slic3r
{
MCMF::MCMF(const FlushMatrix& matrix_, const std::vector<int>& u_nodes, const std::vector<int>& v_nodes)
{
matrix = matrix_;
l_nodes = u_nodes;
r_nodes = v_nodes;
total_nodes = u_nodes.size() + v_nodes.size() + 2;
source_id = total_nodes - 2;
sink_id = total_nodes - 1;
adj.resize(total_nodes);
//add edge from source to left nodes,set capacity to 1, cost to 0
for (int i = 0; i < l_nodes.size(); ++i)
add_edge(source_id, i, 1, 0);
//add edge from right nodes to sink,set capacity to 1, cost to 0
for (int i = 0; i < r_nodes.size(); ++i)
add_edge(l_nodes.size() + i, sink_id, 1, 0);
for (int i = 0; i < l_nodes.size(); ++i) {
int from_idx = i;
for (int j = 0; j < r_nodes.size(); ++j) {
int to_idx = l_nodes.size() + j;
add_edge(from_idx, to_idx, 1, get_distance(i, j));
}
}
}
std::vector<int> MCMF::solve()
{
while (spfa(source_id, sink_id));
std::vector<int>matching(l_nodes.size(), -1);
// to get the match info, just traverse the left nodes and
// check the edges with flow > 0 and linked to right nodes
for (int u = 0; u < l_nodes.size(); ++u) {
for (int eid : adj[u]) {
Edge& e = edges[eid];
if (e.flow > 0 && e.to >= l_nodes.size() && e.to < l_nodes.size() + r_nodes.size())
matching[e.from] = r_nodes[e.to - l_nodes.size()];
}
}
return matching;
}
void MCMF::add_edge(int from, int to, int capacity, int cost)
{
adj[from].emplace_back(edges.size());
edges.emplace_back(from, to, capacity, cost);
//also add reverse edge ,set capacity to zero,cost to negative
adj[to].emplace_back(edges.size());
edges.emplace_back(to, from, 0, -cost);
}
bool MCMF::spfa(int source, int sink)
{
std::vector<int>dist(total_nodes, INF);
std::vector<bool>in_queue(total_nodes, false);
std::vector<int>flow(total_nodes, INF);
std::vector<int>prev(total_nodes, 0);
std::queue<int>q;
q.push(source);
in_queue[source] = true;
dist[source] = 0;
while (!q.empty()) {
int now_at = q.front();
q.pop();
in_queue[now_at] = false;
for (auto eid : adj[now_at]) //traverse all linked edges
{
Edge& e = edges[eid];
if (e.flow<e.capacity && dist[e.to]>dist[now_at] + e.cost) {
dist[e.to] = dist[now_at] + e.cost;
prev[e.to] = eid;
flow[e.to] = std::min(flow[now_at], e.capacity - e.flow);
if (!in_queue[e.to]) {
q.push(e.to);
in_queue[e.to] = true;
}
}
}
}
if (dist[sink] == INF)
return false;
int now_at = sink;
while (now_at != source) {
int prev_edge = prev[now_at];
edges[prev_edge].flow += flow[sink];
edges[prev_edge ^ 1].flow -= flow[sink];
now_at = edges[prev_edge].from;
}
return true;
}
int MCMF::get_distance(int idx_in_left, int idx_in_right)
{
if (l_nodes[idx_in_left] == -1) {
return 0;
//TODO: test more here
int sum = 0;
for (int i = 0; i < matrix.size(); ++i)
sum += matrix[i][idx_in_right];
sum /= matrix.size();
return -sum;
}
return matrix[l_nodes[idx_in_left]][r_nodes[idx_in_right]];
}
//solve the problem by searching the least flush of current filament
static std::vector<unsigned int> solve_extruder_order_with_greedy(const std::vector<std::vector<float>>& wipe_volumes,
const std::vector<unsigned int> curr_layer_extruders,
@ -171,6 +289,7 @@ namespace Slic3r
}
// get best filament order of single nozzle
std::vector<unsigned int> get_extruders_order(const std::vector<std::vector<float>>& wipe_volumes,
const std::vector<unsigned int>& curr_layer_extruders,
@ -202,7 +321,6 @@ namespace Slic3r
}
int reorder_filaments_for_minimum_flush_volume(const std::vector<unsigned int>& filament_lists,
const std::vector<int>& filament_maps,
const std::vector<std::vector<unsigned int>>& layer_filaments,
@ -378,5 +496,4 @@ namespace Slic3r
return cost;
}
}

View File

@ -9,6 +9,36 @@ namespace Slic3r {
using FlushMatrix = std::vector<std::vector<float>>;
class MCMF
{
const int INF = std::numeric_limits<int>::max();
struct Edge
{
int from, to, capacity, cost, flow;
Edge(int u, int v, int cap, int cst) : from(u), to(v), capacity(cap), cost(cst), flow(0) {}
};
public:
MCMF(const FlushMatrix &matrix_, const std::vector<int> &u_nodes, const std::vector<int> &v_nodes);
std::vector<int> solve();
private:
void add_edge(int from, int to, int capacity, int cost);
bool spfa(int source, int sink);
int get_distance(int idx_in_left, int idx_in_right);
private:
FlushMatrix matrix;
std::vector<int> l_nodes;
std::vector<int> r_nodes;
int total_nodes;
int source_id;
int sink_id;
std::vector<Edge> edges;
std::vector<std::vector<int>> adj;
};
std::vector<unsigned int> get_extruders_order(const std::vector<std::vector<float>> &wipe_volumes,
const std::vector<unsigned int> &curr_layer_extruders,
@ -24,6 +54,5 @@ int reorder_filaments_for_minimum_flush_volume(const std::vector<unsigned int> &
std::optional<std::function<bool(int, std::vector<int> &)>> get_custom_seq,
std::vector<std::vector<unsigned int>> *filament_sequences);
}
#endif // !TOOL_ORDER_UTILS_HPP

View File

@ -851,29 +851,22 @@ std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<s
std::vector<int>ret(filament_nums,0);
// if mutli_extruder, calc group,otherwise set to 0
if (extruder_nums == 2)
{
if (extruder_nums == 2) {
std::vector<std::string> extruder_ams_count_str = print_config->extruder_ams_count.values;
auto extruder_ams_counts = get_extruder_ams_count(extruder_ams_count_str);
std::vector<int> group_size = { 16, 16 };
auto extruder_ams_counts = get_extruder_ams_count(extruder_ams_count_str);
std::vector<int> group_size = {16, 16};
if (extruder_ams_counts.size() > 0) {
assert(extruder_ams_counts.size() == 2);
for (int i = 0; i < extruder_ams_counts.size(); ++i) {
group_size[i] = 0;
const auto& ams_count = extruder_ams_counts[i];
for (auto iter = ams_count.begin(); iter != ams_count.end(); ++iter) {
group_size[i] += iter->first * iter->second;
}
group_size[i] = 0;
const auto &ams_count = extruder_ams_counts[i];
for (auto iter = ams_count.begin(); iter != ams_count.end(); ++iter) { group_size[i] += iter->first * iter->second; }
}
}
FilamentGroup fg(
nozzle_flush_mtx,
(int)filament_nums,
group_size
);
FilamentGroup fg(nozzle_flush_mtx, (int) filament_nums, group_size);
fg.get_custom_seq = get_custom_seq;
ret = fg.calc_filament_group(layer_filaments, FGStrategy::BestFit);
ret = fg.calc_filament_group(layer_filaments, FGStrategy::BestFit);
}
return ret;