Grasshopper

algorithmic modeling for Rhino

How to get one branch out of a tree and store it in a list? in C#

Hi all,

till now i've got this code:

 

protected override void SolveInstance(IGH_DataAccess DA)

{

GH_Structure<IGH_Goo> tree = new GH_Structure<IGH_Goo>();

 

DA.GetDataTree(0, out tree);

 

tree.get_Branch(0);

 

and just for a test I did it in the output and it works

DA.SetDataList(0,tree.get_Branch(0));

}

 

But I want to take several branches with a for loop and store them all seperately as lists called for example branch 1 to i, which I can use later for further evaluations.

Would be the same like in Grashopper the explode tree component.

But in C# there is no explode method or something like that.

 

Can somebody help me please? Didn't find anything till now in the other discussions.

Views: 2531

Replies to This Discussion

You mean that you want that type of stuff?

List<someType> someList  = someTree.Branch(i).ToList();

Hey peter,

I found it already by myself, but thank you very much for your answer!

I did it like this:

IList p1 =  tree.get_Branch(0);

I had to take IList, but I don't know why, but it works :-)

I even don't know what's the difference between IList and List do you know that?

And is it possible to graft a list?

For Example, I want all values of a List seperately. Otherwise I've to do it in Grasshopper.

Thank you very much!

List > all values "separately" > in plain English > a tree:

public DataTree<T> MakeTree<T>(List<T> dataList){
    DataTree<T> dataTree = new DataTree<T>();
    for(int i = 0; i < dataList.Count; i++){
      dataTree.Add(dataList[i], new GH_Path(i));
    }
    return dataTree;
  }

Other that that:

https://www.google.gr/url?sa=t&rct=j&q=&esrc=s&sour...

https://www.google.gr/url?sa=t&rct=j&q=&esrc=s&sour...(v%3Dvs.110).aspx&ei=NbLkVdrSHoHasgHv2qjYCw&usg=AFQjCNEviU8qD6_VSaf5-6DWYL9vvVHExQ

http://www.codeproject.com/Questions/325178/Difference-between-ilis...

http://www.codeproject.com/Articles/832189/List-vs-IEnumerable-vs-I...

Hey Peter,

 

I tried your example, but it doesn't work.

I think this MakeTree Method would work, but I can't create a List I only can create an IList with the .get_Branch.

 

and I didn't find this sometree.Branch() is there missing an using assembly or

is it the same like sometree.get_Branch()?

But i don't either find the .ToList()

 

I think the .get_Branch leads to an IList

and maybe this .Branch leads to a List?

 

There's always a warning: IList cannot be converted into List<>. There's already an explicit conversion existing.

 

Thanks for the links to the definitions of IList and List, (could have google it before..my fault:-S) still don't understand it quite all, but I'll try to do so.

Doesn't work?

Are you sure that we are talking about C# matters? he, he

Get these tree/list examples. On top is the thing that doesn't work ... er ... I mean the thing that DOES work.

BTW:

IEnumerable<T> allows you to iterate through a collection.

ICollection<T> builds on this and also allows for adding and removing items.

IList<T> also allows for accessing and modifying them at a specific index. By exposing the one that you expect to work with, you are free to change your implementation.

List<T> happens to implement all three of those interfaces ...

best,Peter

Attachments:

Hey Peter,

 

sorry I'm pretty new in C#.

 

I finally got it. Thanks a lot!

 

But: 

In the End I did it with the C#-Component in GH.

see attached file.

 

When I try the same with C# in Visual Studio it doesn't work.

 

I think there's a problem with the DataAcces, but I didn't find the problem yet.

I tried it with GH_ParamAcces.tree

but then I had to branch the tree with the _getBranch(i) and stored it in an IList. The problems I had before.

 

But when I trie it with GH_ParamAcces.item

there are Errors in the output like Invalid Cast: Number>> DataTree.

And Solution Exception Index out of Range..

 

Here's the code i typed in:

 

 best, Heiko

 

Attachments:

Well ... for such simple scripts I barely can see any reason to use VS (but truth to be said: build in GH editor is crap, he he).

Other than that and since I run an AEC practice with lot's of CAD apps (from CATIA to AECOSim/Generative Components) I try to write code as "GH neutral" as possible for reasons of portability (when possible). With this perspective in mind I would prefer rather "standard" nested structure stuff than DataTrees.

BTW: GH SDK is not the best thing that happened to mankind. 

Get this "tutorial" as well (for fun) ... with regard ... er ... hmm ... dice matters on trees.

best,Peter

Attachments:

Well, that's just the start.

In the end I want to get an Optimisation Algorithm (ParticleSwarmOptimisation) as Component.

Therefore I've to start with this branches and give the values of the branches seperately out and evaluate something with this values in GH and catch this evaluated values from GH into the Component and store them and so on and then optimize the evaluated values of each branch and so on till I get all the local minima. Then I want to give out all local minima and show them in the output.

And therefore I thought it would be better to do this in VS. And like you said building in GH Editor is really crap:D

 

