ipcomp.cpp 9.55 KB
#include <iostream>
#include <time.h>
#include <cfloat>
#include <tuple>
#include <algorithm>

#include "ipcomp.h"


Ipcomp::Ipcomp(const std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float > > &v, const std::vector < std::vector < unsigned int > > &NvBs, unsigned int ncores)
{
    vertices = std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float > > (v);
    NvB = std::vector < std::vector < unsigned int > > (NvBs);
    ip_ = new IP(IP::MIN, ncores);
    //int nbVs = vertices.size();
    //int nbEd = 0;

    /*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])));
    }
    //std::cout << "apres obj function " << std::endl;
    ip_->update();

    /*creating constraints*/




    /*for each vertice*/
    for (size_t i = 0, size = vertices.size(); i != size; i++)
    {
        //std::cout << "i =  " << i << std::endl;
        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);
        }
    }


    //std::cout << "fin ipcomp" << std::endl;


}

/*
Ipcomp::Ipcomp(std::vector< std::vector < Structure > > strs, std::vector< std::vector < SolInteraction > >  intes, int ncores) : strs_(strs), intes_(intes)
{
    //std::cout << "debut ipcomp " << std::endl;

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


    int nbVs = 0;
    int nbVi = 0;
    int nbEd = 0;

    /*creating variables and objective function*/
/*    for(int i=0; i < int(strs_.size()); i++)
    {
        vs_.push_back(std::vector < int > ());
        for(int j = 0; j < int(strs_[i].size()); j++)
        {
            vs_[i].push_back( ip_->make_variable(strs_[i][j].get_obj1_()));
            nbVs++;
        }
    }
    for(int i=0; i < int(intes_.size()); i++)
    {
        vi_.push_back(std::vector < int > ());
        for(int j = 0; j < int(intes_[i].size()); j++)
        {
            //std::cout << "j =  " << j << "/" << int(intes_[j].size()) << std::endl;
            vi_[i].push_back( ip_->make_variable(intes_[i][j].get_score_()));
            nbVi++;
        }
    }
   // std::cout << "apres obj function intes.size = " << intes_.size() << " strs.size = " << strs.size() << std::endl;
    ip_->update();

    /*creating constraints*/




    /*for each interaction vertice*/
    /*find if match between intes/intes, intes/strs*/
/*    int nbIntInt = 0;
    int nbIntStr = 0;
    for (int i = 0; i < int(intes_.size()); i++)
    { //For any interaction of two RNAs
        //std::cout << "i =  " << i << std::endl;
        for (int j = 0; j < int(intes_[i].size()); j++)
        {//For any suboptimal interaction solution

            //std::cout << "j =  " << j << "/" << int(intes_[i].size()) << std::endl;
            int nbNoMatch = 0;
            int row = ip_->make_constraint(IP::UP, 0, 1);

            //std::cout << "nrow = " << row << std::endl;
            /*intes/intes*/
/*            for (int k = 0; k < int(intes_.size()); k++)
            { //For any interaction of two RNAs remaining
                for (int l = 0; l < int(intes_[k].size()); l++)
                {//For any suboptimal interaction solution remaining
                    if(i != k or j != l)
                    {
                        //std::cout << "k,l " << k << "," << l << std::endl;
                        if (! matchIntInt(intes_[i][j],intes_[k][l]))
                        {
                            //std::cout << "no match" << std::endl;
                            nbNoMatch++;
                            ip_->add_constraint(row, vi_[k][l], 1);
                        }
                        else{ nbIntInt++;}
                    }
                }
            }


           // std::cout << "apres intes" << std::endl;
            for (int k = 0; k < int(strs_.size()); k++)
            {//For any rna
                for (int l = 0; l < int(strs_[k].size()); l++)
                { //For any suboptimal structure solution
                    //std::cout << "l =  " << l << "/" << int(strs_[k].size()) << std::endl;

                    //std::cout << "inte/str: " << intes_[i][j].convToDP() << strs_[k][l].convToDP() << " match=" << matchStrInt(intes_[i][j], strs_[k][l]) << std::endl;
                    /*intes/strs*/
/*                    if (! matchStrInt(intes_[i][j], strs_[k][l]))
                    {
                        //std::cout << "no match" << std::endl;
                        nbNoMatch++;
                        ip_->add_constraint(row, vs_[k][l], 1);
                    }else{  nbEd++; nbIntStr++; }
                }
            }




            //std::cout << "apres strs" << std::endl;
            ip_->add_constraint(row, vi_[i][j], nbNoMatch);
            ip_->chg_constraint(row, IP::UP, 0, nbNoMatch);
           // std::cout << "apres chg_constraint" << std::endl;


        }
    }

    /*for each structure vertice*/
    /*find if match between strs/strs, intes/strs*/
