std::pair<std::vector<double>, std::vector<double> > &
    operator()(const std::vector<double>  & real_decision_vars, const std::vector<int> & int_decision_vars, boost::filesystem::path save_path)
    {
        if (!boost::filesystem::exists(save_path)) boost::filesystem::create_directory(save_path);

        std::string filename = "logWorker" + std::to_string(evaluator_id) + "_EvalNo" + std::to_string(eval_count) + "_" + boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()) + ".log";
        boost::filesystem::path debug_log_file_name = save_dir / filename;
        std::ofstream logging_file;
        if (is_logging)
        {
            logging_file.open(debug_log_file_name.c_str(), std::ios_base::app);
            if (!logging_file.is_open())
            {
                is_logging = false;
                std::cout << "attempt to log failed\n";
            }
        }

        boost::filesystem::path objectives_file = save_path / "metrics_for_each_replicate.txt";
        std::ofstream objectives_fs(objectives_file.c_str());


        std::vector<double> & obj = objectives_and_constrataints.first;
        obj[0] = 0; obj[1] = 0;

        for (int j = 0; j < replicates; ++j)
        {
            std::pair<double, double> metric_vals = calcMetrics(worker_dir, real_decision_vars, int_decision_vars, is_logging, logging_file, debug_log_file_name, j);
            double fks =  metric_vals.first;
            double clump = metric_vals.second;
            objectives_fs << "replicate " << j << " fks: " << fks << " clumpiness: " << clump << "\n";
            obj[0] += fks;
            obj[1] += clump;
            boost::filesystem::path save_replicate_path = save_path / ("replicate_" + std::to_string(j));
//            if (!boost::filesystem::exists(save_replicate_path)) boost::filesystem::create_directory(save_replicate_path);
            if (boost::filesystem::exists(save_replicate_path)) boost::filesystem::remove_all(save_replicate_path);
            copyDir(worker_dir, save_replicate_path);
            boost::filesystem::path output_map = worker_dir / "Log" / "Land_use" / "Land use map_2000-Jan-01 00_00_00.rst";
            if (boost::filesystem::exists(output_map))
            {
                boost::filesystem::path png_path = save_path  / ("replicate_" + std::to_string(j) + ".png");
                blink::raster::gdal_raster<int> out_raster = blink::raster::open_gdal_raster<int>(output_map, GA_ReadOnly);
                blink::raster::printRaster2PNG(out_raster, colour_mapper, png_path);
            }

        }

        obj[0] /= replicates;
        obj[1] /= replicates;
        ++eval_count;

        if (is_logging) logging_file << "\n\n\n FKS: " << obj[0] << "\n Average Clump Diff: " << obj[1] << "\n";

        if (is_logging) logging_file.close();

