Syntax error in Python_Steiner tree

ghpython
unhandled
(Chris Hanley) #7

side note: for posts that contain code formatting, you can enclose any code snippets with triple single quotes, put 3 single quotes above your first line of code, and 3 after the last line of code.

#triple quotes are above this line
for i in range(0,10)
  print i
#triple quotes are below this line.
1 Like
(P1r4t3b0y) #8

Hi @Arya,

I’ve managed to transcode it to python.
Feel free to check it out below.

PseudoSteinerTreesforPythonV2.gh (14.0 KB)

2 Likes
(A2905) #9

Hello Pb0y,

Thank you for trying it.This works great, much more cleaner and direct than my syntax.Perfect!
I was missing the use of data tree and mid point.

I wanted to know if there’s a possibility of keeping 120 degrees as a part of my input rule in python, instead of using Kangaroo and constant tension(to achieve 120) after getting the output.
So the python script will detect the nearest relative neighborhood points and make an angle of 120 degrees in between them.

Achieving this (output after relaxing lines in kangaroo)as a python output
image

instead of this(output of python) currently
image

Thanks
Arya

(P1r4t3b0y) #10

Hello @Arya

It sure should be possible somehow!
Is the Steiner tree always defined by 120° angles or is this just your preference?
Do you already have a strategy on how to mathematically or geometrically achieve this in mind?

#11

I don’t think it’s possible to generate the network topology (or geometry I suppose) directly with the 120 degrees angles using this algorithm. As this is an iterative point pairing/inserting process (and thus angles might change at each step). Hence the need for another iterative process downstream to relax the network (by either minimising the edge lengths or using the constant tension goals as Daniel explained).

For reference, in our video/algorithm (that you and @Petras_Vestartas referred to in the other thread) we implemented the Kangaroo relaxation process as a Python function (by implementing the KangarooSolver.dll directly) within the same GHPython component as the function that generated the network topology.

I suppose my question would be why you feel you need to skip the Kangaroo/relaxation process?

1 Like
(P1r4t3b0y) #12

@AndersDeleuran, could you please clear some things up about your kangaroo Python implementation (that I found on GitHub)?

# Reset state
if Reset or "counter" in globals():
    
    # Make physical system and goals list (using .Net type list)
    ps = ks.PhysicalSystem()
    goal_list = List[ks.IGoal]()
    
    # Add goals to system and goals list
    for goal in Goals:
        ps.AssignPIndex(goal, 0.01)
        goal_list.Add(goal)
    
    # Make static counter variable
    counter = 0

# Run state
else:
    if Step:
        ps.Step(goal_list, True, 1)
        counter += 1
  1. What does ps.AssignPIndex(goal, 0.01) exactly do?

  2. Would it be possible to construct complex goals in nested lists and assign those, like you would for instance entwined goals in Grasshopper before plugging them to the solver.

  3. Could ConstantTension simply be called with ks.Goals.ConstantTension() and how do I know what to pass in?

  4. What does the True and 1 mean whilst stepping?

Thanks.

#13

That is very old code, from back when we were first implementing Joey/K2 in GHPython. I’ll see if I can dig up some more up-to-date examples for you.

It assigns a particle index for the particles (i.e. points to operate on) in the goal. That is, instead of managing the topology of the particles/goals yourself, this method handles that for you. This is slower, but simpler. Though, one of the benefits of scripting K2 is exactly managing the topology of the system.

Yes it would, you can really do whatever fits your needs the best. This is a key advantage of scripting K2 IMO, in that one can actually simplify things quite a lot (as compared to the somewhat hoop-jumping of structuring/re-structuring datatrees). As I recall we also added a property on the goals that allowed one to tag them with a string (for explicitly naming/structuring the goals data), but I think that was only for the goal output. I forget, anywho. Yes you can.

Yes, exactly. You would go something like:

import clr
clr.AddReferenceToFile("KangarooSolver.dll")
import KangarooSolver as ks

# Make goals list
goals = []
for l in lines:
    if mode == "spring":
        g = ks.Goals.Spring(l.From,l.To,0.00,1.00)
    elif mode == "constantTension":
        g = ks.Goals.ConstantTension(l.From,l.To,-1.0) 
    goals.append(g)

It can admittedly be a bit difficult figuring what the input parameters mean, but maybe just have a look at the corresponding component, which is generally well documented:

2019-02-15%2008_15_36-Grasshopper%20-%20171201_PseudoSteinerTrees_Extracted_

Though that can also be confusing, since for instance the Spring goal is named Length in its component implementation.

I forget exactly (ping @DanielPiker), but I believe it’s ps.Step(GoalsList, RunParallel, KineticEnergy). One could also use the simpler ps.SimpleStep(GoalsList) when implementing a solver. There’s also the momentum step method, and I think one more :thinking:

1 Like
C# Kangarro2 Custom Goal behaviour
(Daniel Piker) #14

Thanks @AndersDeleuran, great explanation.
All I’d add is:
re. input parameters for the Goal classes, you can also look at the code for each goal to see exactly how these values are being used: https://github.com/Dan-Piker/K2Goals?files=1
For the Step method, the only important input is the list of goals.
RunParallel sets whether to use parallel processing - you can always leave it as True. The option is in there because for very small simulations the overhead of parallel methods can sometimes outweigh the gain, but we’re talking a millisecond or 2, so really not worth thinking about.
KineticEnergy is a threshold for stopping within that step if an energy threshold is reached, but since you should have some other method of stopping steps, there’s rarely any need to do this, so setting it high enough that it is never triggered is easiest, just leave it at 1.0

