Avoiding endless conditional statements

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Here is my problem.  I want to make a webapp that will basically take
the work out of finding what tool works for what situation.  There are
5 factors that go into tool selection.  1)Material Group, 2)Insert
Radius, 3)Insert Type and Size, and 2 others that I can't remember
off-hand.  There are probably about 1000 different scenarios that I
have to code for..(there are several blank table cells and it's not
setup as you might imagine, there is a lot of overlapping), and I
desperately want to avoid making a gigantic conditional statement.  I'm
afraid that I really have to provide a picture of what I'm doing here,
so I will do that.  Here is the link.
http://www.bigkaiser.com/temp/weirdtable.jpgYou only see 3 of the 5
factors on this page, and the other 2 have to do with whether this
table or one of many other tables (not pictured) will be used.

So, now that I've confused everyone.  That's my table.  What's a good
way to go about programming that sucker in so that people don't have to
do all the "work" involved to figure out what tools they need.  They
should just be able to choose selections from 5 dropdown boxes and get
one tool back.  A tool is something like "10.655.600".

Thanks for whomever tries to come up with a solution to this poorly
explained problem. :)


Re: Avoiding endless conditional statements

Hi pk,

Sounds like a schoolbook example for an expert system, typically an  
application of production rules, backward chaining. But you do need an  
inference engine for that, i am not sure there is one written in php. If  
you can't find one, maybe you can just build a series of methods, each  
reacting on one a specific question and filled with rules that return a  
value if applicable. The condition of each rule may call other methods  
and so on, that is how you get the backward chaining funcionality. There  
may be some refactoring to avoid the same rules reproduced in several  
methods. If you can't follow me, buy a book on AI or find a sponsor (a  
big one) and hire a knowledge engineer (and take a lot of time to answer  
his questions).


Henk Verhoeven,

pk wrote:

Quoted text here. Click to load it

Re: Avoiding endless conditional statements

Thanks a lot.  It seems that expert systems are exactly what I was
looking for.  Now to find one that doesn't look horrible...

Re: Avoiding endless conditional statements

pk wrote:
Quoted text here. Click to load it
If you have one handy, my solution would be to us a small database with  
the 5 factors as the key attributes and the tool id as the sixth  
attribute. If that is not possible, a really large multidimensional  
array would do it, but performance issues need to be addressed. Perhaps  
a 2 dimensional table which gives the name of the appropriate include  
module which instantiates a 3 dimensional array which yields tool_id?

Re: Avoiding endless conditional statements

:) After looking at the work involved getting one of those expert
systems off the ground, I think I'll definitely be going the database
route.  I thought about a large multidimensional array, but ultimately
decided against it since it seemed to be overkill.

Re: Avoiding endless conditional statements

(This thread is definitely in the wrong group, but I have no way to
move it so I'll just continue on.)

Why isn't there a program that does just what I want it to?  I'm not
trying to whine here, and maybe it's just because I'm in the thick of
it here, but it seems to me that this could be a very useful little
tool.  It seems that with a little work, one could develop a
user-friendly program could take any table and create a simple GUI with
drop-down menus that would help the end-user make an informed decision.
 I know, I know..."If you want it so bad, make it!"  I'd like to say
that it'd be easy for me to do it, but the simple fact is that it
wouldn't be.  BUT there are many people out there who COULD do it
easily...is there really no demand for such a product?  Am I the only
one with this need?

Re: Avoiding endless conditional statements

Hi pk,

pk wrote:
It seems that with a little work, one could develop a
Quoted text here. Click to load it

Yes, is suppose a generic program could be built to show options given a  
big table of solutions and some choices one has already made. However,  
for most problems the number of solutions is simply too large. For  
example, a game of chess: There are only 32 pieces of 6 types on 64  
fields, and there is only a single starting position. But with all the  
effort that has been put into chess programs, no one has ever come up  
with a series of moves that is allways winning. The reason is that there  
are simply so many options that, with current computing power, you do  
not even get remotely close to a solution, even if you let hundreds of  
computers calculate for several years. The strategy to put all solutions  
into a big soluton table is simply not a viable strategy for most real  
ife problems. So why would someone make a nice GUI for using this strategy?

To put it an other way: suppose you have built this program and filled  
your solution table. Your customer is enthousiast and puts in in use.  
Soon someone finds a situation where the outcome of the program is  
clearly incorrect. You soon find out that there was another factor that  
was not taken into account. So your customer asks you to add this factor  
to the program. This  factor has 10 possible values. Here your table  
grows from, lets's say 10.000 to a 100.000 rows. If this happens only  
once, it may not be a problem. But if it is repeated a few times your  
database quickly runs out of control. Your customer may not want to be  
held liable for not taking into account a factor he nows to be relevant  
for a correct outcome. So he decides to take the entire program off line.

So in practice too many factors may render your program useless for the  
problem you are solving. Your customer may not understand this, becuase  
the number of factors to be taken into account only doubled, and he he  
is perfectly willing to pay you twice as much (but not a thousend times  
as much, of course). So if you go for the solution table, you better  
tell him in advance about the limitations, if you don't want to end up  
trying to explain this to a lawyer. (Building your own inference engine  
will be easier, i guess ;-) )


Henk Verhoeven,

Re: Avoiding endless conditional statements

pk wrote:
Quoted text here. Click to load it

Is there a constant in this problem? Can you find with a certain
"equation" the right tool for the input?

If not, you'll definitely have to enter the data somewhere. Each tool
depends on 5 factors. In a relational database, that would look like

[Tools]         [Factor1]    [Factor2]    ...
.tool_id        .fac1_id     .fac2_id     ...
.tool_name      .fac1_name   .fac2_name   ...


First, fill the factor and tool tables (for example, tool_id is 1 and
tool_name is 11.938.846) and then associate them in the data table.
When you search the database, you provide all the factor ids and you
get the corresponding line, fetch the tool_name and display it. That's
a really simple application of databases.

You can adapt this situation to many kind of databases. You could use
an xml file:

<tool id="1">

You search all tools, check their factors and if it matches, display
the name. At worst, use an array you filled manually:

echo data["fac1id"]["fac2id"]["name"];

Anyways, it says "all cutting data without guarantee", so you may make
a few mistakes :)


Re: Avoiding endless conditional statements

<26 Aug 2005 12:30:28 -0700>

Quoted text here. Click to load it

If I understand it correctly - you want something that some car websites  
have when buying a car on the internet .

- select model
- (another drop down box appears)
- select engine size
- (another drop down box appears)
- select colour

etc etc


Re: Avoiding endless conditional statements

Meaby you should try using masks. For example: you have 3 factors
1)Material Group
2)Insert Radius

and every factor have say 5 conditions wich would make many situations
=] situations. YOu start from giving every condition a number like 1,
3, 5, 7, 11.
Now imagine you factor 1) is 1+3+7=10 factor 2) is 1+11=12 3) is

Then you probalby will choose a 3D array[F1][F2][F3].


All you need now is to fill that array with solutions.

Then again I didn'd try that myself so I myght be wrong

Site Timeline