
#include <stdio.h>
#include <stdlib.h>
#include "genetico.h"
#include <string.h>
#include <math.h>
#include </home_nfs/mgonzes/gurobi752/linux64/include/gurobi_c.h>
#include <time.h>


int eficiencia(int *empresas, int *eficientes, double **entrada, double **salida){


int i,j,k=0,optimstatus=0,M=10000,estado=0,eficaz=0,cantidad=0,contador=0, error=0;

/*
double **entrada,
       **salida;
*/
double objval;

double t1,t2;

char myprob[10] = "/tmp/eficiencia";
char myprob2[4];
char myprob3[4] = ".lp";
char myprob4[20];

FILE *fentradas, *fsalidas, *fsolucion, *fmodelo;

GRBenv   *masterenv = NULL;
GRBmodel *model     = NULL;
GRBenv   *modelenv  = NULL;


/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* ----------------------------------------- INICIALIZACION------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/


t1=time(0);

/*

fentradas=fopen("entradas.txt","r");
fsalidas=fopen("salidas.txt","r");

entrada = (double **)malloc((CANTIDAD_N+1)*sizeof(double*));
for (i=0;i<CANTIDAD_N+1;i++) {
		entrada[i] = (double*)malloc((CANTIDAD_M+1)*sizeof(double));
}

salida = (double **)malloc((CANTIDAD_N+1)*sizeof(double*));
for (i=0;i<CANTIDAD_N+1;i++) {
		salida[i] = (double*)malloc((CANTIDAD_S+1)*sizeof(double));
}

//empresas = (int*)malloc(CANTIDAD_N*sizeof(int));

//////////////////// GUARDAR DATOS //////////////////////////

for(i=1;i<CANTIDAD_M+1;i++){
    for(j=1;j<CANTIDAD_N+1;j++){
        fscanf(fentradas,"%lf",&entrada[j][i]);
    }
}
for(i=1;i<CANTIDAD_S+1;i++){
    for(j=1;j<CANTIDAD_N+1;j++){
        fscanf(fsalidas,"%lf",&salida[j][i]);
    }
}
fclose(fentradas);
fclose(fsalidas);
fflush(stdout);

*/


/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* ----------------------------------------- CALCULO DE EFICIENCIA ----------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/
/* --------------------------------------------------------------------------------------------------*/

fsolucion=fopen("eficientes.txt","a+");
fprintf(fsolucion,"\n\n ============ EFICIENTES ============= \n\n");
fclose(fsolucion);

for(k=0;k<CANTIDAD_N;k++){

//fmodelo=fopen("modelo.lp","w");
//myprob4[0]='\0';
//sprintf(myprob2,"%d",my_rank);
//strcat(myprob4,myprob);
//strcat(myprob4,myprob2);
//strcat(myprob4,myprob3);
fmodelo=fopen("/tmp/eficiencia.lp","w");

fflush(stdout);
/* CREAR MODELO A OPTIMIZAR */

fprintf(fmodelo,"Maximize\nfuncion: ");
fflush(stdout);

fprintf(fmodelo,"sn1");
for(i=2;i<CANTIDAD_M+1;i++){
    fprintf(fmodelo," + sn%d",i);
}
fflush(stdout);

for(i=1;i<CANTIDAD_S+1;i++){
    fprintf(fmodelo," + sp%d",i);
}
fflush(stdout);
/* CREAR RESTRICCIONES UTILIZADAS */

fprintf(fmodelo,"\nSubject To\n");

for(i=0;i<CANTIDAD_M;i++){
        fprintf(fmodelo,"sumatorio_entradas%d:",i+1);
        for(j=0;j<CANTIDAD_N;j++){
            fprintf(fmodelo," + %f d%d",entrada[j][i],j+1);
        }
       fprintf(fmodelo," + sn%d = %f\n",i+1,entrada[k][i]);
}
fflush(stdout);

for(i=0;i<CANTIDAD_S;i++){
        fprintf(fmodelo,"sumatorio_salidas%d:",i+1);
        for(j=0;j<CANTIDAD_N;j++){
            fprintf(fmodelo," + %f d%d",salida[j][i],j+1);
        }
       fprintf(fmodelo," - sp%d = %f\n",i+1,salida[k][i]);
}
fflush(stdout);

/*fprintf(fmodelo,"VRS: ");
//#pragma omp parallel for schedule (static) private (j)
for(i=1;i<CANTIDAD_N+1;i++){
    fprintf(fmodelo,"+d%d",i);
}
fprintf(fmodelo," = 1\n");*/
/* CREAR COTAS DE VARIABLES */

fprintf(fmodelo,"Bounds\n");

	for(i=1;i<CANTIDAD_N+1;i++){
        fprintf(fmodelo,"d%d >= 0\n",i);
	}
	for(i=1;i<CANTIDAD_M+1;i++){
        fprintf(fmodelo,"sn%d >= 0\n",i);
	}
	for(i=1;i<CANTIDAD_S+1;i++){
        fprintf(fmodelo,"sp%d >= 0\n",i);
	}
fprintf(fmodelo,"end");
fclose(fmodelo);
fflush(stdout);

fsolucion=fopen("eficientes.txt","a+");

	error = GRBloadenv(&masterenv, "lp.log");
		//if (error) goto QUIT;
	error = GRBreadmodel(masterenv, "/tmp/eficiencia.lp", &model);
		//if (error) goto QUIT;
	error = GRBoptimize(model);
		//if (error) goto QUIT;
	error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
		//if (error) goto QUIT;
	
	if (optimstatus == GRB_INF_OR_UNBD) 
	{
		modelenv = GRBgetenv(model);
		if (!modelenv) 
		{
		  fprintf(stderr, "Error: could not get model environment\n");
		  //goto QUIT;
		}
		error = GRBsetintparam(modelenv, "PRESOLVE", 0);
			//if (error) goto QUIT;
		error = GRBoptimize(model);
			//if (error) goto QUIT;
		error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
			//if (error) goto QUIT;
	}
	
	if (optimstatus == GRB_OPTIMAL) 
	{
		error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
			//if (error) goto QUIT;
		printf("Optimal objective: %.4e\n\n", objval);
	} 
	else if (optimstatus == GRB_INFEASIBLE) 
	{
		printf("Model is infeasible\n\n");
		error = GRBcomputeIIS(model);
			//if (error) goto QUIT;
		error = GRBwrite(model, "model.ilp");
			//if (error) goto QUIT;
	}
	else if (optimstatus == GRB_UNBOUNDED)
	{
		printf("Model is unbounded\n\n");
	} 
	else 
	{
		printf("Optimization was stopped with status = %d\n\n", optimstatus);
	}
	
	if (error) 
	{
    printf("ERROR: %s\n", GRBgeterrormsg(masterenv));
    //exit(1);
	}
	
	
if(fabs(objval) < 0.00000001)
    {
        eficaz=1;
        fprintf(fsolucion,"DMU%d= %.10lf\t|||| EFICIENTE = %d\n",k+1,objval, eficaz);
		eficientes[cantidad]=k;
        cantidad++;
    }
    else
        {
            fprintf(fsolucion,"DMU%d= %.10lf\t|||| EFICIENTE = %d\n",k+1,objval, eficaz);
            empresas[contador]=k;
            contador++;
        }

//fprintf(fsolucion,"DMU%d= %.10lf\t|||| EFICIENTE = %d\n",k,objval, eficaz);

eficaz=0;
fflush(stdout);


GRBfreemodel(model);
GRBfreeenv(masterenv);


fclose(fsolucion);

}


t2=time(0);

fsolucion=fopen("eficientes.txt","a+");
fprintf(fsolucion,"\n\nEficientes = %d",cantidad);
fprintf(fsolucion,"\n-----------Tiempo calculo eficiencia = %f--------------\n",t2-t1);
fclose(fsolucion);

return contador;

}
