0

我正在尝试使用 minisat 调用程序时尝试一些随机参数system()。我以前从来没有做过这样的事情,不得不承认我很迷茫。

例如我可以这样做:

system("minisat -luby -rinc=1.5 <dataset here>")

如何将其随机化为-lubyor-no-luby并将1.5值随机化-rinc

4

3 回答 3

1

system 只是一个接收 c 风格字符串作为参数的普通函数。您可以自己构造字符串。

bool luby = true;
double rinc = 1.5;
system((std::string("minisat -")+(luby?"luby":"no-luby")+" -rinc="+std::to_string(rinc)).c_str());
于 2016-09-03T01:35:51.810 回答
0

在这里,您可以尝试使用这样的随机字符串命令生成器来创建随机命令:

#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());
于 2016-09-03T01:40:53.410 回答
0

您需要使用变量动态构造命令。

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());
于 2016-09-03T01:21:56.163 回答