biclique.cpp 3.72 KB
#include <iostream>
#include <time.h>
#include <cfloat>
#include <tuple>
#include <algorithm>

#include "biclique.h"


Biclique::Biclique(int coef1, int coef2, double lambdaMin, double lambdaMax, double U2, unsigned int ncores,
                   const std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float, unsigned int > > &v, const std::vector < std::vector < unsigned int > > &NvBs)
    : Boip(coef1, coef2, lambdaMin, lambdaMax, U2)
{
    vertices = std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float, unsigned int > > (v);
    NvB = std::vector < std::vector < unsigned int > > (NvBs);

    ip_ = new IP(IP::MIN, ncores);

    /*creating variables and objective function*/
    for(size_t i=0, size = vertices.size(); i != size; i++)
    {
        v_.push_back( ip_->make_variable(std::get<3>(vertices[i])));
    }
    ip_->update();

    /*creating constraints*/

    /*for each vertice*/
    for (size_t i = 0, size = vertices.size(); i != size; i++)
    {
        int row = ip_->make_constraint(IP::UP, 0, double(NvB[i].size()));
        ip_->add_constraint(row, v_[i], double(NvB[i].size()));

        for (size_t j = 0, size2 = NvB[i].size(); j != size2; j++)
        {
            ip_->add_constraint(row, v_[NvB[i][j]], 1);
        }
    }

    /*second objective variable as constraint*/
    int ctObj21 = ip_->make_constraint(IP::UP, 0, lambdaMax_);
    int ctObj22 = ip_->make_constraint(IP::LO, lambdaMin_, 0);
    for (size_t i = 0, size = vertices.size(); i != size; i++)
    {
        ip_->add_constraint(ctObj21, v_[i], double(std::get<4>(vertices[i])));
        ip_->add_constraint(ctObj22, v_[i], double(std::get<4>(vertices[i])));
    }

}

Biclique::Biclique(const Biclique& that) :
    Boip(that.coef1_, that.coef2_, that.lambdaMin_, that.lambdaMax_, that.U2_),
    vertices(that.vertices), NvB(that.NvB)
{
    ip_ = new IP ();
    ip_ = that.ip_;
}

Biclique::~Biclique()
{
}

/*void Biclique::add_bj_ct(std::vector< unsigned int > c)
{

    int row = ip_->make_constraint(IP::LO, 0, 0);
    int B(0);
    for(size_t i = 0, size = vertices.size(); i != size; i++)
    {
        if(std::find(c.begin(), c.end(), int(i)) != c.end())
        {
            ip_->add_constraint(row, v_[i], 1);
            B++;
        }
        else
        {
            ip_->add_constraint(row, v_[i], -1);
        }
    }
    ip_->chg_constraint(row, IP::UP, -DBL_MAX, B-1);
}*/

int Biclique::solve(BoipSolution& s)
{
    time_t start, end;  /* returns elapsed time in sec */
    double total_time;
    start = clock();

    int status = ip_->solve();

    end = clock();
    total_time = (double)( end - start )/(double)CLOCKS_PER_SEC;

    printf( "\nElapsed time SOLVE IP: %0.3f \n", total_time );

    if(status == 0)
    {

        //recover first objective value
        double obj1(ip_->get_obj());

        //recover decision variable values
        std::vector < double > v = std::vector < double > (v_.size());
        for (size_t i = 0, size = v_.size(); i != size; i++)
        {
            v[i] = ip_->get_value(v_[i]);
        }

        //recover second objective value
        double obj2 = 0.0;
        for (size_t i = 0, size = vertices.size(); i != size; i++)
        {
            if(ip_->get_value(v_[i]) > 0.5)
                obj2 += std::get<4>(vertices[i]);
        }

        s.set_v_(v);
        s.set_obj1_(obj1);
        s.set_obj2_(obj2);
    }
    return 	status;
}


Biclique& Biclique::operator=(const Biclique& that)
{
    vertices = that.vertices;
    NvB = that.NvB;
    coef1_ = that.coef1_;
    coef2_ = that.coef2_;
    lambdaMin_ = that.lambdaMin_;
    lambdaMax_ = that.lambdaMax_;
    U2_ = that.U2_;
    IP * local_ip = new IP ();
    local_ip = that.ip_;
    delete ip_;
    ip_ = local_ip;
    return *this;
}