//        boost::filesystem::remove_all(previous_log_file);
//        previous_log_file = debug_log_file_name;

        return (objectives_and_constrataints);




    }
 //Copies entire directory - so that each geoproject is running in a different directory.
 bool copyDir(
         boost::filesystem::path const & source,
         boost::filesystem::path const & destination
         )
 {
     namespace fs = boost::filesystem;
     try
     {
         // Check whether the function call is valid
         if(
                 !fs::exists(source) ||
                 !fs::is_directory(source)
                 )
         {
             std::cerr << "Source directory " << source.string()
                       << " does not exist or is not a directory." << '\n'
                          ;
             return false;
         }
         if(fs::exists(destination))
         {
             std::cerr << "Destination directory " << destination.string()
                       << " already exists." << '\n'
                          ;
             return false;
         }
         // Create the destination directory
         if(!fs::create_directory(destination))
         {
             std::cerr << "Unable to create destination directory"
                       << destination.string() << '\n'
                          ;
             return false;
         }
     }
     catch(fs::filesystem_error const & e)
     {
         std::cerr << e.what() << '\n';
         return false;
     }
     // Iterate through the source directory
     for(
         fs::directory_iterator file(source);
         file != fs::directory_iterator(); ++file
         )
     {
         try
         {
             fs::path current(file->path());
             if(fs::is_directory(current))
             {
                 // Found directory: Recursion
                 if(
                         !copyDir(
                             current,
                             destination / current.filename()
                             )
                         )
                 {
                     return false;
                 }
             }
             else
             {
                 // Found file: Copy
                 fs::copy_file(
                             current,
                             destination / current.filename()
                             );
             }
         }
         catch(fs::filesystem_error const & e)
         {
             std:: cerr << e.what() << '\n';
         }
     }
     return true;
 }
    MetronamicaOF2RandstadLargeCaseStudy3Obj(boost::filesystem::path & metro_exe,
                   boost::filesystem::path & mck_exe,
                   boost::filesystem::path & wine_exe,
                   boost::filesystem::path & java_exe,
                   boost::filesystem::path & geoproj_edit_jar,
                   boost::filesystem::path & template_path,
                   boost::filesystem::path & working_dir,
                                 boost::filesystem::path & save_dir,
                   std::string &_wine_work_dir,
                   std::string &_geoproj_name,
                   boost::filesystem::path & _logfile_name,
                   boost::filesystem::path & actual_map_path,
                   boost::filesystem::path & original_map_path,
                   boost::filesystem::path & masking_map_path,
                   boost::filesystem::path &  fks_coefficients_path,
                   boost::filesystem::path & wine_regedit_path,
                   int _id = 0,
                   bool _is_logging = false,
                   int _replicates = 10)
        :   num_objectives(3),
          num_real_decision_vars(238),
          num_int_decision_vars(0),
          num_constraints(0),
          replicates(_replicates),
          geo_cmd(metro_exe),
          java_geoproj_edit(geoproj_edit_jar),
          mck_cmd(mck_exe),
          wine_cmd(wine_exe),
          java_cmd(java_exe),
          template_dir(template_path),
          working_dir(working_dir),
          save_dir(save_dir),
          wine_temp_dir(_wine_work_dir),
          geoproject_name(_geoproj_name),
          metro_logfile_name(_logfile_name),
          actual_map(actual_map_path),
          original_map(original_map_path),
          masking_map(masking_map_path),
          fks_coefficients(fks_coefficients_path),
          wine_regedit(wine_regedit_path),
          int_lowerbounds(0, std::numeric_limits<int>::min()),
          int_upperbounds(0, std::numeric_limits<int>::max()),
          prob_defs(new ProblemDefinitions(min_dv_values, max_dv_values,  int_lowerbounds, int_upperbounds, minimise_or_maximise, num_constraints)),
          objectives_and_constrataints(std::piecewise_construct, std::make_tuple(num_objectives, std::numeric_limits<double>::max()), std::make_tuple(num_constraints)),
          evaluator_id(_id),
          is_logging(_is_logging),
          eval_count(0)
    {
        analysisNum = createAnalysis();
        loadMapActual(analysisNum, actual_map.c_str());
        loadOriginalMap(analysisNum, original_map.c_str());
        loadMaskingMap(analysisNum, masking_map.c_str());
        loadTransitionFuzzyWeights(analysisNum, fks_coefficients.c_str());
        numClasses(analysisNum, 9);

        std::string temp_dir_template = "Metro_Cal_OF_worker" + std::to_string(evaluator_id) + "_%%%%-%%%%";
        worker_dir = boost::filesystem::unique_path(working_dir / temp_dir_template);
        //            create_directories(ph);
        copyDir(template_dir, worker_dir);


        namespace fs = boost::filesystem;
        std::string metro_cp_log_name = "calibration_log_%%%%-%%%%.xml";
        boost::filesystem::path metro_cp_log_path = boost::filesystem::unique_path(worker_dir / metro_cp_log_name);
        fs::copy_file(
                    metro_logfile_name,
                    metro_cp_log_path
                    );
        cp_metro_log_name = metro_cp_log_path.filename().string();

        //        std::string filename = "logWorker" + std::to_string(evaluator_id) + "_WineRegEdit_" + boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()) + ".log";
        //        boost::filesystem::path log_file_name = working_dir / filename;
        //        std::ofstream logging_file;
        //        if (is_logging)
        //        {
        //            logging_file.open(log_file_name.c_str(), std::ios_base::app);
        //            if (!logging_file.is_open()) is_logging = false;
        //        }

        //        std::stringstream cmd;
        //        cmd << wine_cmd << " regedit " << wine_regedit_path;
        //        if (is_logging) cmd << " >> \"" << log_file_name.c_str() << "\" 2>&1";
        //        if (is_logging) logging_file << "Running: " << cmd.str() << "\n";
        //        system(cmd.str().c_str());

        colour_mapper.push_back(std::make_tuple(255,   255,   160));
        colour_mapper.push_back(std::make_tuple(255,   128,     0));
        colour_mapper.push_back(std::make_tuple(255,     0,     0));
        colour_mapper.push_back(std::make_tuple( 78,     0,   192));
        colour_mapper.push_back(std::make_tuple(158,    12,   234));
        colour_mapper.push_back(std::make_tuple(255,    87,   255));
        colour_mapper.push_back(std::make_tuple(  0,   192,     0));
        colour_mapper.push_back(std::make_tuple(192,   157,     0));
        colour_mapper.push_back(std::make_tuple(  0,     0,     0));
        colour_mapper.push_back(std::make_tuple(  0,   128,   255));

        //Make sure the number of dv is correct
        BOOST_ASSERT(max_dv_values.size() == 520);
    }
