windows+VS2019+gurobi解決方案配置


第一步,VS創建新的解決方案和空項目

 

 一個解決方案下面可以包含多個項目。

 

右鍵點擊項目名,選擇屬性,進行配置

  • 添加附加包含目錄
C:\gurobi911\win64\include

  • 添加附加庫目錄
C:\gurobi911\win64\lib

  • 添加附加依賴項

 

 

gurobi91.lib;gurobi_c++mdd2019.lib;

 

  • 如果有需要別的文件,如依靠外部lp文件,添加命令參數

 

  •  還有一點基本的,把平台Debug設置為x64

 

 

 

OK,現在就可以測試一下了

/* Copyright 2020, Gurobi Optimization, LLC */

/* This example creates a very simple Special Ordered Set (SOS) model.
   The model consists of 3 continuous variables, no linear constraints,
   and a pair of SOS constraints of type 1. */

#include "gurobi_c++.h"
using namespace std;

int
main(int   argc,
    char* argv[])
{
    GRBEnv* env = 0;
    GRBVar* x = 0;
    try {
        env = new GRBEnv();

        GRBModel model = GRBModel(*env);

        // Create variables

        double ub[] = { 1, 1, 2 };
        double obj[] = { -2, -1, -1 };
        string names[] = { "x0", "x1", "x2" };

        x = model.addVars(NULL, ub, obj, NULL, names, 3);

        // Add first SOS1: x0=0 or x1=0

        GRBVar sosv1[] = { x[0], x[1] };
        double soswt1[] = { 1, 2 };

        model.addSOS(sosv1, soswt1, 2, GRB_SOS_TYPE1);

        // Add second SOS1: x0=0 or x2=0 */

        GRBVar sosv2[] = { x[0], x[2] };
        double soswt2[] = { 1, 2 };

        model.addSOS(sosv2, soswt2, 2, GRB_SOS_TYPE1);

        // Optimize model

        model.optimize();

        for (int i = 0; i < 3; i++)
            cout << x[i].get(GRB_StringAttr_VarName) << " "
            << x[i].get(GRB_DoubleAttr_X) << endl;

        cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;

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

    delete[] x;
    delete env;
    return 0;
}
test.cpp

輸出為:

 

 完成!

grb過期解決方法

 

 

 

 需要把gurobi.lic文件從默認的位置拷貝到girobi的安裝目錄下面

 windows+gurobipy+vscode配置方法

1.查看系統中python的安裝路徑

 

 

 2.為vscode選擇一個路徑作為調試用的解釋器

設置里面搜索:default interpreter Path ,並賦值為 D:\Users\user\AppData\Local\Programs\Python\Python38\python.exe

 

 3.添加gurobipy拓展包

從gurobi安裝目錄中找到:

 

 將此文件拷貝到python解釋器對應的...\Lib目錄下:

D:\Users\user\AppData\Local\Programs\Python\Python38\Lib

 

 4.配置完成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM