Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/adshhzy/VIPSS
21 September 2020, 16:04:55 UTC
  • Code
  • Branches (1)
  • Releases (0)
  • Visits
Revision 19e9e0f636c421901ec39d7608cc2fb69f486eb6 authored by adshhzy on 17 April 2019, 05:35:22 UTC, committed by adshhzy on 17 April 2019, 05:35:22 UTC
readme
1 parent 50d6f74
  • Files
  • Changes
    • Branches
    • Releases
    • HEAD
    • refs/heads/master
    • 19e9e0f636c421901ec39d7608cc2fb69f486eb6
    No releases to show
  • 28b9470
  • /
  • src
  • /
  • Solver.cpp
Raw File Download
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
  • snapshot
origin badgerevision badge
swh:1:rev:19e9e0f636c421901ec39d7608cc2fb69f486eb6
origin badgedirectory badge
swh:1:dir:62ca9b75fd35e168030aa784b2487900fa04e3ef
origin badgecontent badge
swh:1:cnt:354d629cfdd1cf7680c9243a00986708aa04de3a
origin badgesnapshot badge
swh:1:snp:416b2d138b45cb2802453235f1dabd6b268aa79a

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: 19e9e0f636c421901ec39d7608cc2fb69f486eb6 authored by adshhzy on 17 April 2019, 05:35:22 UTC
readme
Tip revision: 19e9e0f
Solver.cpp

#include "Solver.h"

#include <ctime>
#include <chrono>
#include <eigen3/Eigen/CholmodSupport>
#include <gurobi_c++.h>

typedef std::chrono::high_resolution_clock Clock;
//static GRBModel static_model;

void LinearVec::set_label(int label){
    this->label = label;
}

void LinearVec::set_ab(double a, double b){
    this->a = a;
    this->b = b;
}

void LinearVec::clear(){
    ts.clear();
    ws.clear();
}

void LinearVec::push_back(int t, double w){
    ts.push_back(t);
    ws.push_back(w);
}

void QuadraticVec::push_back(int i,int j,double val){
    ts.push_back(triple(i,j,val));
}

void QuadraticVec::clear(){
    ts.clear();
}

void QuadraticVec::set_b(double b){
    this->b = b;
}

void Solution_Struct::init(int n){
    solveval.reserve(n);
    Statue = 0;
}

int solveQuadraticProgramming_Core(GRBModel &model, vector<GRBVar>&vars, Solution_Struct &sol, bool suppressinfo = false){

    int n = vars.size();

    try{

        auto t1 = Clock::now();

        model.optimize();

        auto t2 = Clock::now();

        if(!suppressinfo)cout << "Total opt time: " << (sol.time = std::chrono::nanoseconds(t2 - t1).count()/1e9) <<endl<<endl;
        if(!suppressinfo)cout << "Status: " << model.get(GRB_IntAttr_Status) << endl;

        //cout<<"0"<<endl;
        if (sol.Statue = (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL)) {
            sol.solveval.resize(n);
            //cout<<"1"<<endl;
            sol.energy = model.get(GRB_DoubleAttr_ObjVal);
            if(!suppressinfo)cout << "Obj: " << sol.energy << endl;
            //cout<<"2"<<endl;
            for(int i=0;i<n;++i)sol.solveval[i] = vars[i].get(GRB_DoubleAttr_X);
            //for(int i=0;i<n;++i)cout<<sol.solveval[i]<<' ';cout<<endl;

        }

    }catch (GRBException e) {
        cout << "Error code = " << e.getErrorCode() << endl;
        cout << e.getMessage() << endl;
    } catch (...) {
        cout << "Exception during optimization" << endl;
    }

    return model.get(GRB_IntAttr_Status);
    //return time;

}
int Solver::solveQuadraticProgramming(vector<triple> &M, vector<triple> &Ab, int n, vector<double>&solveval){


    vector<GRBVar>vars(n);

    try{

        GRBEnv env = GRBEnv();
        //cout << "optimize 0" << endl;
        GRBModel model = GRBModel(env);

        model.getEnv().set(GRB_IntParam_OutputFlag, 0);



        for(int i=0;i<n;++i)vars[i] = model.addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS,to_string(i));


        //cout << "optimize 1" << endl;
        GRBQuadExpr obj = 0;

        for(int i=0;i<M.size();++i){

            obj += vars[M[i].i] * vars[M[i].j] * M[i].val;

        }
        // cout << "optimize 2 " <<codeer<< endl;
        model.setObjective(obj, GRB_MINIMIZE);


        //model.update();

        for(int i=0;i<Ab.size();++i){
            model.addConstr(vars[Ab[i].i] - vars[Ab[i].j] >= Ab[i].val);
        }



        auto t1 = Clock::now();

        model.optimize();

        auto t2 = Clock::now();

        cout << "Total opt time: " << std::chrono::nanoseconds(t2 - t1).count()/1e9<< endl<< endl;
        //cout << "Status: " << model.get(GRB_IntAttr_Status) << endl;
        if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
            solveval.resize(n);
            cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
            for(int i=0;i<n;++i)solveval[i] = vars[i].get(GRB_DoubleAttr_X) ;
        }


    }catch (GRBException e) {
        cout << "Error code = " << e.getErrorCode() << endl;
        cout << e.getMessage() << endl;
    } catch (...) {
        cout << "Exception during optimization" << endl;
    }


	return 1;
}


