Java Code for an Example Help Desk

createTestFactsWorkingMemory workingMemory throws Exception { Problem p1 = new Problem101, Problem.REFRIGERATOR; p1.problemType = Problem.NOT_RUNNING; p1.environmentalData = Problem.CIRCUIT_BREAKER_OFF; workingMemory.insertp1; Problem p2 = new Problem101, Problem.TV; p2.problemType = Problem.SMOKING; workingMemory.insertp2; } In this code we created new instances of the class P roblem and set desired at- tributes. We then use the W orkingM emory method insert to add the ojects to the working memory collection that Drools maintains. The output when running this example is reformatted to fit the page width: From rule ’Print all problems’: [Problem: TV problem type: SMOKING environmental data: NONE] From rule ’Print all problems’: [Problem: REFRIGERATOR problem type: NOT_RUNNING environmental data: CIRCUIT_BREAKER_OFF] Unplug appliance to prevent fire danger: [Problem: TV problem type: SMOKING environmental data: NONE] Check for power source: [Problem: REFRIGERATOR problem type: NOT_RUNNING environmental data: CIRCUIT_BREAKER_OFF] The unit is not is not on and the circuit breaker is tripped - check the circuit breaker for this room. 96 Systems It may seem like rule-based expert systems have a lot of programming overhead; that is, it will seem excessively difficult to solve simple problems using production systems. However, for encoding large ill-structured problems, production systems provide a convenient notation for collecting together what would otherwise be too large a collection of unstructured data and heuristic rules Programming Expert Sys- tems in Ops5: An Introduction to Rule-Based Programming , Brownston et al. 1985. As a programming technique, writing rule-based expert systems is not for everyone. Some programmers find rule-based programming to be cumbersome, while others find it a good fit for solving some types of problems. I encourage the reader to have some fun experimenting with Drools, both with the examples in this chapter, and the many examples in the Drools distribution package and documentation. Before starting a moderate or large expert system project, there are several steps that I recommend: • Write a detailed description of the problem to be solved. • Decide what structured data elements best describe the problem space. • Try to break down the problem into separate modules of rules; if possible, try to develop and test these smaller modules independently, preferably one source file per module. • Plan on writing specific rules that test parts of the system by initializing work- ing memory for specific tests for the various modules; these tests will be very important when testing all of the modules together because tests that work correctly for a single module may fail when all modules are loaded due to unexpected rule interactions. Production systems model fairly accurately the stimulus-response behavior in peo- ple. The left-hand side LHS terms represent environmental data that triggers a response or action represented by the right-hand side RHS terms in production rules. Simple stimulus-response types of production rules might be adequate for modeling simple behaviors, but our goal in writing expert systems is to encode deep knowledge and the ability to make complex decisions in a very narrow or limited problem domain. In order to model complex decision-making abilities, we also of- ten need to add higher-level control functionality to expert systems. This higher level, or meta control, can be the control of which rule modules are active. We did not look at the Drools APIs for managing modules in this chapter but these APIs are covered in the Drools documentation. Hopefully, this chapter both gave you a quick- start for experimenting with Drools and enough experience to know if a rule-based system might be a good fit for your own development. 97 98 6 Genetic Algorithms Genetic Algorithms GAs are computer simulations to evolve a population of chro- mosomes that contain at least some very fit individuals. Fitness is specified by a fitness function that rates each individual in the population. Setting up a GA simulation is fairly easy: we need to represent or encode the state of a system in a chromosome that is usually implemented as a set of bits. GA is basically a search operation: searching for a good solution to a problem where the solution is a very fit chromosome. The programming technique of using GA is useful for AI systems that must adapt to changing conditions because “re-programming” can be as simple as defining a new fitness function and re-running the simulation. An advantage of GA is that the search process will not often “get stuck” in local minimum because the genetic crossover process produces radically different chro- mosomes in new generations while occasional mutations flipping a random bit in a chromosome cause small changes. Another aspect of GA is supporting the evo- lutionary concept of “survival of the fittest”: by using the fitness function we will preferentially “breed” chromosomes with higher fitness values. It is interesting to compare how GAs are trained with how we train neural networks Chapter 7. We need to manually “supervise” the training process: for GAs we need to supply a fitness function and for the two neural network models used in Chapter 7 we need to supply training data with desired sample outputs for sample inputs.

6.1 Theory

GAs are typically used to search very large and possibly very high dimensional search spaces. If we want to find a solution as a single point in an N dimensional space where a fitness function has a near maximum value, then we have N param- eters to encode in each chromosome. In this chapter we will be solving a simple problem that is one-dimensional so we only need to encode a single number a float- ing point number for this example in each chromosome. Using a GA toolkit, like the one developed in Section 6.2, requires two problem-specific customizations: • Characterize the search space by a set of parameters that can be encoded in a chromosome more on this later. GAs work with the coding of a parameter set, not the parameters themselves Genetic Algorithms in Search, Optimiza- 99 Figure 6.1: The test function evaluated over the interval [0.0, 10.0]. The maximum value of 0.56 occurs at x=3.8 tion, and Machine Learning , David Goldberg, 1989. • Provide a numeric fitness function that allows us to rate the fitness of each chromosome in a population. We will use these fitness values to determine which chromosomes in the population are most likely to survive and repro- duce using genetic crossover and mutation operations. The GA toolkit developed in this chapter treats genes as a single bit; while you can consider a gene to be an arbitrary data structure, the approach of using single bit genes and specifying the number of genes or bits in a chromosome is very flexible. A population is a set of chromosomes. A generation is defined as one reproductive cycle of replacing some elements of the chromosome population with new chromosomes produced by using a genetic crossover operation followed by optionally mutating a few chromosomes in the population. We will describe a simple example problem in this section, write a general purpose library in Section 6.2, and finish the chapter in Section 6.3 by solving the problem posed in this section. For a sample problem, suppose that we want to find the maximum value of the function F with one independent variable x in Equation 6.1 and as seen in Figure 6.1: F x = sinx ∗ sin0.4 ∗ x ∗ sin3 ∗ x 6.1 The problem that we want to solve is finding a good value of x to find a near to possible maximum value of F x. To be clear: we encode a floating point number 100