so let's see how I'll proceed.

 

So once again thanks for your help and I doubt this wasn't my last question at this website:D

 

See you, Heiko

 

Hmm ... even I haven't got the gist of that Swarm thingy of yours in full I have a feeling that's not THAT complicated (doable with the ... crap, he he).

If you describe EXACTLY what are you after I could give it a spin. Or (Plan B) provide the values in some trees (internalized) and describe EXACTLY what kind of "treatment" you wish out of them.

BTW: Component? why not a C# (or some) that could been altered/modified/improved "on the fly" ?

This doesn't seem to work anymore. Branch no longer exists for data trees, instead I think it has been replaced by Branches. Is there a clean method for doing this?

Found the answer:

List<Type> someList= someTree.Branches[path];

duh.

OK well, I try to explain Exactly what I want.

But puh I think it's little bit complicated for me but maybe not for you :-)

 

First of all I give you an example on what it (The optimization tool) should work.

For example there's a grid shell and I've got a number of control points (for example 3) that can move up and down.

Depending on the control points I get forms that are structurally good and some that are bad.

 

In my office we've got a GH-Component, which leads the geometry in structural members and solves the structural forces and so on through an external Software called Sofistik and afterwards gives back to GH some Values, for example maximum bending moments. (Like Karamba)

Now I want to create this optimization component or something like that to minimize e.g. the bending moments in the given geometry.

 

Let's start with the work of the component.

So when I've three control points that can only move in z-direction.

P1(0,0,Z1), P2(10,0,Z2), P3(5,5,Z3)

They only depend on Z, so everything depends on Z1 to Z3 which have a range between 0 and 10 f.e.

First I want to get some (between 9 and 15) random Particles, one particle consists of this 3 different Z's.

So for example the first particle Part1 is [Z1=10, Z2=5, Z3=7]

and the second particle Part2 is [Z1=7, Z2=1, Z3=9]

and so on.

I created these Start Particles in a Cluster. See attached file.

I also tried this in C#, but thought it is easier in GH.

 

After I've got the Start Particles I want to give out the first particle and evaluate with its including Z's the target value in GH. Therefore I had to take the first branch and graft this branch (Discussion before)

Afterwards I want to save this Target Value that depends on the first starting Particle. Then I want to give out the second starting Particle to evaluate its target Value and store it. And so on till the last target Value of the last Starting Particle got assigned.

Then I want to assign the particles with its target values. E.g. part1: t=0.9, part2: t=1.8...

 

Then I want to define neighborhoods or the count of the expected local minima.

These neighborhoods can look like: Each neighborhood has to include not less than 3 particles. And the particles have to be next to each other.

E.g. if there are 12 particles and I want to have a look for 3 local minima, I need 3 or 4 neighborhoods. Then I would take 3 neighborhoods, because the more particles in one neighborhood, the better.

So the Count of the neighborhoods would be N=min{(Count of Part/3)& N_min}

How to define these neighborhoods I don't know at the moment. I think it has to be searched for the distance between the particles. E.g. part1 with (9,9,9) and part2 with (9,9,8) are next to each other but part 3 with(1,1,2) is far away.

 

Then each StartParticle is set to Partx_localbest.

And in each Neighbourhood the best of these localbeststs is Part_NyBest. (The best ist the one with the smallest target Value)

 

Loop:

Now I want to create new Particles. These Particles don't change their Z-values randomly. They change their Z-Values depending on Part_NxBest and Part_localBest. Therefore it has to be evaluated a new velocityfactor with v_Partx_new=0,792*v_PartxOld+1,5*random(0,1)*(partx_localbest-partx)+1,5*random(0,1)*(part_NyBest-partx)

The new particles will then be partx_new=partx+v_Partx_new.

The new Particle partx_new will be set to partx and then set in the output.

then there has to be caught the targetValue of part1 afterwards part2 can be put out and its target value caught and so on.

Then it has to be looked for the Partx_localbest through comparing the partx_localbest and its target value with the new part_x and its target value. If the target value of the new partx is smaller than partx_localbest,

then partx_localbest is the new partx.

This has to be done for each partx. Afterwards the same for neighborhoods best (best of all partx_localbest in one neighborhood)

Endloop if velocity gets small.

 

Output all part_NxBest

Output all targetvalues of the part_NxBests.

 

So in the Input there have to be:

StartParticles if they are given through the cluster attached.

Device on the target Value like in the attached gh.file from David Rutten I found in the discussions

Count of neighborhoods

 

And in the output

Output particle for evaluation

Output all part_NxBest

Output all targetvalues of the part_NxBests

 

Hope didn’t forget anything. And hope it isn’t crushed to badly. Sorry for my bad English by the way ;-)

 

For more explanation, how the PSO works in other programs. There’s attached a workflow script (is it called like that?) I think for GH it should be a little bit changed like I tried in my explanations.

 

So if you can help me a in some parts or you have any advices would be great, otherwise thank you nevertheless!!!!

Thankfully there’s no limit for the words in the discussions :-D

 

Best, Heiko

 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service