int Solver::solveQuadraticProgramming(vector<triple> &M, vector<LinearVec> &Ab, int n, vector<double>&solveval){

    vector<GRBVar>vars(n);

    try{

        GRBEnv env = GRBEnv();
        //cout << "optimize 0" << endl;
        GRBModel model = GRBModel(env);

        model.getEnv().set(GRB_IntParam_OutputFlag, 0);

        for(int i=0;i<n;++i)vars[i] = model.addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS,to_string(i));

        //cout << "optimize 1" << endl;
        GRBQuadExpr obj = 0;

        for(int i=0;i<M.size();++i){

            obj += vars[M[i].i] * vars[M[i].j] * M[i].val;

        }
        // cout << "optimize 2 " <<codeer<< endl;
        model.setObjective(obj, GRB_MINIMIZE);


        //model.update();

        for(int i=0;i<Ab.size();++i){
            auto &a = Ab[i];
            GRBLinExpr cons = 0;
            for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]] * a.ws[i];
            if(a.label == 0)model.addConstr(cons == a.b);
            else if(a.label == -1)model.addConstr(cons <= a.b);
            else if(a.label == 1)model.addConstr(cons >= a.b);
        }



        auto t1 = Clock::now();

        model.optimize();

        auto t2 = Clock::now();

        cout << "Total opt time: " << std::chrono::nanoseconds(t2 - t1).count()/1e9<< endl<< endl;
        //cout << "Status: " << model.get(GRB_IntAttr_Status) << endl;
        if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
            solveval.resize(n);
            cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
            for(int i=0;i<n;++i)solveval[i] = vars[i].get(GRB_DoubleAttr_X) ;
        }

    }catch (GRBException e) {
        cout << "Error code = " << e.getErrorCode() << endl;
        cout << e.getMessage() << endl;
    } catch (...) {
        cout << "Exception during optimization" << endl;
    }
	return 1;
}

int Solver::solveQuadraticProgramming(arma::mat &M, vector<LinearVec> &Ab, int n, Solution_Struct &sol){

    vector<GRBVar>vars(n);
    int re;

    sol.init(n);

    try{

        GRBEnv env = GRBEnv();
        //cout << "optimize 0" << endl;
        GRBModel model = GRBModel(env);

        model.getEnv().set(GRB_IntParam_OutputFlag, 0);

        for(int i=0;i<n;++i)vars[i] = model.addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS,to_string(i));

        //cout << "optimize 1" << endl;
        GRBQuadExpr obj = 0;

        int mm = M.n_rows;
        for(int i=0;i<mm;++i)for(int j=0;j<mm;++j){

            obj += vars[i] * vars[j] * M(i,j);

        }
        // cout << "optimize 2 " <<codeer<< endl;
        model.setObjective(obj, GRB_MINIMIZE);


        int ec = 0, iec = 0;
        for(int i=0;i<Ab.size();++i){
            auto &a = Ab[i];
            GRBLinExpr cons = 0;
            for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]] * a.ws[i];
            //cout<<a.ts.size()<<endl;
            if(a.a == a.b){model.addConstr(cons == a.b);ec++;}
            else {model.addConstr(cons >= a.a);model.addConstr(cons <= a.b);iec+=2;}

        }
        cout<<ec<<' '<<iec<<endl;


        cout<<"Begin Solve"<<endl;
        re = solveQuadraticProgramming_Core(model, vars, sol);

        if(0){
            for(int i=0;i<Ab.size();++i){
                auto &a = Ab[i];
                double cons = 0;
                for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]].get(GRB_DoubleAttr_X) * a.ws[i];
                cout<<cons<<endl;
            }
        }



    }catch (GRBException e) {
        cout << "Error code = " << e.getErrorCode() << endl;
        cout << e.getMessage() << endl;
    } catch (...) {
        cout << "Exception during optimization" << endl;
    }

    return re;


}





int Solver::solveLinearLS(vector<triple> &M, vector<double> &b, int n, vector<double>&solveval){

	return -1;


}



