Program Listing for File IndependentSetImprovement.h

↰ Return to documentation for file (include/IndependentSetImprovement.h)

#ifndef INDEPENDENT_SET_IMPROVEMENT_H
#define INDEPENDENT_SET_IMPROVEMENT_H

#include "DataTypeHandling.h"
#include "SubmodularOptimizer.h"
#include <algorithm>
#include <numeric>
#include <random>
#include <unordered_set>
#include <string>
#include <queue>

#include <iostream>

class IndependentSetImprovement : public SubmodularOptimizer {

protected:

    struct Pair {
        // The weight
        data_t weight;

        // The index in the summary
        unsigned int idx;

        Pair(data_t _weight, unsigned int _idx) {
            weight = _weight;
            idx = _idx;
        }

        bool operator < (const Pair &other) const {
            return weight > other.weight;
        }
    };

    // The priority queue
    std::priority_queue<Pair> weights;
public:

    IndependentSetImprovement(unsigned int K, SubmodularFunction & f) : SubmodularOptimizer(K,f)  {
    }

    IndependentSetImprovement(unsigned int K, std::function<data_t (std::vector<std::vector<data_t>> const &)> f) : SubmodularOptimizer(K,f) {
    }


    void next(std::vector<data_t> const &x, std::optional<idx_t> const id = std::nullopt) {
        unsigned int Kcur = solution.size();

        if (Kcur < K) {
            data_t w = f->peek(solution, x, solution.size()) - fval;
            f->update(solution, x, solution.size());
            solution.push_back(x);
            if (id.has_value()) ids.push_back(id.value());
            weights.push(Pair(w, Kcur));
        } else {
            Pair to_replace = weights.top();
            data_t w = f->peek(solution, x, solution.size()) - fval;
            if (w > 2*to_replace.weight) {
                f->update(solution, x, to_replace.idx);
                solution[to_replace.idx] = x;
                if (id.has_value()) ids[to_replace.idx] = id.value();
                weights.pop();
                weights.push(Pair(w, to_replace.idx));
            }
        }
        fval = f->operator()(solution);
        is_fitted = true;
    }
};

#endif