Community

Hi, I designed a experiment in which I have 2 factors: Nworkers(defined in Main) and ChoiceIndez(defined in Main). Now, I want to write in anyfile the results of my experiment along with the values of factors. For the moment I tried to write the following row in After Simulation Run: traceln("#Worker "+Nworkers+" #Rule "+ChoiceIndex+" Replication "+getCurrentReplication()+" #Products "+get_Main().NumberOfProducts); NumberOfProducts is a variable plane defined in Main, in which I have one of my results of simulation. The model doesn't work and shows The method get_Main() is undefined..... if I write only NumberOfProducts .....then the error is ....cannot be resolved! Someone has suggestions? Is there another way to export? I have educational license. Thanks
Hey Mauro, get_Main() is undefined for the Experiment? That makes sense to me. I think the Experiment only has a reference to your top-level ActiveObject as getEngine().getRoot(). So, you could try accessing those variables that way. (getEngine().getRoot().NumberOfProducts) If I were you, though, I'd put it in the Destroy code section of your Main object. I guess that depends, though... what kind of Experiment is it? Simulation? Optimization? Sensitivity Analysis? Also, are you planning to use a BufferedWriter to write the outputs to a file? I can provide a code snippet if you like. /willy
Hi Willy, I tried to write only: root.NumberOfPRoducts and it is work very well. Thanks I tried to write in Destroy code but there are many error during the experiment run. My esperiment is a design experiment with many factors (parameters) and some results. For the moment I write all in console....then I copy console and paste in excel..... I don't know "BufferedWriter". What is? The code could be useful for me. Thanks
Mauro, Regarding capturing data outputs: I *JUST NOW* noticed that there's a "File" object in the AnyLogic 6.5 connectivity palette. I haven't used it, and it MAY be better than what I'm suggesting now. Here's what I do to capture outputs: First, you need to add this to the "Imports section" of the ParameterVariation experiment: import java.io.*; This gives your application access to the classes that it needs to handle input/output. Basically, in the same place that you're traceln()ing now, you'd do something like this: After Simulation Run: ------------------------------------ try { String filename = "myOutputs.txt"; //this will be the name of your output file. FileWriter fw = new FileWriter(filename); //this opens a stream to write to the file BufferedWriter bw = new BufferedWriter(fw); // this wraps the FileWriter in a more efficient stream writer bw.write("#Worker "+Nworkers); //this sends the data to the buffered writer. This does bw.write(" #Rule "+ChoiceIndex ); bw.write("#Replication"+ getCurrentReplication()); bw.write( "#Products "+root.NumberOfProducts); bw.newLine(); // this adds a new line to the file. bw.flush(); // this makes sure everything in the bufferedWriter has been send to the file via the filewriter bw.close(); //close the file } catch (IOException e) { //Handle the exception... issues may include file not found, read only, etc. e.printStackTrace(); //this prints the error info to the console so you can see it. } ------------ This is a pretty straight forward implementation. Very often, I will open a BufferedWriter on a file at the beginning of a simulation, and stream data to it as the simulation runs, then flush() and close() it in the Destroy code. Anyway, from here you can format the outputs any way you want. Comma separated values for importation into Excel, for example... Here's more information on BufferedWriter: http://java.sun.com/javase/6/docs/api/java/io/BufferedWriter.html
Hi Willy, I am using your method to store information. I have a problem.....the code creates the file but each time the code rewrite a new line on old line. I try to explain with an example: after run 1 in file "abc" I have 1 line with: replication 1 number product 23 after run 2 in file "abc" I have only 1 line with replication 2 number produt 31 The row of run 1 has been deleted. I want to store each run. Have you a suggest? Thanks, byebye
Hmmm... Not 100% sure. Try changing the .write() calls to .append(). /w
Hi, Willy, unfortunately it works in the same way...... maybe I have not to create each time the file by String filename = "myOutputs.txt"; //this will be the name of your output file. FileWriter fw = new FileWriter(filename); //this opens a stream to write to the file Maybe I have to use this only the first time. I tryed but I have many errors! I don't know Java Thanks
Hi Willy, I solved by FileWriter fw = new FileWriter("abc.txt",true); the value true enable to write in the end of file. BYEBYE
Yeah, there you go. that second argument puts it in append mode. Good going. /w