当前位置: 首页>>代码示例>>C++>>正文


C++ graph::neighbours方法代码示例

本文整理汇总了C++中graph::neighbours方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::neighbours方法的具体用法?C++ graph::neighbours怎么用?C++ graph::neighbours使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在graph的用法示例。


在下文中一共展示了graph::neighbours方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: improve_solution_2

bool improve_solution_2(
    graph<pair<int, int>>* solution,
    graph<int>& g1,
    graph<int>& g2,
    unordered_map<int, int>& g2_to_g1_mapping,
    unordered_set<int>& mapped_nodes,
    unordered_set<int>& unmapped_nodes,
    vector<int>& unmapped_nodes_vector,
    float neighbourhood_proportion,
    bool strict_comparisons,
    unsigned int tabu_list_size,
    priority_queue<movement, vector<movement>,
        movement_compare>& tabu_queue,
    movement_compare& mc,
    vector<movement>& tabu_list,
    bool allow_worse,
    const float time_delta,
    int& max_edge_diff,
    const float aspiration_threshold
) {
    enum action_type {none, swap, exchange};

    vector<pair<int, int>> solution_vertices = solution->get_vertices();
    vector<int> g2_vertices = g2.get_vertices();

    bool use_tabu = tabu_list_size > 0;
    bool is_tabu_mapping = false;
    vector<pair<int, int>> tabu_free_mappings;
    vector<bool> tabu_free_mapping_type;

    bool any_improvement = false;
    int best_edge_diff = 0;
    action_type best_action = none;
    pair<int, int> best_node_1;
    pair<int, int> best_node_2;
    vector<int> best_new_edges_1;
    vector<int> best_new_edges_2;

    for (unsigned int i = 0; i < g2.n(); i++) {
        bool i_mapped = mapped_nodes.find(g2_vertices[i]) != mapped_nodes.end();
        for (unsigned int j = 0; j < i; j++) {
            if (rand() < neighbourhood_proportion * RAND_MAX) {
                bool j_mapped = mapped_nodes.find(g2_vertices[j]) != mapped_nodes.end();

                action_type action;
                int node_1;  // to add if exchange
                int node_2;  // to remove if exchange
                int node_1_mapped;
                int node_2_mapped;
                int lost_edges;
                vector<int> new_edges_1;
                vector<int> new_edges_2;
                int edge_diff = -1;  // to avoid trying to make invalid moves

                if (i_mapped && j_mapped) {
                    // cout << "  try SWAP  " << g2_vertices[i] << " <-> " << g2_vertices[j] << endl;
                    action = swap;

                    node_1 = g2_vertices[i];
                    node_2 = g2_vertices[j];
                    node_1_mapped = g2_to_g1_mapping[node_1];
                    node_2_mapped = g2_to_g1_mapping[node_2];

                    lost_edges =
                        solution->degree({node_1_mapped, node_1}) +
                        solution->degree({node_2_mapped, node_2});
                    if (solution->adjacent({node_1_mapped, node_1}, {node_2_mapped, node_2})) {
                        lost_edges -= 2;
                    }

                    vector<int> node_1_neigh = g2.neighbours(node_1);
                    for (unsigned int k = 0; k < node_1_neigh.size(); k++) {
                        if (node_1_neigh[k] != node_2 &&
                            unmapped_nodes.find(node_1_neigh[k]) ==
                                unmapped_nodes.end() &&
                            g1.adjacent(node_2_mapped,
                                g2_to_g1_mapping[node_1_neigh[k]])
                        )
                        {
                            new_edges_1.push_back(node_1_neigh[k]);
                        }
                    }

                    vector<int> node_2_neigh = g2.neighbours(node_2);
                    for (unsigned int k = 0; k < node_2_neigh.size(); k++) {
                        if (node_2_neigh[k] != node_1 &&
                            unmapped_nodes.find(node_2_neigh[k]) ==
                                unmapped_nodes.end() &&
                            g1.adjacent(node_1_mapped,
                                g2_to_g1_mapping[node_2_neigh[k]])
                        )
                        {
                            new_edges_2.push_back(node_2_neigh[k]);
                        }
                    }
                    // cout << "      lost edges:  " << lost_edges  << endl;
                    // cout << "      new edges 1: " << new_edges_1 << endl;
                    // cout << "      new edges 2: " << new_edges_2 << endl;

                    edge_diff = new_edges_1.size() + new_edges_2.size() - lost_edges;
//.........这里部分代码省略.........
开发者ID:ivanpondal,项目名称:aed3-tp3,代码行数:101,代码来源:local_search.cpp

示例2: improve_solution_1

bool improve_solution_1(
    graph<pair<int, int>>* solution,
    graph<int>& g1,
    graph<int>& g2,
    unordered_map<int, int>& g2_to_g1_mapping,
    unordered_set<int>& mapped_nodes,
    unordered_set<int>& unmapped_nodes,
    vector<int>& unmapped_nodes_vector,
    float neighbourhood_proportion,
    bool strict_comparisons,
    unsigned int tabu_list_size,
    priority_queue<movement, vector<movement>,
        movement_compare>& tabu_queue,
    movement_compare& mc,
    vector<movement>& tabu_list,
    bool allow_worse,
    const float time_delta,
    int& max_edge_diff,
    const float aspiration_threshold
) {
    vector<pair<int, int>> solution_vertices =
        solution->get_vertices();

    bool use_tabu = tabu_list_size > 0;
    bool is_tabu_mapping = false;
    vector<pair<int, int>> tabu_free_mappings;

    bool any_improvement = false;
    pair<int, int> best_to_remove;
    pair<int, int> best_to_add;
    int best_edge_diff = 0;
    vector<int> best_new_edges;

    // This iterates over every pair present in the solution, each of
    // which corresponds to a vertex in g1
    for (unsigned int i = 0; i < solution->n(); i++) {
        // This iterates over the possible nodes in g2 to which the
        // current g1 vertex could be associated

        unsigned int neighbourhood_size = unmapped_nodes_vector.size();

        for (unsigned int j = 0; j < neighbourhood_size; j++) {
            if (rand() < neighbourhood_proportion * RAND_MAX) {
                // Possible new pair. Let's evaluate its potential
                int node_mapped    = solution_vertices[i].first;   // part of g1
                int node_to_remove = solution_vertices[i].second;  // part of g2
                int node_to_add    = unmapped_nodes_vector[j];     // part of g2

                int lost_edges = solution->degree({node_mapped, node_to_remove});
                vector<int> new_edges;

                vector<int> node_to_add_neigh = g2.neighbours(node_to_add);
                for (unsigned int k = 0; k < node_to_add_neigh.size(); k++) {
                    if (node_to_add_neigh[k] != node_to_remove &&
                        unmapped_nodes.find(node_to_add_neigh[k]) ==
                            unmapped_nodes.end() &&
                        g1.adjacent(node_mapped,
                            g2_to_g1_mapping[node_to_add_neigh[k]])
                    )
                    {
                        new_edges.push_back(node_to_add_neigh[k]);
                    }
                }

                int edge_diff = new_edges.size() - lost_edges;

                // Check if this movement is tabu
                if (use_tabu && allow_worse) {
                    for (unsigned int k = 0; k < tabu_list.size(); k++) {
                        // If this movement is in the tabu list
                        if (tabu_list[k].mapping.first == node_mapped &&
                                tabu_list[k].mapping.second == node_to_add
                            ) {
                            // If it doesn't gain much edges, we discard it
                            if (edge_diff < max_edge_diff*aspiration_threshold) {
                                is_tabu_mapping = true;
                                break;
                            }
                        }
                    }

                    // Try with next potential matching
                    if (is_tabu_mapping) {
                        is_tabu_mapping = false;
                        continue;
                    }
                    else {
                        tabu_free_mappings.push_back({i, j});
                    }
                }

                bool is_improvement = strict_comparisons
                                        ? edge_diff > best_edge_diff
                                        : edge_diff >= best_edge_diff;

                if (is_improvement) {
                    any_improvement = true;
                    best_to_add = {node_mapped, node_to_add};
                    best_to_remove = {node_mapped, node_to_remove};
                    best_edge_diff = edge_diff;
//.........这里部分代码省略.........
开发者ID:ivanpondal,项目名称:aed3-tp3,代码行数:101,代码来源:local_search.cpp


注:本文中的graph::neighbours方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。