Dec 24, 2013 at 2:05pm UTC
I am writing a program wich read a file of more 3000 lines and it takes many times to read all the file. I'd to know if there is a way to read such file more quickly. This is my code :
//Bibliotheque CPLEX pour C++
#include <ilcplex/ilocplex.h>
#include <ilcplex/ilocplexi.h>
//traitement de fichiers de donnees
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
ILOSTLBEGIN
typedef IloArray<IloNumVarArray> NumVarMatrix;
typedef IloArray<IloNumArray> NumMatrix;
float distance2(double x1,double y1,double x2,double y2){
return pow(x1-x2,2) + pow(y1-y2,2);
}
void creerModel(const IloInt nbreNoeud, NumVarMatrix x, IloModel mod){
IloEnv env = mod.getEnv();
cout << "FONCTION creerModdel" << endl << endl;
int u, v, i, j;
float dij, ci;
string ligne;
//fonction objective
IloObjective cost = IloMinimize(env);
////////////////////////////////////////////////////////////////////////////////////////////
const char* fichierEntree;
fichierEntree = "pcb3038.tsp";
ifstream data(fichierEntree, ios::in);
if(!data){
cerr << "impossible d'ouvrir le fichier" << fichierEntree << endl;
throw(1);
}
float x1, y1, xc, yc;
int num, numc, dim;
char sdim[20], sdim2pt[2];
string name, comment, type, dimension, e_w_t, n_c_s;
getline(data, name);
getline(data, comment);
getline(data, type);
getline(data, dimension);
sscanf(dimension.c_str(), "%s %s %i", sdim, sdim2pt, &dim);
cout << dimension << endl;
getline(data, e_w_t);
getline(data, n_c_s);
cout << dim << endl;
int poscourant;
u = 0;
cout << "hello" << endl;
while(getline(data, ligne)){
//prendre la position courante
poscourant = data.tellg();
//lire la position courante
sscanf(ligne.c_str(), "%i %f %f", &numc, &xc, &yc);
//parcours de tous les suivants de courant
v = u + 1;
while(std::getline(data, ligne)){
sscanf(ligne.c_str(), "%i %f %f", &num, &x1, &y1);
cost.setLinearCoef(x[u][v], floor(sqrt(distance2(x1,y1,xc,yc))));
cost.setLinearCoef(x[v][u], floor(sqrt(distance2(x1,y1,xc,yc))));
v++;
cout << v << endl;
if(num==dim)
break;
}
cout << u << endl;
u++;
data.seekg(poscourant);
}
cout << "bon" << endl;
data.close();
////////////////////////////////////////////////////////////////////////////////////////////
char s1[20], s2[2];
try{
float max;
max = 0.0;
//Fonction objective coef de x(u,v) est duv
IloNumArray t(env);
for(u=0;u<nbreNoeud;u++){
for(v=u+1;v<nbreNoeud;v++){
getline(data, ligne);
sscanf(ligne.c_str(), "%i %i %f", &i, &j, &dij);
cost.setLinearCoef(x[u][v], floor(sqrt(dij)));
cost.setLinearCoef(x[v][u], floor(sqrt(dij)));
}
max = rand() % ((5000 - 1000) + 1000) + 1000;
cost.setLinearCoef(x[u][u], max);
max = 0.0;
}
cout << " creer modèle fonction objective " << endl << endl;
mod.add(cost);
//contrainte 1
for(u=0;u<nbreNoeud;u++){
IloExpr expres(env);
for(v=0;v<nbreNoeud;v++){
if(u!=v){
expres += x[u][v];
}
}
mod.add(expres + x[u][u] == 1.0);
}
cout << " creer modèle contrainte 1 " << endl << endl;
//contrainte 2
for(u=0;u<nbreNoeud;u++){
for(v=0;v<nbreNoeud;v++){
if(u!=v){
mod.add(x[u][v] <= x[v][v]);
}
}
}
cout << " creer modèle contrainte 2 " << endl << endl;
}
catch (const char* msg){
cerr << msg;
}
}
int main(){
//variable temps
time_t tbegin,tend;
double texec=0;
//ouverture du fichier
const char* fichierDonnee;
fichierDonnee = "pcb3038.tsp";
IloInt nbreNoeud = 3038;
IloInt i,u,v;
IloEnv env;
try{
cout << "Modele declare" << endl << endl;
IloModel modell(env);
NumVarMatrix varx(env, nbreNoeud);
char bufferx[100];
int poschar = 0;
for(i=0;i<nbreNoeud;i++){
varx[i] = IloNumVarArray(env, nbreNoeud, 0.0, IloInfinity,ILOFLOAT);
}
for(u=0;u<nbreNoeud;u++){
for(v=0;v<nbreNoeud;v++){
poschar = sprintf(bufferx,"X(%i,%i)", (int)u+1, (int)v+1);
varx[u][v].setName(bufferx);
}
}
creerModel(nbreNoeud, varx, modell);
cout << " Modele creer" << endl ;
IloCplex cplex(modell);
cplex.exportModel("projet.lp");
//Desactiver le LP Presolve : cest un optimiseur de code
cplex.setParam(IloCplex::PreInd, false);
cplex.setParam(IloCplex::TiLim, 3600);
//resolution
tbegin = time(NULL);
if(!cplex.solve()){
env.error() << " Echec dans la resolution " << endl;
throw(-1);
}
tend = time(NULL);
IloNumArray vals(env);
NumMatrix valeursX(env, nbreNoeud);
for(u=0;u<nbreNoeud;u++)
valeursX[u] = IloNumArray(env, nbreNoeud);
//on transfere la solution dans vals
for(u=0;u<nbreNoeud;u++)
{
cplex.getValues(vals,varx[u]);
for(v=0;v<nbreNoeud;v++)
valeursX[u][v] = vals[v];
}
texec = difftime(tend,tbegin);
ofstream fichier("resultat.txt", ios::out | ios::trunc); //déclaration du flux et ouverture du fichier
fichier << "time : " << texec << endl;
for(u=0;u<nbreNoeud;u++)
{
//for(v=0;v<nbreNoeud;v++)
//{
fichier << valeursX[u][u] << " " << u << endl;
//}
}
env.out() << cplex.getObjValue() << endl;
fichier.close();
}
catch (const char* msg){
cerr << msg;
}
return 0;
}