• 4
name Punditsdkoslkdosdkoskdo

How to manage thousands of IF…THEN…ELSE rules?

I am considering building an application, which, at its core, would consist of thousands of if...then...else statements. The purpose of the application is to be able to predict how cows move around in any landscape. They are affected by things like the sun, wind, food source, sudden events etc.

How can such an application be managed? I imagine that after a few hundred IF-statements, it would be as good as unpredictable how the program would react and debugging what lead to a certain reaction would mean that one would have to traverse the whole IF-statement tree every time.

I have read a bit about rules engines, but I do not see how they would get around this complexity.

Please, use software/computer languages that are fit for the task. Matlab is used very often to model complex systems, where you can have indeed literally thousands of conditions. Not using if/then/else clauses, but by numerical analysis. R is an open source computer language that is filled with tools and packages to do the same. But this means you also have to restate your model in more mathematical terms, so you can include both the main influences and the interactions between influences in models.

If you didn't already, please follow a course about modelling and simulation. The last thing you should do, is considering writing a model like that in terms of if - then - else. We have monte carlo markov chains, support vector machines, neural networks, latent variable analysis, ... Please don't throw yourself 100 years back by ignoring the wealth on modelling tools you have available.

  • 0
Reply Report

It sounds like all these conditional statements that you're talking about should really be data that configures your program rather than part of your program itself. If you can treat them that way, then you'll be free to modify the way your program works by just changing its configuration instead of having to modify your code and recompile every time you want to improve your model.

There are a lot of different ways to model the real world, depending on the nature of your problem. Your various conditions might become rules or constraints that are applied to the simulation. Instead of having code that looks like:

if (sunLevel > 0.75) {
   foreach(cow in cows) {
       cow.desireForShade += 0.5;
if (precipitation > 0.2) {
   foreach(cow in cows) {
       cow.desireForShelter += 0.8;

you can instead have code that looks like:

foreach(rule in rules) {
   foreach (cow in cows) {

Or, if you can develop a linear program that models cow behavior given a number of inputs, each constraint might become a line in a system of equations. You might then turn that into a Markov model that you can iterate.

It's hard to say what the right approach is for your situation, but I think you'll have a much easier time of it if you consider your constraints to be inputs to your program and not code.

  • 0
Reply Report

The logic programming language Prolog may be what you're looking for. Your problem statement is not specific enough for me to assess if it's a good fit but it's rather similar to what you say.

A Prolog program consists of facts and rules that are applied. Here's a simple example rule that states "A cow moves to a location if the cow is hungry and there is more food in the new location than in the old location":

moves_to(Cow, Location) :-
  current_location(Cow, OldLoc),
  food_in(OldLoc, OldFood), food_in(Location, NewFood),
  NewFood > OldFood.

All things in capital letters are variables, things you don't know the value of. Prolog attempts to find values for these variables that satisfies all the conditions. This process is done with a powerful algorithm called unification that is the heart of Prolog and similar logic programming environments.

In addition to the rules, a database of facts is provided. A simple example that works with the rules above could be something like:

current_location(white_cow, pasture).

current_location(black_cow, barn).

current_location(angry_bull, forest).

food_in(barn, 3).
food_in(pasture, 5).
food_in(forest, 1).

Notice that white_cow and pasture, etc. are not written in capitals. They are not variables, they are atoms.

Finally you make a query and ask what's going to happen.

?- moves_to(white_cow, Destination).
?- moves_to(black_cow, Destination).
Destination = pasture
?- moves_to(Cow, Destination).
Cow = black_cow, Destination = pasture
Cow = angry_bull, Destination = barn
Cow = angry_bull, Destination = pasture

The first query asks where the white cow will move. Given the rules and the facts above, the answer is No. This can be interpreted as "I don't know" or "It doesn't move" depending on what you want.

The second query asks where the black cow moves. It moves to the pasture to eat.

The final query asks where do all the cows move. As a result you get all the possible (Cow, Destination) that make sense. In this case the black bull moves to the pasture as expected. However, the angry bull has two choices that satisfy the rules, it can either move to the pasture or the barn.

Note: It's been years since I last wrote Prolog, all examples may not be syntactically valid but the idea should be correct.

  • 4
Reply Report