示例#4
0
文件: cpy.c 项目: lazopard/CS240
int main(void) {
				char *source = "userData";
				char *destination = "/home/adminuser/CS/CS240/project3/backupDir";
				copyDir(source,destination);
				//copyFile("backup.c",destination);
}
 foreach( QString dir, dirs )
 {
     if ( !copyDir( path + dir, dest + dir ) )
         return false;
 }
示例#6
0
int copyDir(char *src, char *dest)
{
	int sourceDev, destDev, n;
	char buf[BLKSIZE], tempName[256];
	char *cp;
	DIR *dp;
	STAT tempStatBuf, destStatBuf;

	// We have to recursively copy over a directory

	stat(dest, &destStatBuf);
	if(destStatBuf.st_ino != 0)
	{
		printf("Dest dir already exists!\n");
		return -1;
	}
	
	// We assume the destination directory doesn't exist
	// We create a directory
	printf("Making directory: %s\n", dest);
	mkdir(dest);

	// Open the src directory for read
	sourceDev = open(src, 0);
	
	n = read(sourceDev, buf, 1024);
	cp = buf;
	dp = (DIR *)buf;

	while(cp < (buf + BLKSIZE))
	{
		int pid, child, status;
		char srcDirEntryPath[BUF_SIZE], destDirEntryPath[BUF_SIZE];

		strcpy(tempName, dp->name);
		tempName[dp->name_len] = 0;

		if(strcmp(tempName, ".") == 0 || strcmp(tempName, "..") == 0)
		{
			cp += dp->rec_len;
			dp = (DIR *)cp;
			continue;
		}

		

		// Build the src path
		strcpy(srcDirEntryPath, src);
		strcat(srcDirEntryPath, "/");
		strcat(srcDirEntryPath, tempName);

		stat(srcDirEntryPath, &tempStatBuf);

		// Build the dest path
		strcpy(destDirEntryPath, dest);
		strcat(destDirEntryPath, "/");
		strcat(destDirEntryPath, tempName);



		if(tempStatBuf.st_mode == 0x41ED)
		{
			copyDir(srcDirEntryPath, destDirEntryPath);
		}
		else
		{
			copyFile(srcDirEntryPath, destDirEntryPath);
		}

		cp += dp->rec_len;
		dp = (DIR *)cp;
	}
}
void cloneFile(TFile *iOutputFile,TFile *iReadFile,std::string iSkipHist) {
  //copy all objects and subdirs of directory source as a subdir of the current directory   
  iOutputFile->cd();
  copyDir(iReadFile,iSkipHist);
}