开发者

homogenization the functions can be compiled into a calculate networks?

开发者 https://www.devze.com 2023-01-27 08:54 出处:网络
Inside of a network, information (package) can be passed to different node(hosts), by modify it\'s content it can carry different meaning. The final package depends on hosts input via it\'s given rout

Inside of a network, information (package) can be passed to different node(hosts), by modify it's content it can carry different meaning. The final package depends on hosts input via it's given route of network.

Now I want to implement a calculating network model can do small jobs by give different calculate path.

Prototype:

def a(p): return p + 1
def b(p): return p + 2
def c(p): return p + 3
def d(p): return p + 4
def e(p): return p + 5


def link(p, r):
    p1 = p
    for x in r:
        p1 = x(p1)
    return p1

p = 100
route = [a,c,d]
result = link(p,result)开发者_如何学JAVA
#========
target_result = 108
if result = target_result:
   # route is OK

I think finally I need something like this:

  p with [init_payload, expected_target, passed_path, actual_calculated_result]
  |
  \/
 [CHAOS of possible of functions networks]
  |
  \/
  px [a,a,b,c,e]  # ok this path is ok and match the target 

Here is my questions hope may get your help:

  1. can p carry(determin) the route(s) by inspect the function and estmated result?

    (1.1 ) for example, if on the route there's a node x()

    def x(p): return x / 0 # I suppose it can pass the compile

    can p know in somehow this path is not good then avoid select this path?

    (1.2) Another confuse is if p is a self-defined class type, the payload inside of this class essentially is a string, when it carry with a path [a,c,d], can p know a() must with a int type then avoid to select this node?'

  2. same as 1.2 when generating the path, can I avoid such oops

    def a(p): return p + 1

    def b(p): return p + 2

    def x(p): return p.append(1)

    def y(p): return p.append(2)

    full_node_list = [a,b,x,y]

    path = random(2,full_node_list) # oops x,y will be trouble for the inttype P and a,b will be trouble to list type. pls consider if the path is lambda list of functions

PS: as the whole model is not very clear in my mind the any leading and directing will be appreciated.

THANKS!


You could test each function first with a set of sample data; any function which returns consistently unusable values might then be discarded.

def isGoodFn(f):
    testData = [1,2,3,8,38,73,159]   # random test input
    goodEnough = 0.8 * len(testData)  # need 80% pass rate

    try:
        good = 0
        for i in testData:
            if type(f(i)) is int:
                good += 1
        return good >= goodEnough
    except:
        return False

If you know nothing about what the functions do, you will have to essentially do a full breadth-first tree search with error-checking at each node to discard bad results. If you have more than a few functions this will get very large very quickly. If you can guarantee some of the functions' behavior, you might be able to greatly reduce the search space - but this would be domain-specific, requiring more exact knowledge of the problem.

If you had a heuristic measure for how far each result is from your desired result, you could do a directed search to find good answers much more quickly - but such a heuristic would depend on knowing the overall form of the functions (a distance heuristic for multiplicative functions would be very different than one for additive functions, etc).


Your functions can raise TypeError if they are not satisfied with the data types they receive. You can then catch this exception and see whether you are passing an appropriate type. You can also catch any other exception type. But trying to call the functions and catching the exceptions can be quite slow.

You could also organize your functions into different sets depending on the argument type.

functions = { list : [some functions taking a list], int : [some functions taking an int]}

...
x = choose_function(functions[type(p)])
p = x(p)


I'm somewhat confused as to what you're trying to do, but: p cannot "know about" the functions until it is run through them. By design, Python functions don't specify what type of data they operate on: e.g. a*5 is valid whether a is a string, a list, an integer or a float.

If there are some functions that might not be able to operate on p, then you could catch exceptions, for example in your link function:

def link(p, r):
    try:
        for x in r:
            p = x(p)
    except ZeroDivisionError, AttributeError: # List whatever errors you want to catch
        return None
    return p
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号