使用 System() 的随机参数?
Randomized Parameters Using System()?
我正在尝试使用 system()
调用程序时为 minisat 尝试一些随机参数。我以前从未做过这样的事情,不得不承认我很迷茫。
例如我可以这样做:
system("minisat -luby -rinc=1.5 <dataset here>")
如何将其随机化为 -luby
或 -no-luby
并将 1.5
值随机化为 -rinc
?
您需要使用变量动态构建命令。
bool luby = true; // if you want -no-luby, set it to be false
double rinc = 1.5; // set it to be other values
char command[1024];
std::string luby_str = (luby ? "luby" : "no-luby");
std::snprintf(command, sizeof(command), "minisat -%s -rinc=%f", luby_str.c_str(), rinc);
system(command);
正如@RemyLebeau 指出的那样,C++ 风格应该更好。
std::string command;
std::ostringstream os;
os << "minisat -" << luby_str << " -rinc=" << rinc;
system(command.c_str());
system 只是一个接收 c 风格字符串作为参数的普通函数。你可以自己构造字符串。
bool luby = true;
double rinc = 1.5;
system((std::string("minisat -")+(luby?"luby":"no-luby")+" -rinc="+std::to_string(rinc)).c_str());
在这里,您可以尝试使用像这样的随机字符串命令生成器来创建随机命令:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include <string>
std::string getCommand()
{
std::string result = "minisat ";
srand(time(0));
int lubyflag = rand() % 2; //Not the best way to generate random nums
//better to use something from <random>
if (lubyflag == 1)
{
result += "-luby ";
} else
{
result += "-no-luby ";
}
double lower_bound = 0; //Now were using <random>
double upper_bound = 2; //Or whatever range
std::uniform_real_distribution<double> unif(lower_bound,upper_bound);
std::default_random_engine re;
double rinc_double = unif(re);
result += "-rinc=" + rinc_double;
return result;
}
int main()
{
std::string command = getCommand();
system(command.c_str());
}
如果您想要所有控制权,请执行以下操作:
bool flaga = false;
double valueb = 1.5;
system(std::string("ministat " + ((flaga) ? "-luby " : "-no-luby ") +
"rinc= " + std::to_string(valueb)).c_str());
我正在尝试使用 system()
调用程序时为 minisat 尝试一些随机参数。我以前从未做过这样的事情,不得不承认我很迷茫。
例如我可以这样做:
system("minisat -luby -rinc=1.5 <dataset here>")
如何将其随机化为 -luby
或 -no-luby
并将 1.5
值随机化为 -rinc
?
您需要使用变量动态构建命令。
bool luby = true; // if you want -no-luby, set it to be false
double rinc = 1.5; // set it to be other values
char command[1024];
std::string luby_str = (luby ? "luby" : "no-luby");
std::snprintf(command, sizeof(command), "minisat -%s -rinc=%f", luby_str.c_str(), rinc);
system(command);
正如@RemyLebeau 指出的那样,C++ 风格应该更好。
std::string command;
std::ostringstream os;
os << "minisat -" << luby_str << " -rinc=" << rinc;
system(command.c_str());
system 只是一个接收 c 风格字符串作为参数的普通函数。你可以自己构造字符串。
bool luby = true;
double rinc = 1.5;
system((std::string("minisat -")+(luby?"luby":"no-luby")+" -rinc="+std::to_string(rinc)).c_str());
在这里,您可以尝试使用像这样的随机字符串命令生成器来创建随机命令:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include <string>
std::string getCommand()
{
std::string result = "minisat ";
srand(time(0));
int lubyflag = rand() % 2; //Not the best way to generate random nums
//better to use something from <random>
if (lubyflag == 1)
{
result += "-luby ";
} else
{
result += "-no-luby ";
}
double lower_bound = 0; //Now were using <random>
double upper_bound = 2; //Or whatever range
std::uniform_real_distribution<double> unif(lower_bound,upper_bound);
std::default_random_engine re;
double rinc_double = unif(re);
result += "-rinc=" + rinc_double;
return result;
}
int main()
{
std::string command = getCommand();
system(command.c_str());
}
如果您想要所有控制权,请执行以下操作:
bool flaga = false;
double valueb = 1.5;
system(std::string("ministat " + ((flaga) ? "-luby " : "-no-luby ") +
"rinc= " + std::to_string(valueb)).c_str());