We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to save an algorithm
Page Index Toggle Pages: 1
how to save an algorithm (Read 455 times)
how to save an algorithm
Nov 24th, 2008, 10:28am
 
Hi,

I have a series of functions(algorithms) that generates 8x8 matrix as output(int[][]). I'd like to save and load these algorithms but i have no idea about save it and after that load and run it. i suppose i need another function who just run the saved functions.

anybody can help me?

Q
Re: how to save an algorithm
Reply #1 - Nov 25th, 2008, 10:45pm
 
There are 3 parts to this.
1. Determine how to save a file. I'd use spaces to separate the digits.
2. Get save to work
3. Get load to work

1 is basically done
2 is easy.. just make a string, and output all the numbers to that, with spaces between..
ie
String str = "";
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
if(j > 0){
str += " "; // add a space if it's not the first digit
}
str += ""+arr[i][j];
}
str += "\n"; // new line
}

then, save the string. look up saveStrings in the reference

3.
loadStrings give you an array of lines
then you just have to split each line on space, and you'll have your array

good luck!
Re: how to save an algorithm
Reply #2 - Nov 26th, 2008, 10:37am
 
hi,

the problem is different, in your way i can save the results but not the function that's my goal.

I think i'm gonna create classes, compile them and run by Class.forName().

At the moment is the only way i found to save and run algorithms.

Maybe i will declare an interface or something like that to have just one main class and the others(the real algorithms) inherit/extend/implement(yes i don't remember how use it) that one.

how it sounds?

Re: how to save an algorithm
Reply #3 - Nov 26th, 2008, 2:01pm
 
Ah, like seltar, I initially misunderstood the question... Smiley

For what it is worth, I did something similar in a version of my (unpublished yet) fern drawing sketch: I made a generic code to draw ferns using trunk, branches, leaves, with variations on angles, lengths, colors.
The base drawing unit of my sketch is a line segment.
So, in a .java file, I defined an abstract class Segment, base class of Trunk, Branch, Leaf classes, each instance holding a Genotype reference.

Genotype is an interface (that's why I had to make a .java file, interfaces cannot be internal to a class).

interface Genotype
{
float GetBaseLength(Segment seg);
float GetLength(Segment seg);
float GetBaseWidth(Segment seg);
float GetWidth(Segment seg);
float GetAbsoluteAngle(Segment seg, boolean bIsLeft);
float GetAngle(Segment seg, boolean bIsLeft);
int GetBaseColor(Segment seg);
int GetColor(Segment seg);
boolean ShouldAddSegment(Segment seg);
}

Then I have a number of genotype .pde files, currently unimaginatively named Genotype01 to Genotype09, implementing Genotype, where I can make more or less convoluted variations on these parameters.

Advantage: no need for reflection, natural OO approach, .pde files act as script files (although you need Processing to add them and they have to be known by the final sketch).

Hope it will give you some ideas.
Re: how to save an algorithm
Reply #4 - Nov 26th, 2008, 9:55pm
 
Hi,

yes i got some ideas, basically you are doing the same thing, we just have different kind of objects.

I have a question, how do you use your genotype?(genotype1 ...genotypeN) I need to display a list of all classes(in my case effects) to select them and put in a sequence, at the moment i have a Sequence object that contains a sequence of Loop objects, i also think that Loop could become my interface and the other algorithms (effects) will implement that interface.

Your sketch sounds really interesting, let me know when you'll publish something, if you agree i could use some stuff for the exbhiting that i'm planning.
Re: how to save an algorithm
Reply #5 - Nov 27th, 2008, 12:30am
 
If you are curious, you can see a static view of the result at Ferns - Static view.
I made a short video with QuickTime, but due to lack of time (it was for a kind of challenge), I didn't made the ferns to grow as I wanted, but just appear and disappear at various times, and move everything regularly to the left...
I should finish this sketch someday.

To answer your question, my abstract Segment class has a Genotype member. I use that in the derivated classes, to set settings based on support segment, eg. for Leaf:

SetWidth(m_genotype.GetBaseWidth(baseBranch));
SetLength(m_genotype.GetBaseLength(baseBranch));
SetAbsoluteAngle(m_genotype.GetAbsoluteAngle(baseBranch, m_bIsLeftLeaf));
SetColor(m_genotype.GetBaseColor(baseBranch));

I made a fern class taking a genotype in the constructor:

Fern(Genotype genotype, float startTime, float endTime)
{
 this(null, genotype, startTime, endTime);
}

The times indicate when the fern appeared and disappeared in the movie.
And I created a bunch of ferns put in an ArrayList with:

ferns.add(new Fern(new Genotype07(), 0.45, 0.67));// Big

There are some things I might redo today, but the design was relatively flexible.
Re: how to save an algorithm
Reply #6 - Nov 27th, 2008, 11:34am
 
ah ok then you don't initialize you object dynamically, i have to create new instance by the interface.

my flow is: load all classes(effects), display them in the interface, select one, create an instance and put in the sequence.
Re: how to save an algorithm
Reply #7 - Nov 27th, 2008, 3:02pm
 
It shouldn't be hard to select a class from user input.
Something like:

Genotype g;
switch (choice) {
case LEFT: g = new GenotypeLeft(); break;
case RIGHT: g = new GenotypeRight(); break;
//[...]
default: g = new GenotypeFoo(); break;
}

switch is often frown upon, these days, but allows simple, efficient code...
Or you can use an enum. In a .java file, since 1.5 syntax isn't yet supported by Processing, AFAIK.
Re: how to save an algorithm
Reply #8 - Nov 27th, 2008, 7:44pm
 
mmm i prefer something more dynamic, i would use the Class.forName() method to create new instance easily, i don't like to hardcode all effects, is better to load the list dynamically, for example reading a directory a listing all files, after that should be easy to create a new instance.

in your way i should add one condition for each effects and it doesn't sounds so good no?

sorry if i'm carrying this discussion so far but i really want to make something nice and stable.

if you agree we could move this discussion in the email or instant messaging, as you prefer. It's easier explain everything


Q
Re: how to save an algorithm
Reply #9 - Nov 28th, 2008, 12:29am
 
Ah, bah, I like the forum discussion, and who know, perhaps somebody will find ideas in our discussion... Smiley

"add one condition for each effect", "reading a directory a listing all files"?
Unless I misunderstood the purpose, you have to compile everything, with PDE or something else. It isn't so much dynamic. The logic works with interpreted languages, not so much with (semi-)compiled ones. So adding conditions isn't so much a constraint.
But of course, that's just my point of view, using Class.forName() is perfectly valid too, AFAIK it is often used to manage plugins and similar, which isn't far from your approach.
There is rarely (never?) an unique truth in programming! Smiley
Re: how to save an algorithm
Reply #10 - Nov 30th, 2008, 11:45am
 
ok, i think i found my solution.

at the moment i created a class named Effect, it contains two methods, getNextFrame() and generateFrame(), the second one is just the signature. After that i created other classes that extend Effect, each class only implements the generateFrame() method.

in my app i'm doing something like that and it seems working

class Pippo extend Effect {...

Effect e = new Pippo();


you can find some code here, let me know what do you think about this solution

Main
http://pastie.org/326986

Effect
http://pastie.org/326987

Pippo
http://pastie.org/326989
e.getNextFrame();


PS: now i have a little problem, all my effects are in the main folder, how can i move them into a sub folder? i tried with the package but it doesn't works, maybe i wrong something
Page Index Toggle Pages: 1