0

I'm trying to run an ampl .run (or any ampl code) file from Laravel using Symfony Process. My code is as below:

$commandArray = array('./ampl');

$process = new Process($commandArray);

        $process->setWorkingDirectory('/usr/local/bin/amplitude/amplide.linux64');
        $process->run();

        if (!$process->isSuccessful()) 
        {
            throw new ProcessFailedException($process);
        }

        dd($process->getOutput());

But I cannot start ampl. I get an error like:

""" The command "'./ampl'" failed.\n \n Exit Code: 2(Misuse of shell builtins)\n \n Working directory: /usr/local/bin/amplitude/amplide.linux64\n \n Output:\n ================\n \n \n Error Output:\n ================\n """

I suspected this was a permissions error in the directory but when I use:

$commandArray = array('ls');

it works and outputs the list of files and folders. I understand that ampl is basically a terminal program, so how do I access and write commands to it?

If someone can explain how to access terminal programs from Process, I think it would be very helpful. Thank you in advance.

toing_toing
  • 2,334
  • 1
  • 37
  • 79

1 Answers1

0

I figured out the issue here was that just calling the ampl console by typing ./ampl does not trigger any response. As it cannot be interpreted as a actual command, Laravel gives an error. The trick is to pass an actual command to the process object and to always give the full path to any .run/.mod/ .dat file you are reffering. For example:

$commandArray = array('./ampl path/to/example.run;');

or

$command = './ampl path/to/example.run';

works fine and will give the response.

Another important thing I noticed was that, since AMPL is basically another program running in the terminal, we cannot pass different commands seperately to the process using arrays like the following:

$commandArray = array('./ampl model.mod;','./ampl data.dat;', './ampl solve;');

This will not work. Neither will this:

$commandArray = array('./ampl, model.mod; data.dat; solve;');

Ideally it is best to have everything in a .run file and then execute it.

If you need to pass parameters to the .dat file from Laravel, passing this into the commands using string concatnation causes issues, although I do not exactly know why. I would suggest to use the Storage class in Laravel to update the .dat file first and then run the ampl problem using a .run file.

toing_toing
  • 2,334
  • 1
  • 37
  • 79