#15

Ah yeah, forgot you made those public. How time flies :older_man:

(A2905) #16

Hello Pboy,

Since 120 is the equilibrium angle of a Steiner tree, imitating the same from frei Otto’s experiment on bubbles. I believed that 120 degrees could be a part of my input rules and then I would evaluate the different iterations I get for other fitness criterias.
So the Python syntax would have degree and minimal path as constant rules.

(A2905) #17

Hello Anders,

Yes even I feel the same.I can’t really figure out the strategy mathematically to achieve constant tension in the ghPython syntax itself.
The angles are changing according to each point getting paired with its nearest neighborhood.

I am just inquisitive on the fact of using ghPython to do what kangaroo constant tension is doing, the output after constant tension in gh is not exactly 120 degrees if you would see here which is presenting a problem for me in optimizing the outputs furthermore.
image

The count is 5 here, as I increase the count the angle of 120 vanishes slowly as constant tension balances out the pull points as much as it can.

(Daniel Piker) #18

Hi Arya,

If you aren’t getting 120° angles, something is set up wrong. Kangaroo will converge to 120.0000° in seconds for this sort of thing. Are you sure you have the MaxIterations set high enough? I suspect you are just letting it stop before convergence.

As for doing it without Kangaroo - you could certainly try other approaches, but I’d be surprised if you find any way to do this without implementing some form of iterative/numerical method.
From your question I guess you are imagining some sort of constructive or analytical method (like for example, how you might solve a simple kinematics problem with intersecting circles).
As far as I’m aware no such method is known for finding general Steiner trees in 3d.

Whenever a simple constructive solution to a problem exists, it does usually make much more sense to use that, but there are many whole classes of problems for which we simply do not have any analytical methods of solving them, and the only way we can get an answer is by numerical methods. Surprisingly, sometimes these can even be problems which are simple to state and only involve satisfying simple geometric conditions.

Coming back to Steiner trees - the methods posted above may still break down if some segments approach zero length after relaxation. This can be solved by allowing the graph to reconfigure itself between iteration steps - I did once write something for these topological changes - I’ll see if I can find it and dust it off.

(A2905) #19

Thank you so much for describing it so well Daniel.
I understand now.
As for now I am using Kangaroo constant tension until I change my limits further.
For this particular algorithm, I am able to achieve minimal length and a degree of equilibrium, what I intended to do from the very start.
Indeed, I need to change my MaxIterations everytime I increase or decrease the count.
And yes the segments do break off while approaching zero length.
Thanks again

(P1r4t3b0y) #21

Hi @Arya,

You can simply add these components to remove the points that are inside of the sphere, before passing them on to GHPyhton and other components.

The B input of BrepInc is connect to the sphere component!

(A2905) #22

Hello again Pb0y!

I did try that.
But the lines are still getting inside the sphere even if there are no points.

The syntax should ignore point 2,even if it is shortest because it is entering the boundary of the sphere and instead connect to point 3 which is outside.
This will make you understand better-

You think this demands another python syntax.:thinking:

(P1r4t3b0y) #23

The simplest way would probably be to check the polylines, constructed from the output nodes points, for intersections with and inclusions in the sphere. You could then simply cull the desired curves too.

If I remember correctly, my version of the Python script only provided ordered points that you could construct lines from. In order to check, if a connection pierces the sphere somewhere, you’d probably also have to construct a line segment first, and check for intersections with or inclusion in the sphere in code. At the moment, I can’t think of a better way.

(A2905) #24

I don’t think that would be a proper solution to it.
As when I cull the lines inside of the sphere after the Python component, the points outside wont create a branching network instead create broken individual lines.
Could you please show what you are thinking.

I have another solution in mind.Can we give the input of the subtracted area to python?
(Cube)-(Sphere) and then populate the given area.Will that make sense.

Cube_Steiner_Mesh topology _19-02-19_for space.gh (35.7 KB)

(P1r4t3b0y) #25

Hi @Arya

In my above script, I meant to implement the idea in the point searching part of the python script. At the moment, each point looks for a closest neighbour (if I recall correctly). Each currently searching point should construct a line from itself to the potential closest neighbour and look for an intersection event with the sphere. If an intersection occurs, this potential next point is ignored and the search for another next point begins, if there are any “edible” points left in the system.
I guess my above proposal wasn’t very clear. :wink:

Do you mean populating the boolean subtraction of the boundary cube and the sphere?
I guess that wouldn’t work for the same reasons that my above proposal didn’t work. See, the python script would sill find closest points that, connected by lines would penetrate the sphere, as you showed above. Also, there is no guarantee that the regional population would even work that way in the first place.

Are you still looking for a solution or have you figured it out? :slight_smile:

(P1r4t3b0y) #26

I’ve posted a possible solution in your other thread here.

(A2905) #27

Hi Daniel,

As discussed before , the segments have to be adjusted for achieving the angle everytime. Is there any way through which I could set the MaxIter high in all cases?
I have to manually set it high every time I change the position of the points.

Thank you for your time in advance.

Steiner_ topology.gh (18.9 KB)