2016-12-14 71 views
0

最近我们一直在使用Berkley Pacman AI课程。我们必须分析alpha,epsilon和gamma对于AI的变化值。使用C#在cmd中运行命令系列

要插入命令直接进入CMD我们告诉CMD:

pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 

现在,我想运行一系列的,我们改变这些给定变量的值测试。所以我想打开CMD,运行很多命令(本质上是相同的)并将输出保存在文本文件中。我发现了一些信息StackExchange,它给了我下面的代码:Altough它开辟了CMD

string command = "pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7"; 
Process.Start("CMD.exe", command); 

,似乎没有做任何事情。 CMD的目录也是我解决方案的目录。 这应该是相当容易的(Altows Windows API可以很难使用)

任何人都可以帮助,或给我一个通用的解决方案吗?

+1

http://stackoverflow.com/questions/1469764/run-command-prompt-commands? – FakeCaleb

+0

等一下,你的问题是什么?你说了一个问题,但没有说明你想要做什么。 – Frecklefoot

+1

首先,你并不需要运行cmd.exe,你可以直接运行'pacman.py'。要设置工作目录,请查看[ProcessStartInfo](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v = vs.110).aspx)类。有[Process.Start](https://msdn.microsoft.com/en-us/library/0w4h05yb(v = vs.110).aspx)的重载,它接受一个。 –

回答

1

尝试ProcessStartInfo类。 MSDN ProcessStartInfo

因为这是一个AI课程。我可能会做一个PacmanArgument类。 PacmanArgument将具有每个可能的命令行参数的属性以及要调用的自定义方法ToString。假设输出可以看作适应度,那么编程式地生成诸如遗传算法之类的参数会更容易。

主要功能:

double MAX_EPSILON = 1; //I assume there are constraints 
//create packman agent with initial values 
PacmanAgent agent = new PackmanAgent(0,0,0) //epsilon,alpha,gamma 
//create Process info 
ProcessStartInfo psi = new ProcessStartInfo(); 
psi.FileName = "pacman.py" 
psi.WorkingDirectory = @"C:/FilePathToPacman" 
psi.RedirectStandardOutput = true; 
psi.UseShellExecute = false; 

string output; 
Console.WriteLine("***** Increasing Eplison Test *****"); 
while(agent.Epsilon =< MAX_EPSILON) 
{ 
    psi.Arguments = agent.GetArgument(); 
    // Start the process with the info we specified. 
    // Call WaitForExit and then the using-statement will close. 
    using (Process process = Process.Start(psi)) 
    { 
     output = process.StandardOutput.ReadToEnd(); //pipe output to c# variable 
     process.WaitForExit(); //wait for pacman to end 
    } 

    //Do something with test output 
    Console.WriteLine("Epsilon: {0}, Alpha: {1}, Gamma: {2}",agent.Epsilon,agent.Alpha,agent.Gamma); 
    Console.WriteLine("\t" + output); 

    agent.IncrementEpsilon(0.05); //increment by desired value or default(set in IncrementEpsilon function) 
} 

吃豆子代理类:

public class PacmanAgent 
{ 
    private string ArgumentBase = "-q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a "; 
    [Range(0, 1, ErrorMessage = "Value for {0} must be between {1} and {2}.")] 
    public double Epsilon { get; set; } 
    [Range(0, 1, ErrorMessage = "Value for {0} must be between {1} and {2}.")] 
    public double Alpha { get; set; } 
    [Range(0, 1, ErrorMessage = "Value for {0} must be between {1} and {2}.")] 
    public double Gamma { get; set; } 

    public PacmanAgent(int epsilon, int alpha , int gamma) 
    { 
     Epsilon = epsilon; 
     Alpha = alpha; 
     Gamma = gamma; 
    } 

    public string GetArgument() 
    { 
     string argument = string.Format("{0} epsilon={1}, alpha={2}, gamma={3}", ArgumentBase, Epsilon, Alpha, Gamma) 
     return argument 
    } 

    public void IncrementEpsilon(double i = 0.01) 
    { 
     Epsilon += i; 
    } 
    public void IncrementAlpha(double i = 0.01) 
    { 
     Alpha += i; 
    } 
    public void IncrementGamma(double i = 0.01) 
    { 
     Gamma += i; 
    } 
} 

*我写了一个IDE此之外,所以请原谅任何语法错误

+0

这非常了不起!我今天没有时间去研究它,但是我看到的很有意义。我无法自己做这件事,因为我从未与C#管理其他进程一起工作过。 –

+0

现在文件路径出现问题。试图弄清楚。你也忘了添加'psi.UseShellExecute = false;'但现在解决了。 –

+0

@RichardDirven这是否最终为你工作?我最近只使用过程很少,并没有任何设置来测试此代码,所以我很好奇。 – JaredStroeb

0

你可以首先是建立一个包含所有pacman.py命令的临时批处理文件。使用标准方法在C#中创建文件。使用标准的CMD.EXE管道功能追加到文件(下面的myfile.txt)。

您的临时.BAT文件将是这样的:

cd \mydirectory 
pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 >> myfile.txt 
pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 >> myfile.txt 
pacman.py -q -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.08,alpha=0.3,gamma=0.7 >> myfile.txt 

然后以同样的方式对你已经使用的方法运行它:

var command = "mytemp.bat" 
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); 
processInfo.CreateNoWindow = true; 
processInfo.UseShellExecute = false; 
process = Process.Start(processInfo); 
process.WaitForExit(); 

您也可以处理重定向它的标准输出并在C#中将其读入C#中的字符串变量,如果您不想在批处理文件中进行管道操作。

+0

那么我的程序也将不得不临时更改bat文件几次? –

+0

我会设想随时随地创建它,执行它,然后删除它。所以你打开一个流到一个名为'myfile.bat'的文本文件,抽取上面的CMD.EXE命令,关闭它,然后用Process.Start()运行它, –