Exemplo n.º 1
0
//--------------------------------------------
void read_solution(urdme_model *model, char*file){
	mxArray *tspan,*U;
	MATFile *input_file;
	/* Open mat-file (read-only) */
	input_file = matOpen(file,"r"); 
	if (input_file == NULL){
		printf("Failed to open mat-file '%s'.\n",file);
		return;	
	}
    printf("read in '%s'\n'",file);
	
	/* Get U-matrix. */
    init_sol(model,1);
    U = matGetVariable(input_file, "U");
	if (U == NULL){
        printf("No U solution variable in mat-file '%s'\n.",file);
        return;
    }
    int Usz = mxGetNumberOfElements(U);
	model->U[0] =(int *)malloc(Usz*sizeof(int));
    double*Utmp = mxGetPr(U);
	model->nsol = 1;
    int i;
    for(i=0;i<Usz;i++){
        model->U[0][i] = (int) Utmp[i];
    }

	/* time span (optional) */
	tspan = matGetVariable(input_file, "tspan");
	if (tspan != NULL){
        model->tspan = mxGetPr(tspan);
        model->tlen = mxGetNumberOfElements(tspan);
	}
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{
	int seed,i;

	//cout<<"c This is NuMVC, a local search solver for the Minimum Vertex Cover (and also Maximum Independent Set) problem."<<endl;
	
	if(build_instance(argv[1])!=1){
		cout<<"can't open instance file"<<endl;
		return -1;
	}
		optimal_size=0;
		i=2;
		//sscanf(argv[i++],"%d",&cand_count);//if you want to stop the algorithm only cutoff time is reached, set optimal_size to 0.
		//sscanf(argv[i++],"%d",&edge_cand);
		sscanf(argv[i++],"%d",&seed);
		sscanf(argv[i++],"%d",&cutoff_time);

	
		srand(seed);

		//cout<<seed<<' ';
		//cout<<argv[1]<<' ';
		
		times(&start);
		start_time = start.tms_utime + start.tms_stime;

    	init_sol();

#ifdef individual_analysis_on_init_sls_mode
		times(&finish);
		init_time = double(finish.tms_utime - start.tms_utime + finish.tms_stime - start.tms_stime) / sysconf(_SC_CLK_TCK);
		init_time = round(init_time * 100)/100.0;
#endif

		//if(c_size + uncov_stack_fill_pointer > optimal_size ) 
		//{
			//cout<<"c Start local search..."<<endl;
			cover_LS();
		//}
#ifdef individual_analysis_on_init_sls_mode
		times(&finish);
		sls_time = double(finish.tms_utime - start.tms_utime + finish.tms_stime - start.tms_stime) / sysconf(_SC_CLK_TCK) - init_time;
		sls_step_speed_per_ms = double(step) * 0.001 / sls_time;
#endif			
		//check solution
		if(check_solution()==1)
		{
			cout << "o " << best_c_size << endl;
			//cout << best_c_size << ' ';

			//print_mvc_solution();
			cout << "c searchSteps " << best_step << endl;
			//printf("%ld ", best_step);
			cout << "c solveTime " << best_comp_time << endl;
			//cout << "c stepVelocity(/0.001ms) " << (long double)(best_step) / (best_comp_time * 1000000) << endl;
			/*cout<<"c Best found vertex cover size = "<<best_c_size<<endl;
			print_solution();
			cout<<"c searchSteps = "<<best_step<<endl;
			cout<<"c solveTime = "<<best_comp_time<<endl;*/
			
			//cout<<best_c_size<<' '<<best_comp_time<<' '<<best_step<<endl;
#ifdef 	individual_analysis_on_init_sls_mode
		//cout<<"c initTime " << init_time << endl;
		//cout<<"c slsTime " << sls_time << endl;
		cout<<"c stepSpeed(/ms) "<< sls_step_speed_per_ms << endl;
#endif
		}
		else
		{
			cout<<"the solution is wrong."<<endl;
			//print_solution();
		}
	
		free_memory();

	return 0;
}
Exemplo n.º 3
0
Arquivo: dfsp.c Projeto: Aratz/pyurdme
int main(int argc, char *argv[])
{
    char *infile,*outfile;
	int i, nt=1, report_level=1;
	
	/* TODO. Parsing of input with error checking. 
	   Input syntax on form: -numtraj=4 etc. ?*/
	
	if (argc < 3){
		printf("To few arguments to nsm.");
	    exit(-1);	
	}
	
	/* Input file. */
	infile  = argv[1];
	
	/* Output file (or directory). */
	outfile = argv[2]; 
	
	/* Read model specification */
	urdme_model *model;
	model = read_model(infile);
	model->infile = infile;
	
	if (model == NULL){
		printf("Fatal error. Couldn't load model file or currupt model file.");
		return(-1);
	}
	
    /*reopen MAT file*/
    MATFile *input_file;
    mxArray *temp,*sopts;

    input_file = matOpen(infile,"r");
    if (input_file == NULL){
        printf("Failed to open mat-file.\n");
        return(-1);   
    }

	/* Initialize RNG(s).  */
    /* Look for seed */
    temp = matGetVariable(input_file, "seed");

    /* Initialize rng from GSL. */
    const gsl_rng_type * T;
    T = gsl_rng_taus2;
    gsl_rng_env_setup();
    runif = gsl_rng_alloc (T);
    //gsl_rng_set (runif, 463728648);  //Why would we seed the name number each time??

    long int seed;
    /* If seed is provided as a parameter, it takes precedence. */
    if (argc > 3) {
        srand48((long int)atoi(argv[3]));
        gsl_rng_set(runif,(long int)atoi(argv[3]));
    }else if(temp != NULL){
        seed = (long int) mxGetScalar(temp);
        srand48(seed);
        gsl_rng_set(runif,seed);
    }else{
		/* Not a foolproof solution */
        //srand48((long int)time(NULL)+(long int)(1e9*clock()));
        srand48( time(NULL) * getpid() );
        gsl_rng_set(runif,time(NULL) * getpid());
    }
	
	/* Look for an optional parameter matrix. */
	const double *matfile_parameters; 
	int mpar = 0;
	int npar = 0;
	int param_case=0;
	
    temp = matGetVariable(input_file, "parameters");
	if (temp != NULL) {
		matfile_parameters = (double *)mxGetPr(temp);
		mpar = mxGetM(temp);
		npar = mxGetN(temp); 
	}
	
	/* Look if a parameter case if supplied as a parameter. */
	if (argc > 4) {
	    param_case = (int)atoi(argv[4]);
	}
	
	if (param_case > npar ) {
		printf("nsmcore: Fatal error, parameter case is larger than n-dimension in parameter matrix.\n");
		exit(-1);
	}
	
	/* Create global parameter variable for this parameter case. */
	parameters = (double *)malloc(mpar*sizeof(double));
	memcpy(parameters,&matfile_parameters[npar*param_case],mpar*sizeof(double));
	
    /* Initialize extra args */
	model->num_extra_args = 4;
	model->extra_args=malloc(model->num_extra_args*sizeof(void *));
	
	/* Set report level */
    temp = matGetVariable(input_file, "report");
   
    if (temp != NULL)
        report_level = (int) mxGetScalar(temp);
    else
        if (nt > 1)
            report_level=0;
        else
            report_level=1;

	model->extra_args[0] = malloc(sizeof(int));
	*(int *)(model->extra_args[0]) = report_level;

    /* Set tau_d */
    sopts = matGetVariable(input_file, "sopts");
    if(sopts==NULL){
        printf("Step length (tau_d) is missing in the model file\n");
        return(-1);   
    }
    model->extra_args[1] = malloc(sizeof(double));
    model->extra_args[2] = malloc(sizeof(double));
    model->extra_args[3] = malloc(sizeof(double));
    double*sopts_tmp = mxGetPr(sopts);
    //printf("HERE: tau_d=%e\n",sopts_tmp[0]);
    //printf("HERE: max_jump=%e\n",sopts_tmp[1]);
    //printf("HERE: err_tol=%e\n",sopts_tmp[2]);
    *(double*)model->extra_args[1] = sopts_tmp[0];
    *(double*)model->extra_args[2] = sopts_tmp[1];
    *(double*)model->extra_args[3] = sopts_tmp[2];
    //printf("HERE: tau_d=%e\n",*(double *)(model->extra_args[1]));
    //printf("HERE: max_jump=%e\n",*(double *)(model->extra_args[2]));
    //printf("HERE: err_tol=%e\n",*(double *)(model->extra_args[3]));
    /* close MAT file*/
    matClose(input_file);

	/* Allocate memory to hold nt solutions. */
	init_sol(model,nt);

    /* Get a writer to store the output trajectory on a hdf5 file. */
    urdme_output_writer *writer;
    writer = get_urdme_output_writer(model,outfile);
    printf("writer = get_urdme_output_writer(model,%s);\n",outfile);
	
    printf("dfsp_core\n");
    dfsp_core(model, writer);
	
    /* Write the timespan vector to the output file */
    printf("write_tspan\n");
    write_tspan(writer,model);

    
    //free(parameters);
    
    printf("destroy_output_writer\n");
    destroy_output_writer(writer);
    printf("destroy_model\n");
    destroy_model(model);
	
	return(0);
	
}