1

I'm working on an Mechatronics project where i access current(Amps) data from multiple sources and have to calculate a response(fed to mechanical system) based on changing value trend within and among (increasing/decreasing values and increasing/decreasing relative differences). There are many a conditions to access (unique or mixed response to each) and many a variables they are dependent on, so i'm left with lots of nested if-elif-else statements each evaluating multiple conditions and flags thus taking time to respond while data flows in fast (as much as 85 Hz).

The module is part of larger project and needs to be done using Python only. Here's how that part of my current code looks like -

def function(args):
    if flag1 and flag2 and condition1 and not condition2:
        if condition3 and not flag3:
            response += var1
            flag4 = True
        elif -- :
            response = var2
            flag3 = False
        elif -- :
            ------------
        else :
            ------------
     if not flag_n and flag_m and condition_p and condition_q and not condition_r:
        if.. elif ... else :
            flags... response changes..
     more IFs

i need is a better and effecient way of doing this or a completely different approach e.g. some machine learning or deep learning algorithm or framework suitable for above kind of usage.

gSivaych
  • 13
  • 4
  • Draw a karnaugh map of your function and simplify it to a boolean expression. – rdas May 01 '19 at 19:08
  • How many flags are there, and how many unique conditions? – user3386109 May 01 '19 at 19:52
  • K-map approach might simplify the situation for me. There are 8 flags, 18 unique conditions and 8 unique responses as of now, more will be added (new sensor to integrate) when this becomes somewhat stable. – gSivaych May 02 '19 at 07:51

2 Answers2

2

You could use binary, maybe:

flag_bits = {flag1: 0b0000000001, 
             flag2: 0b0000000010, 
             flag3: 0b0000000100, 
             flag4: 0b0000001000, 
             condition1: 0b0000010000,
             condition2: 0b0000100000,
             ...}

Then as you receive flags and conditions evaluate them bitwise, and have a dictionary of results or methods to calculate results based on it:

def add_response(response, add_value):
    return response += add_value

def subtract_response(response, subtract_value):
    return response -= subtract_value

response_actions = {0b0000110011: ('add', var1, 0b0000001000), ...}
response_methods = {'add': add_response, 'sub': subtract_response, ...}

response_action = response_actions[0b0000110011]
response_method = response_action[0]
response = response_method(response, respnose_action[1])
flag_bits = response_action[2]

Obviously, not totally perfect, but it will eliminate lots of ifs and turn actions into a lookup and hopefully save time.

Wes S.
  • 51
  • 4
0

From your question, I could not understand whether your problem was if-else statements getting bigger and more confusing over time, or them being computationally intensive.

In either case, any type of machine learning or deep learning framework would probably be much slower than your if-else's, and more confusing because it is very hard to know why-AI-deep-learning-algorithm-does-what exactly. What if your robot flips over? You'll never know why. But, you can trace if-else statements... I would strongly suggest not going the AI route, if your if-else trees aren't something like... 3000-5000 lines long, they keep changing daily 100-200 lines, or anything like that.

Software developers generally try to follow good design principles to not fall into situations as this, however, if it is too late to change architecture, What is the best way to replace or substitute if..else if..else trees in programs? (polymorphism) can come to your rescue.

That being said, I've worked on a lot of sensors/math heavy projects, and they grew always the same: project starts nice and slow, then comes nice improvements, then comes the deadline and you end up with an if-else spaghetti. Always the same. At least for me. So, what I do nowadays is whenever I have an improvement, I try to add it to the source code such that the general architecture stays in tact.

Another way of dealing with this issue is writing flowcharts, as in Matlab's Simulink, and showing explicitly your general idea of how the project is going to work/how it is actually implemented etc.