int Solver::solveQCQP(arma::mat &M, vector<LinearVec> &Ab, vector<QuadraticVec> &Cb, int n, Solution_Struct &sol){
    vector<GRBVar>vars(n);
    int re;

    sol.init(n);

    try{

        GRBEnv env = GRBEnv();
        //cout << "optimize 0" << endl;
        GRBModel model = GRBModel(env);

        model.getEnv().set(GRB_IntParam_OutputFlag, 0);
        //model.getEnv().set(GRB_INT_PAR_DUALREDUCTIONS, 0);

        for(int i=0;i<n;++i)vars[i] = model.addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS,to_string(i));

        //cout << "optimize 1" << endl;
        GRBQuadExpr obj = 0;

        int mm = M.n_rows;
        for(int i=0;i<mm;++i)for(int j=0;j<mm;++j){

            obj += vars[i] * vars[j] * M(i,j);

        }
        // cout << "optimize 2 " <<codeer<< endl;
        //model.setObjective(obj, GRB_MINIMIZE);
        model.set(GRB_IntParam_DualReductions, 0);
        model.setObjective(obj, GRB_MAXIMIZE);
        //cout<<"model.set(GRB_INT_PAR_DUALREDUCTIONS,0): "<<model.get(GRB_IntA)<<endl;


        int ec = 0, iec = 0;
        for(int i=0;i<Ab.size();++i){
            auto &a = Ab[i];
            GRBLinExpr cons = 0;
            for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]] * a.ws[i];
            model.addConstr(cons <= a.a);
            //cout<<a.ts.size()<<endl;
            if(a.mt == CM_EQUAL){model.addConstr(cons == a.a);ec++;}
            else if(a.mt == CM_LESS){model.addConstr(cons <= a.b);iec++;}
            else if(a.mt == CM_GREATER){model.addConstr(cons >= a.a);iec++;}
            else if(a.mt == CM_RANGE){model.addConstr(cons >= a.a);model.addConstr(cons <= a.b);iec+=2;}
        }
        cout<<"equalc & inequalc: "<<ec<<' '<<iec<<endl;
        for(int i=0;i<Cb.size();++i){
            QuadraticVec &a = Cb[i];
            GRBQuadExpr cons = 0;
            for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i].i] * vars[a.ts[i].j] * a.ts[i].val;
            if(a.mt == CM_EQUAL){model.addQConstr(cons == a.b);ec++;}
            else if(a.mt == CM_LESS){model.addQConstr(cons <= a.b);iec++;}
            else if(a.mt == CM_GREATER){model.addQConstr(cons >= a.b);iec++;}
        }
        cout<<"equalc & inequalc: "<<ec<<' '<<iec<<endl;



        cout<<"Begin Solve"<<endl;
        re = solveQuadraticProgramming_Core(model, vars, sol);

        if(0){
            for(int i=0;i<Ab.size();++i){
                auto &a = Ab[i];
                double cons = 0;
                for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]].get(GRB_DoubleAttr_X) * a.ws[i];
                cout<<cons<<endl;
            }
        }



    }catch (GRBException e) {
        cout << "Error code = " << e.getErrorCode() << endl;
        cout << e.getMessage() << endl;
    } catch (...) {
        cout << "Exception during optimization" << endl;
    }

    return re;


}


//int Solver::solveQP_multiupdata_main( Solution_Struct &sol, vector<LinearVec> &Ab, int n, bool suppressinfo){


//    vector<GRBVar>vars(n);
//    int re;

//    sol.init(n);

//    try{


//        //cout << "optimize 0" << endl;
//        GRBModel model(objmodel);

//        //cout << "optimize 1" << endl;
//        GRBVar *pVar = model.getVars();
//        for(int i=0;i<n;++i){
//            vars[i] = pVar[i];
//        }

//        //model.update();

//        //cout<<"Begin Cons"<<endl;

//        int ec = 0, iec = 0;
//        for(int i=0;i<Ab.size();++i){
//            auto &a = Ab[i];
//            GRBLinExpr cons = 0;
//            for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]] * a.ws[i];
//            //cout<<a.ts.size()<<endl;
//            if(a.a == a.b){model.addConstr(cons == a.b);ec++;}
//            else {model.addConstr(cons >= a.a);model.addConstr(cons <= a.b);iec+=2;}

//        }
//        //cout<<ec<<' '<<iec<<endl;


//        //cout<<"Begin Solve"<<endl;
//        re = solveQuadraticProgramming_Core(model, vars, sol, suppressinfo);

//        if(0){
//            for(int i=0;i<Ab.size();++i){
//                auto &a = Ab[i];
//                double cons = 0;
//                for(int i=0;i<a.ts.size();++i)cons += vars[a.ts[i]].get(GRB_DoubleAttr_X) * a.ws[i];
//                cout<<cons<<endl;
//            }
//        }



//    }catch (GRBException e) {
//        cout << "Error code = " << e.getErrorCode() << endl;
//        cout << e.getMessage() << endl;
//    } catch (...) {
//        cout << "Exception during optimization" << endl;
//    }

//    return re;



//}


//int Solver::solveQP_multiupdata_init(arma::mat &M, int n){


//    c_vars.resize(n);
//    for(int i=0;i<n;++i)c_vars[i] = objmodel.addVar(-GRB_INFINITY, GRB_INFINITY, 0, GRB_CONTINUOUS,to_string(i));

//    //cout << "optimize 1" << endl;
//    GRBQuadExpr obj = 0;

//    int mm = M.n_rows;
//    for(int i=0;i<mm;++i)for(int j=0;j<mm;++j){

//        obj += c_vars[i] * c_vars[j] * M(i,j);

//    }
//    // cout << "optimize 2 " <<codeer<< endl;
//    objmodel.setObjective(obj, GRB_MINIMIZE);

//    objmodel.update();
//    cout<<"solveQP_multiupdata_init"<<endl;





//}

The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API