2015-07-01 102 views
0

我正在學習如何將CPlex與C++一起使用,因此,我的問題可能看起來很愚蠢。以下算法必須訪問包含足球運動員數據的SQlite數據庫,獲取其內容,然後使用CPlex根據幻想遊戲中的平均積分及其成本確定最佳陣容。CPlex和C++錯誤:沒有匹配函數調用'IloModel :: add(bool)'

下面的代碼:

#include <iostream> 
#include <stdio.h> 
#include <cstdlib> 
#include <vector> 
#include <iomanip> 
#include <sqlite3.h> 
#include <string.h> 
#include <ilcplex/ilocplex.h> 

#define TECNICO 0 
#define GOLEIRO 1 
#define ZAGUEIRO 2 
#define LATERAL 3 
#define MEIA 4 
#define ATACANTE 5 
#define NOME 0 
#define TIME 1 
#define POSICAO 2 
#define SITUACAO 3 
#define JOGOS 4 
#define PRECO 5 
#define PRECO_MEDIO 6 
#define PONTUACAO 7 
#define PONTUACAO_MEDIA 8 
#define FALTAS_SOFRIDAS 9 
#define ASSISTENCIAS 10 
#define FINALIZACAO_TRAVE 11 
#define FINALIZACAO_DEFENDIDA 12 
#define FINALIZACAO_FORA 13 
#define GOLS 14 
#define ROUBADA_BOLA 15 
#define SG 16 
#define DEFESA_DIFICIL 17 
#define DEFESA_PENALTI 18 
#define PASSE_ERRADO 19 
#define IMPEDIMENTO 20 
#define PENALTI_PERDIDO 21 
#define FALTA_COMETIDA 22 
#define GOL_CONTRA 23 
#define CARTAO_AMARELO 24 
#define CARTAO_VERMELHO 25 
#define GOL_SOFRIDO 26 
#define ID 27 
#define MAX 855 

using namespace std; 

struct Jogador{ 
    string nome; 
    int id; 
    vector<bool> posicao; 
    float pontuacao_media; 
    float custo; 
    bool provavel; 
    bool pertinencia; 
}; 

Jogador jogadores[MAX]; 

int contador = 0; 

int callback(void *, int, char **, char **); 

int main(){ 
    sqlite3 *db; 
    sqlite3_stmt *stmt; 
    char *err_msg; 

    int rc = sqlite3_open("jogadores.db", &db); 

    if(rc == SQLITE_OK){ 
     const char *sql = "SELECT * FROM jogador"; 
     rc = sqlite3_exec(db, sql, callback, 0, &err_msg); 
    } 
    else{ 
     cout << "falhou ao abrir o banco" << endl; 
    } 

    IloEnv env; 
    IloModel model(env); 
    IloCplex cplex(env); 

    cplex.setParam(IloCplex::Threads, 1); 

    float capacidade_cartoletas; 

    cout << "Insira a quantidade de cartoletas" << endl; 
    cin >> capacidade_cartoletas; 

    //IloNumVarArray itens = IloNumVarArray(env, MAX, 0, 1, ILOBOOL); 

    bool itens[MAX]; 

    IloExpr obj = IloExpr(env); 

    int max_posicao[] = {1, 1, 2, 2, 4, 2}; 
    int posicoes_usadas[6]; 
    for(int i = 0; i < MAX; i++){ 
     obj += jogadores[i].pontuacao_media*itens[i]; 
    } 

    IloExpr cartoletas_totais = IloExpr(env); 

    for(int i = 0; i < MAX; i++){ 
     cartoletas_totais += jogadores[i].custo*itens[i]; 
    } 

    for(int i = 0; i < MAX; i++){ 
     for(int j = 0; j < MAX; j++){ 
      posicoes_usadas[j] += jogadores[i].posicao[j]*itens[i]; 
     } 
    } 

    model.add(cartoletas_totais <= capacidade_cartoletas); 
    for(int i = 0; i < 6; i++){ 
     model.add(posicoes_usadas[i] <= max_posicao[i]); 
    } 

    model.add(IloMaximize(env,obj)); 

    cplex.extract(model); 

    cplex.solve(); 

    for(int i = 0; i < MAX; i++){ 
     if(cplex.getValue(itens[i])){ 
      cout << jogadores[i].nome << endl; 
     } 
    } 

} 

int callback(void *NotUsed, int argc, char **argv, char **azColName){ 
    NotUsed = 0; 
    vector<bool> aux; 
    aux.assign(6, 0); 
    if(!strcmp(argv[POSICAO], "Técnico")){ 
      aux[TECNICO] = 1; 
    } 
    else if(!strcmp(argv[POSICAO], "Goleiro")){ 
     aux[GOLEIRO] = 1; 
    } 
    else if(!strcmp(argv[POSICAO], "Zagueiro")){ 
     aux[ZAGUEIRO] = 1; 
    } 
    else if(!strcmp(argv[POSICAO], "Lateral")){ 
     aux[LATERAL] = 1; 
    } 
    else if(!strcmp(argv[POSICAO], "Meia")){ 
     aux[MEIA] = 1; 
    } 
    else if(!strcmp(argv[POSICAO], "Atacante")){ 
     aux[ATACANTE] = 1; 
    } 
    int id = atoi(argv[ID]); 
    jogadores[id].id = id; 
    jogadores[id].posicao = aux; 
    jogadores[id].pontuacao_media = atof(argv[PONTUACAO_MEDIA]); 
    jogadores[id].custo = atof(argv[PRECO]); 
    jogadores[id].nome = argv[NOME]; 

    jogadores[id].provavel = !strcmp(argv[SITUACAO], "Provável"); 
    contador++; 
    return 0; 
} 

我用G ++編譯它,但它給出了一個錯誤:

cartola.cpp:116:55: error: no matching function for call to ‘IloModel::add(bool)’ 
    model.add(posicoes_usadas[i] <= max_posicao[i]); 

另一件事:我沒有與CPLEX了豐富的經驗,這些代碼被基於一個更簡單的問題,我的老師通過C++使用CPlex解決了問題

回答

0

您不是使用CPLEX建模變量建模問題。對初學者來說,這是一個非常普遍的錯誤。您需要考慮您知道哪些數據(例如您的數據庫表中的數據)來描述您的問題,以及您希望解決方案(例如CPLEX)制定的決策。然後,使用正常的C++(或C#或Java或其他)變量來處理您所知道的數據,並使用建模變量類型(IloNumVar等)和約束對這些決策(尚未知的值)進行建模。然後,將這些約束添加到CPLEX模型,並讓CPLEX解決模型併爲您的建模變量選擇最佳值。

相關問題