2016-12-14 70 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()運行它, –