How to read the data inside any type of input from ghenv command

grasshopper
python

(Alexholden18) #1

@qythium @piac @DavidRutten

Hi everyone

I want to access to any type of data using ghenv command. Here is the code and image:

import Grasshopper

slider = ghenv.Component.Params.Input[0].Sources[0]

a = Grasshopper.Kernel.Parameters.Param_Number.Access.GetValue(slider)

I do know that this simply possible by directly using a = x but I need to do this through accessing it by ghenv components. Here I have made 3 different types of input, one is a number, another is a panel and the third one is a generic output. They all have the same amount but different type. Is there any code which could read the data regardless of its type.

any help or comment is appreciated

read any kind of input.gh (5.9 KB)


(Giulio Piacentino) #2

May I ask why you would need this?


(Alexholden18) #4

Hi piac

Somehow I managed to do it with VolatileData but actually I am working on different small task to learn how to code an optimization algorithm.

As I have understood, in optimization tasks when we change any variable we should run the solution from start so we could get the final answer and load it as cost function. As I read in one of posts, @DavidRutten said that in Galapagos the optimization code runs in another window. I don’t really know how to do this.

To clarify the question I want to have 100 iterations in my optimization code and for each iteration I want to generate 50 different sets of random variables. Each set has for example 3 variables. Each of these random set of variables should be sent to number sliders and the final answer should be received back in the code. I don’t know how to do this so that I don’t have solution problems in my code…

Here I have wrote a simple code so we could speak on this case.

import random

def CostFunction(a):
    return sum(a)

variables = []
variables_results = []

for i in range(100):
    pop = []
    pop_results = []
    for j in range(50):
        set = []
        for k in range(3):
            set.append(random.randint(0,100))
        c_function = CostFunction(set)
        pop.append(set)
        pop_results.append(c_function)
    variables.append(pop)
    variables_results.append(pop_results)

In the python code I have wrote the CostFunction and it has no problem. But if I want to write the Cost function as a series of grasshopper components (as shown in the image) I face the solution problem. I want to see if there is any ways that I could evaluate different sets of variables and receive the result in my for loop.

simple code.gh (7.7 KB)


(qythium) #5

Alex, isn’t this the exact same problem you had in your previous post? (Question on ExpireSolution in GhPython) I explained how to get around the ‘object expired in solution’ errors by using SolutionSchedule, but your file above doesn’t follow any of it, and makes no sense at all with relation to your description - you’re constructing a nested list of 5000 random items without reference to the inputs or outputs?
Also, ‘set’ is a reserved word in Python, you really shouldn’t be overriding it.

I was a little hesitant in leading you down this direction previously, because it seemed like you’re trying to dive into the sparsely-documented inner workings of Grasshopper without a solid grasp of its basic interface or of the Python language… Maybe you could take a look at the Hoopsnake or Anemone plugins, they allow you to do sequential iteration on the canvas with the full introspection of states and variables that Grasshopper provides .


(Alexholden18) #6

hi qythium

The previous post you answered was a very useful and good code and I tried to use it for some nested loops. I went through it and I recognized that if I want to use that code for optimization algorithms I have to use if and else commands several times and nest some if and else inside previous else command and it makes my coding a little bit complicated in overall. (I am not sure if this would be a correct way of using previous code!)

The point is that now I am looking for way to change variables and receive the answers without having any effects on the solution of optimization algorithm. So I could easily write any optimization code with for loops and independent of any new solution effects of variables…

In the code presented here I have defined a costfunction inside the code and I referred it from the main loop.

For the set name I made the mistake, thank you for the explanation