I am solving an optimal control problem using the python GEKKO and/or APM Matlab interface(they are the same package for both languages). I can solve my problem successfully, but I cannot figure out how to specify the output location of the solution file.
After loading my files and solving my problem using
% server
s = 'http://byu.apmonitor.com';
% application name
a = 'test';
% load model and data file
apm_load(s,a,'test.apm')
csv_load(s,a,'test.csv')
output = apm(s,a,'solve');
a file called 'solution_test.csv' is created in the directory of the file i just ran. How can i change this output directory?
You can specify the run folder by changing m._path. If you leave it as the default temp directory then you can view that folder with print(m._path) or open the temp folder with m.open_folder(). Below is an example of specifying the run directory with a simple application:
from gekko import GEKKO
m = GEKKO()
m._path = r'C:\test'
x = m.Var()
m.Obj(x**2)
m.solve(disp=False)
print(x.value[0])
You'll need to make sure that you can write to this folder, in this case to C:\test. It will throw an error if the folder does not exist or you don't have permission to write to that folder. Also, if you open one of the files in that folder with a program that puts a lock on the file (such as Microsoft Excel) then you will receive an error if the file is open and locked for writing when you try to run the Python program again.
if i understand you correctly you want to move files from a dir to new dir. Try incorporating this into your code.
#dirpath = path of current file
#fname = file name
import os
import shutil
dest_dir = 'some new directory path'
shutil.copy(os.path.join(dirpath, fname), dest_dir)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With