/*    int nbStrStr = 0;
    for (int i = 0; i < int(strs_.size()); i++)
    { //For any RNA
        //std::cout << "i =  " << i << std::endl;

        //std::cout << "nrow2 = " << row2 << std::endl;

        for (int j = 0; j < int(strs_[i].size()); j++)
        {//For any suboptimal str solution

            //std::cout << "j =  " << j << "/" << int(strs_[i].size()) << std::endl;
            int nbNoMatch = 0;
            int row = ip_->make_constraint(IP::UP, 0, 1);

            //std::cout << "nrow = " << row << std::endl;

            /*strs/strs*/
/*            for (int k = 0; k < int(strs_.size()); k++)
            {
                for (int l = 0; l < int(strs_[k].size()); l++)
                {//For any suboptimal str solution remaining
                    if(i != k or j != l)
                    {
                        //std::cout << "k,l " << k << "," << l << std::endl;
                        if (! matchStrStr(strs_[i][j],strs_[k][l]))
                        {
                            nbNoMatch++;
                            ip_->add_constraint(row, vs_[k][l], 1);
                        }
                        else{ nbStrStr++;}
                    }
                }
            }

            /*intes/strs*/
/*            for (int k = 0; k < int(intes_.size()); k++)
            {//For any rna
                for (int l = 0; l < int(intes_[k].size()); l++)
                {//For any suboptimal interaction solution remaining
                    //std::cout << "l =  " << l << "/" << int(strs_[k].size()) << std::endl;
                    if (! matchStrInt(intes_[k][l], strs_[i][j]))
                    {
                        //std::cout << "no match" << std::endl;
                        nbNoMatch++;
                        ip_->add_constraint(row, vi_[k][l], 1);
                    }

                }
            }
            //std::cout << "apres strs" << std::endl;
            ip_->add_constraint(row, vs_[i][j], nbNoMatch);
            ip_->chg_constraint(row, IP::UP, 0, nbNoMatch);
           // std::cout << "apres chg_constraint" << std::endl;


        }
    }
    nbEd+=nbStrStr/2.0 + nbIntInt/2.0;



    std::cout << "fin ipcomp nbVs =" << nbVs << " nbVi=" << nbVi << " nbVs+nbVi=" << nbVs+nbVi << " nbEd=" << nbEd  << " nbStrStr=" << nbStrStr/2.0 << " nbIntInt=" << nbIntInt/2.0 << " nbIntStr=" << nbIntStr << std::endl;
}*/

Ipcomp::~Ipcomp()
{
    delete ip_;
}

void Ipcomp::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);
}

/*void Ipcomp::add_bj_ct(const ComplexeIP &cp)
{

    int row = ip_->make_constraint(IP::LO, 0, 0);
    int B(0);
    for(int i=0; i < int(strs_.size()); i++)
    {
        for(int j = 0; j < int(strs_[i].size()); j++)
        {
            if(cp.get_vars_()[i][j] > 0.5)
            {
                ip_->add_constraint(row, vs_[i][j], 1);
                B++;
            }
            else {
                ip_->add_constraint(row, vs_[i][j], -1);
            }
        }
    }
    for(int i=0; i < int(intes_.size()); i++)
    {
        for(int j = 0; j < int(intes_[i].size()); j++)
        {
            if(cp.get_vari_()[i][j] > 0.5)
            {
                ip_->add_constraint(row, vi_[i][j], 1);
                B++;
            }
            else {
                ip_->add_constraint(row, vi_[i][j], -1);
            }
        }
    }
    ip_->chg_constraint(row, IP::UP, -DBL_MAX, B-1);
}*/

int Ipcomp::solve(std::vector< unsigned int > &c, float &score)
{
    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){

        score = ip_->get_obj();
        float obj12(0.0);

        //recover decision variable values
        for(size_t i=0, size = v_.size(); i != size; i++)
        {
            obj12+=ip_->get_value(v_[i])*std::get<3>(vertices[i]);
            if(ip_->get_value(v_[i]) > 0.5)
                c.push_back(i);
        }
        //std::cout << "ob12=" << obj12 << std::endl;
    }
    return 	status;
}