嘗試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此之外,所以請原諒任何語法錯誤
http://stackoverflow.com/questions/1469764/run-command-prompt-commands? – FakeCaleb
等一下,你的問題是什麼?你說了一個問題,但沒有說明你想要做什麼。 – Frecklefoot
首先,你並不需要運行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)的重載,它接受一個。 –