簡體   English   中英

停止Kamada-Kawai布局的條件

[英]Stopping condition for Kamada-Kawai layout

我使用以下代碼來獲取Kamada-Kawai布局:

template <class PointMap>
PointMap layout() const {
    PointMap res;
    boost::associative_property_map<PointMap> temp(res);

    minstd_rand gen;
    rectangle_topology<> rect_top(gen, 0, 0, 50, 50);
    random_graph_layout(g_, temp, rect_top); // random layout to show that
                                             // Kamada-Kawai isn't doing the job

    // circle_graph_layout(g_, temp, 10.0);

    // http://stackoverflow.com/q/33903879/2725810
    // http://stackoverflow.com/a/8555715/2725810
    typedef std::map<VertexDescriptor, std::size_t> IndexMap;
    IndexMap mapIndex;
    associative_property_map<IndexMap> propmapIndex(mapIndex);
    // http://www.boost.org/doc/libs/1_59_0/libs/graph/doc/bundles.html
    kamada_kawai_spring_layout(
        g_, temp,
        boost::make_transform_value_property_map([](int i)
                                                     ->double { return i; },
                                                 get(edge_bundle, g_)),
        //get(edge_bundle, g_),
        square_topology<>(50.0), side_length(50.0),
        //layout_tolerance<CostType>(0.01),
        kamada_kawai_done(),
        CostType(1), propmapIndex);

    return res;
}

使用以下類型:

  • 圖表類型是:

     boost::adjacency_list<vecS, setS, undirectedS, State, CostType>; 

    其中CostTypeint

  • PointMap是:

     std::map<VertexDescriptor, square_topology<>::point_type> 

這是我正在使用的停止條件:

struct kamada_kawai_done
{
    kamada_kawai_done() : last_delta() {}

    template<typename Graph>
    bool operator()(double delta_p,
                    typename boost::graph_traits<Graph>::vertex_descriptor /*p*/,
                    const Graph& /*g*/,
                    bool global)
    {
        if (global) {
            double diff = last_delta - delta_p;
            if (diff < 0) diff = -diff;
            std::cout << "delta_p: " << delta_p << std::endl;
            last_delta = delta_p;
            return diff < 0.01;
        } else {
            return delta_p < 0.01;
        }
    }

    double last_delta;
};

請注意,它會在每次迭代時顯示delta_p

我正在運行這個只有六個頂點的簡單圖形。 delta_p只顯示一次,它是0.鑒於初始布局是隨機的,這真的很奇怪。 這是我得到的圖片: 與開羅一起顯示的布局

正如你所看到的,隨機布局並不漂亮,而Kamada-Kawai並沒有做到這一點。

我嘗試了另一個停止條件: layout_tolerance<CostType>(0.01) 這導致Kamada-Kawai永遠奔跑。

我在這做錯了什么?

PS:由於我無法在瀏覽器中看到圖片,以防它沒有附加,這里是圖形的鄰接結構。 該圖表示三個煎餅情況下的煎餅拼圖的狀態空間。 即,頂點對應於數字0,1,2的不同排列,並且每個頂點有兩個邊(都具有權重1):

[0, 2, 1]:
    [2, 0, 1] (w=1)
    [1, 2, 0] (w=1)
[2, 0, 1]:
    [0, 2, 1] (w=1)
    [1, 0, 2] (w=1)
[1, 2, 0]:
    [0, 2, 1] (w=1)
    [2, 1, 0] (w=1)
[2, 1, 0]:
    [1, 2, 0] (w=1)
    [0, 1, 2] (w=1)
[1, 0, 2]:
    [2, 0, 1] (w=1)
    [0, 1, 2] (w=1)
[0, 1, 2]:
    [1, 0, 2] (w=1)
    [2, 1, 0] (w=1)

更新 :這是我的代碼,以實現接受的答案:

template <class PointMap> PointMap layout() const {
    PointMap res;

    // Make a copy into a graph that is easier to deal with:
    //     -- vecS for vertex set, so there is index map
    //     -- double for edge weights
    using LayoutGraph =
        boost::adjacency_list<vecS, vecS, undirectedS, int, double>;
    using LayoutVertexDescriptor =
        typename graph_traits<LayoutGraph>::vertex_descriptor;
    std::map<VertexDescriptor, LayoutVertexDescriptor> myMap;
    std::map<LayoutVertexDescriptor, VertexDescriptor> myReverseMap;

    LayoutGraph lg; // This is the copy

    // Copy vertices
    for (auto vd : vertexRange()) {
        auto lvd = add_vertex(lg);
        myMap[vd] = lvd;
        myReverseMap[lvd] = vd;
    }

    // Copy edges
    for (auto from: vertexRange()) {
        for (auto to: adjacentVertexRange(from)) {
            auto lfrom = myMap[from], lto = myMap[to];
            if (!edge(lfrom, lto, lg).second)
                add_edge(lfrom, lto, (double)(g_[edge(to, from, g_).first]),
                         lg);
        }
    }
    // Done copying

    using LayoutPointMap =
        std::map<LayoutVertexDescriptor, square_topology<>::point_type>;
    LayoutPointMap intermediateResults;
    boost::associative_property_map<LayoutPointMap> temp(
        intermediateResults);

    minstd_rand gen;
    rectangle_topology<> rect_top(gen, 0, 0, 100, 100);
    random_graph_layout(lg, temp, rect_top);

    // circle_graph_layout(lg, temp, 10.0);

    kamada_kawai_spring_layout(lg, temp, get(edge_bundle, lg),
                               square_topology<>(100.0), side_length(100.0),
                               //layout_tolerance<CostType>(0.01));
                               kamada_kawai_done());

    for (auto el: intermediateResults)
        res[myReverseMap[el.first]] = el.second;

    return res;
}

對於6個頂點,布局是一個完美的sexagon,所以它的工作原理! 對於24個頂點,最后顯示的delta_p是~2.25(不應低於0.01?)。 此外,從隨機布局開始時的布局比從圓形布局開始時更漂亮......

使用較小的矩形(例如20乘20而不是100乘100)會導致布局不太美觀,因此使用layout_tolerance<double>(0.01)作為停止條件。

我認為中間近似可能存儲在實際的邊緣束屬性中,這使得它轉換為整數。

由於輸入的規模,它顯然失去了實現(局部)最佳布局的重要數字。 我建議用邊緣束加一個看看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM