Share via


Bits & Bytes: Lesson 4

February 2007

Computer IQ

Making Computers Smart

Do you recall this conversation from the film, 2001: A Space Odyssey?

Hal, the spacecraft computer, killed a crew member by throwing him into space when the crewman went outside to replace the computer unit. Dave, the hero of the film, has just discovered that Hall has also killed the rest of the crew as they slept.

(Dave) "Open the pod bay doors, Hal. Hello Hal, do you read me? Do you read me, Hal? Hello Hal, do you read me? Open the pod bay doors, Hal".

(Hal) "I’m sorry, Dave. I can't do that, Dave ...the mission is too important for me to allow you to jeopardize it".

(Dave) "Ok Hal, I’ll go in through the emergency airlock".

(Hal) "Just what do you think your doing, Dave? I know everything hasn’t been quite right with me, but I can assure you it’s going to be alright again. I feel much better now, I really do."

(Hal) "Look Dave, I can see you are really upset about this. I know I’ve made some very poor decisions lately. I want to help you. Stop, Dave… will you stop, Dave? I’m afraid, Dave..."

This film, plus many before and after, fueled our love-fear relationship with smart machines. While machines like Hal, capable of independent thought and emotions, are science fiction, artificial intelligence is currently combining computer science and engineering to create machines that exhibit intelligent behavior, learn useful skills, and adapt to new situations. These machines are embedded within many everyday appliances and tools in our homes, hospitals, factories, businesses, government agencies, and research facilities. Whole new fields of science and business domains are developing as new applications are invented for the smart computers behind these indispensible machines. And it’s all based upon some rather basic programming concepts which you will come to realize in this lesson, are very human.

AI and Expert Systems

Conventional Artificial Intelligence (AI) is the science of creating computers that mimic what humans refer to as intelligence—the ability to process information and respond appropriately. AI is supported by several types of machine learning technologies including expert systems which apply statistical and reasoning capabilities to huge amounts of data in order to draw conclusions and solve problems.

Expert system - Computer programs that answer questions and draw conclusions by applying statistical rules and logic.

The goal of expert systems is to bring the knowledge of experts to the hands of decision makers who need accurate answers quickly. Computer expert systems are sometimes called knowledge based systems and have been in use since the 1960’s. They are created by first gathering the knowledge of human experts on some specific, narrow field of knowledge. Then, a knowledge engineer organizes the information with rules that help to focus the knowledge toward solving specific problems.

The childhood guessing game in which one child thinks of an animal, and the other child tries to guess it by asking only yes-or-no questions, simulates an expert system. A process of elimination based upon rules guides a series of questions until the correct animal name is guessed. It might go something like this:

      “Does it have fur?”

       “No”

       “Does it have feathers?”

       “Yes”

       “Can it fly?”

       “No”

       “Is it black and white?”

       “Yes”

       “Is it a penguin?”

       “Yes”

Expert systems contain information supplied by experts on a single topic and usually involve a “dialog” with the user. This problem-solving system relies upon rules that guide the dialog toward the discovery of the solution that fits all of the responses given by the user. A recent addition to expert systems is the inclusion of “fuzzy logic” which doesn’t rely on definite yes-or-no answers, but rather on conditions of “mostly true” or “mostly false.” These less definite answers result in more realistic “most likely” conclusions.

Fuzzy Logic - A system that relies upon “mostly true” and “mostly false” conditions to arrive at more realistic “most likely” conclusions.

Expert systems are employed quite commonly in:

  • Wizards that help you install software

  • Artificial intelligence of computer games

  • Diagnostic programs for everything from identifying skin diseases to getting help with your computer system

  • Decision making in business, science, engineering, and the military

Computational Intelligence and Artificial Neural Networks

A specialized area of artificial intelligence called computational intelligence includes the application of artificial neural networks. When groups of processing units—sometimes referred to as nodes—are interconnected in a fashion resembling the neurons in the brain, an artificial neural network can be constructed. An artificial neural network “learns” by observing complex relationships between inputs and outputs and inferring from observations. This is particularly useful in applications where the complexity of the data or the difficulty of the task makes traditional program design extremely complicated or impractical. Artificial neural networks are especially useful in tasks such as:

Neural network - A collection of interconnected processing units which resemble brain neurons and are capable of complex analysis of data.

  • Pattern recognition in military radar systems

  • Face identification in Homeland Security operations

  • Sequence recognition in gesture, speech, writing

  • Medical diagnosis with EMR technology

  • Detection of abnormal charges on credit cards

  • Data mining or knowledge discovery in databases

  • Identification of e-mail spam

Bb330929.41af21de-bed8-4c36-bace-c2262e38da13(en-US,VS.80).png

Robotics and Artificial Intelligence

"The problem with most robots is that they tend to be, well, robotic. They know nothing they aren't programmed to know, and can do nothing they aren't programmed to do. But for many applications where robots could be useful, they need to be more like humans, able to respond as a cooperative partner rather than a mindless machine.”

—Scientific American Frontiers, April 13, 2005 . Alan Alda, host

Today’s robots are simply machines with enough AI to do useful tasks. And the tasks run the gamut from entertainment in the form of Aibo, and the robotic pet, to Roomba, the robotic vacuum cleaner, to Penelope, the surgical assistant. Robots come in all shapes and sizes. Depending upon how you define the word “robot,” you could be talking about the “smart wiper” on your car windshield that is “just a robotic arm” or a machine the size of a vehicle such as the Mars Rover or the trucks in the DARPA Challenge road race.

It appears that robots will definitely be part of our future of smart machines. Analysts estimate that about 4 million Roomba vacuum robots will be in homes by 2007 and some people in Japan foresee 39 million household robots in use by the end of the decade. Add to that the 1.1 million industrial robots in the U.S. and you have a robot population greater than the population of California or Spain!

Research and development in AI and robots includes such projects as:

  • Systems for surgical assistance

  • Solar-powered underwater vehicles

  • Robotic assistants for the elderly

  • Space exploration rovers

  • Bomb-sniffing military robotics

  • Childcare workers

Since the earliest days of computers and AI, great debate has raged over a definition of intelligence and creativity, whether or not a machine can actually understand and be intelligent, and even, if the distinction between human and machine will eventually disappear. Various schemes to distinguish between computer and human intelligence and creativity have appeared over the past several decades. Perhaps you have heard of the Turing Test in which a judge asks questions to an entity in another room. If at some point the judge cannot tell if the answers are coming from a person or a computer, the computer is declared intelligent. And of course, we’ve all heard of the famous computer versus human chess games. While philosophers will continue to explore the distinctions between humans and machines, computer scientists will forge ahead with research and developments of more and more amazing machines. Ultimately, computer IQ comes down to machines following the directions given to it by programmers and engineers.

Get Real

  1. Explore an expert system by visiting The Whale Watcher at https://www.aiinc.ca/demos/whale.html. Does this system employ strict true/false logic or fuzzy logic?

  2. Review the basic characteristics of expert systems, fuzzy logic, neural networks, and robots. Match the tasks on the left with the technology on the left that would best be able to accomplish the task described.

Task to accomplish

Technology

____ Discover the most likely cause of a panic attack

A. Expert systems

____ Identify wild flowers seen on a hike

B. Fuzzy logic

____ Inspect mines for dangerous gases

C. Neural networks

____ Uncover forged artwork

D. Robotics

Here are some possible answers:

  1. After identifying several hypothetical sighting of whales, it appears that this system employs strict true/false logic. All of the response choices are yes/no or either/or.

Task to accomplish

Technology

B, Discover the most likely cause of a panic attack

A. Expert systems

A, Identify wild flowers seen on a hike

B. Fuzzy logic

D, Inspect mines for dangerous gases

C. Neural networks

C, Uncover forged artwork

D. Robotics

Teaching Computers to Make Decisions

Computers are “taught” to display intelligence by making decisions through various coding techniques. Collectively these techniques are called “selection structures”; the lines of code instruct the computer to “select” between various options based upon particular conditions.

If-Statements

The most basic selection structure is the if-statement. In the real-world we process if-statements so often and with such speed that we barely give it any thought at all. You have likely experienced these opportunities to process an “if” decision:

      If it’s raining outside, you grab an umbrella.

      If steak is on sale, you buy it.

      If the traffic signal is red, you stop.

      If you see a friend, you shout, “Hi!”

If-statement - A computer instruction that decides upon an action based upon the evaluation of a condition.

In all of these situations, and in dozens more in every hour of every day, you respond to a situation or condition with some action that uniquely fits the specific circumstances. Obviously, we learned the correct responses to unique situations from modeling, training, and trial-and-error. Computers don’t learn quite as easily as humans…or even as easily as dogs for that matter! Computers must be instructed to respond correctly with very specific instructions.

The if-statement is used in computer programs to instruct the computer to carry out specific actions in response to specific conditions.

The if-statement looks like this in pseudo-code:

**      If*** <some condition is true> then *<do some action>

Think about this situation in a word processing program:

You are writing a letter to your friend, Sara. In order to capitalize Sara’s name you press the shift key while pressing the ‘s’ key. The computer interprets your action like this:

If the shift key is down, then print on the screen the uppercase version of the letter key being pressed at the same time.

Comparison Operators

Sometimes we must evaluate the situation we are experiencing with words like “greater than” or “less than or equal to.”

You might have experienced the need to use these comparisons when writing a check. You likely thought to yourself, “If my checkbook balance is “greater than” the amount of this item, then I can buy it.” Or you might have thought about the situation in reverse, “If the amount of this item is “less than or equal to” my checkbook balance, then I can buy it.

We use these comparisons quite automatically, but the computer must be taught the logic very explicitly. Special symbols, called comparison operators, are used in computer programming to compare values.

Comparison operators - Symbols used to compare the relationship between values such as greater than, less than, equal, or combinations of these relationships.

This is what the purchasing scenario would look like in pseudo-code:

      If checkbook balance > price then buy item

      Or

      If price <= checkbook balance then buy item

You might be familiar with some of these symbols from your experiences in mathematics. These are the comparison operators commonly used in computer programs:

Operator

Description

Example

<

Less than

Height < 72

<=

Less than or equal

Age <=16

>

Greater than

Size > 32

>=

Greater than or equal

Length >= 21

==

Equal

Temperature ==32

!=

Not equal

Balance != 0

You might not recognize the last two symbols for equal and not equal. The “==”symbol instructs the computer to check if the values on each side of the symbol are equivalent. This is different than the assignment operator (=) that we learned about in Lesson 3 of this series which stores a value in a variable.

Logical Operators

Sometimes a situation requires more than one comparison of values to arrive at an appropriate response. In the game of volleyball, a team wins if it has scored at least 15 points and has at least 2 more points than the opponent. So, if the Cougars have 15 points and the Vikings have 14 points, no one has yet won and the game is not over. If the Cougars win one more point for a total of 16 points and the Vikings still have 14 points, the game is over and the Cougars win. Putting this logic into a computer requires the use of logical operators. With logical operators, computer programmers (and the rest of us) create powerful comparisons which fine tune the exactness of problem solving.

In pseudo-code the volleyball scoring comparison would look like this:

      If team A score >= 15

      and team A score >= Team B score +2

      then winner = Team A.

You constantly use logical operators in your daily life. When you get dressed in the morning you probably check the weather. If the day is going to be cool you take a light jacket. So when you listen to the weather report, you likely say to yourself, “If the temperature is greater than 40° but less than 70°, I’ll take a light jacket.”

Logical Operators - Symbols used to combine the logic of comparison operators into more complex comparisons.

Deciding to take a jacket that would look like this in pseudo-code:

      If temperature > 40 and temperature < 70 then garment = light jacket

Special symbols are used to express this logic in computer programs:

Operator

Description

Example

&&

AND

Both conditions must be true to make the whole expression true.

(snow >=4) && (temperature <= 0)

||

OR

At least one of the conditions must be true to make the whole expression true.

(age >= 21) || (gender = male)

!

NOT

Changes a true expression to false and a false expression to true.

!(on)

where on is a Boolean (true/false) variable

HINT: Avoid confusion over the exact meanings of “&&” or “||” by thinking about each like this:

When you see “&&” say to yourself, “Both condition 1 and condition 2 must be true in order for the action to be carried out.”

When you see “||” say to yourself, “Either condition 1 or condition 2 must be true in order for the action to be carried out.”

Logical operators are very powerful for programming “almost” conditions. Sometimes computers evaluate when a situation is “almost” at a critical point. Your home thermostat operates on this principal of “almost.” If you set the thermostat at 68° you want the heater to turn on when the room temperature falls to “almost” 68° so that by the time the burner is hot and the fan is ready to kick in, the room temperature hasn’t fallen too much below 68°. You also want the heater to turn off a bit above 68° so that when the fan finally turns off, the room is within a comfortable range. If the condition for turning the furnace on and off is exactly 68°, the furnace will continually turn on and off and the room will never be very comfortable.

Comparing Objects

For a moment, think back to Lesson 2 where you learned about object-oriented design and programming. You will recall that objects have properties and methods. Properties are the objects characteristics and methods are their behaviors. Because of the manner in which an object is stored in memory, a direct comparison between objects is not usually meaningful when you want to evaluate if one object has the exact same property values as another object. You can not write a pseudo-code statement like this:

      If myPetObject == yourPetObject then declare them twins

To evaluate if any given property or all of the properties of two objects are the same a special method must be written into the class. The name of the method is always compareTo. Within this method, the programmer defines what it means for two objects to be equal dependent upon the values of their respective properties. Based upon the goals for the program, it might mean that two pets are “equal” if they share the same parents and birthday. In another program it might mean the two pets are equal if they are of the same breed and similar weight. The point is that comparing objects for equality, or with any of the comparison operators, is very different than comparing other data types. You will learn more about the specifics of comparing objects when you learn a specific computer programming language.

compareTo - The name given to a class method for comparing the properties of objects.

If-then-else

For the wide range of decision-making we expect of computers, several other comparison strategies are necessary. In addition to the basic if-statement, programmers can use an if-else statement. This statement adds an additional level of sophistication with which you are already very familiar. It’s rare that you are faced with a situation for which there is only a single possible reaction. Let’s revisit the situations from earlier in this lesson and add more realistic outcomes.

      If it’s raining outside, grab an umbrella, else grab sunglasses.

      If steak is on sale, buy it, else buy hotdogs.

      If the traffic signal is red, stop your car, else proceed with caution.

      If you meet a friend on the street, shout “Hi!”, else smile and walk on.

Computers commonly use if-else logic. If you fail to click “OK” in some dialog boxes, the computer beeps a warning. The pseudo-code for this situation might look like this:

If* reply = “OK” then close dialog box else *beep

You will encounter many if-else scenarios while shopping online. Sometimes the price for an item depends upon a special coupon code or membership in an organization. Renting an automobile definitely falls into this category. If you belong to the American Automobile Association (AAA) you might get discount on a car rental. The pseudo-code for a car rental by an AAA member might look like this:

If* membership == yes then price = 72 else *price = 80

If-else-if ladder

Another selection strategy is called an if-else ladder. In this technique, only one outcome action is allowed. As soon as a condition is found to be true on one “rung” of the ladder, the process of evaluating conditions stops. It looks like this in pseudo-code:

      If temperature >= 90 then get ice cream

      else if temperature >= 70 then go to the beach

      else if temperature >= 50 then go to the movies

      else get hot chocolate

The trick in understanding this type of logic is to remember:

  • You start at the top

  • Only one action can occur

  • Once a selection based upon the temperature is made, the process stops

Bb330929.ab3b034d-d457-41ec-b997-3ad6419c8a66(en-US,VS.80).png

Try your hand at the process with this example: What happens if the temperature is 60°?

      Is the temperature >= 90? No. Go to the next rung of the ladder.

      Is the temperature >= 70? No. Go to the next rung of the ladder.

      Is the temperature >= 50? Yes. Go to the movies.

      Stop the selection process.

Complex modern problems can be solved with combinations of comparison and logical operators. The power of the computer is that it can process the complexity at blinding speed and remember the multitude of possibilities far beyond our human capability. Whether in a chess match, a flight reservation system, or weather predictions, the if statement is a key component. There are many more variations of the “if” selection strategy to discover when you begin learning a specific programming language. The general rule is to always choose the most efficient strategy available to fit the goals of the problem.

Get Real

  1. Practice using comparison operators by writing if-statements pseudo-code for these situations.

    1. To receive a passing grade, a student must earn a grade of 60 percent or better.

    2. A gas credit card will allow no more than a $75 charge.

    3. The first 200 customers receive a 10% discount on merchandise purchased.

  2. Determine the result of the following logical statements giving the following facts.

    Age = 20, gender = male, traffic tickets = 2, vehicle = car

    1. Age <= 21 && gender == male

    2. Traffic tickets > 2 && vehicle == motorcycle

    3. Gender == female || vehicle == car

    4. Tickets <=2 || gender == female

    5. Vehicle != truck && age >= 30

  3. Imagine playing the game “Rock, Paper, Scissors.” Write the if-else pseudo-code for all of the possible outcomes of one round of the game. The first one is completed for you. How many are required to evaluate all of the possible outcomes?

    If player 1 == “Rock” and player 2 == “Scissors” then player 1 wins.

  4. Consider this if-else-if ladder of directions for cooking meat.

    If temperature >= 170 then the meat is well done

    else if temperature >= 165 then the meat is medium-well

    else if temperature >= 160 then the meat is medium

    else if temperature >= 145 then the meat is medium-rare

    else if temperature >= 140 then the meat is rare

    else the meat is not ready to eat

    Classify meat prepared to these temperatures:

    1. 150

    2. 135

    3. 162

    4. 173

Your answers will likely be similar to these:

    1. If grade>= 60 then student passes

    2. If charge == 75 then turn off the pump

    3. If customer number <= 200 then discount = 10%

    1. True

    2. False

    3. False

    4. False

    5. True

  1. If player 1 == “Rock” and player 2 == “Scissors” then player 1 wins.

    If player 1 == “Rock” and player 2 == “Paper” then player 2 wins.

    If player 1 == “Rock” and player 2 == “Rock” then it’s a tie.

    If player 1 == “Paper” and player 2 == “Scissors” then player 2 wins.

    If player 1 == “Paper” and player 2 == “Rock” then player 1 wins.

    If player 1 == “Paper” and player 2 == “Paper” then it’s a tie.

    If player 1 == “Scissors” and player 2 == “Paper” then player 1 wins.

    If player 1 == “Scissors” and player 2 == “Rock” then player 2 wins.

    If player 1 == “Scissors” and player 2 == “Scissors” then it’s a tie.

    1. 150 medium-rare

    2. 135 not ready to eat

    3. 162 medium

    4. 173 well-done

Play it again, Sam

The repetition of sequences in technology is a familiar experience for technology users.

  • Remember back to the last time a program or a website required a password that you just couldn’t recall. The dialog box accepted your password attempt over and over and when you failed on the fifth try, it closed you out. How does that happen?

  • Picture the little “busy” icon that cycles through three or four graphics, on and on, until a task is completed. How does that happen?

  • Remember (as if you could forget this one!) the background music on some website that plays but never ends. How does that happen?

All of these scenarios are the result of program looping strategies called repetition. Repetition provides the power to create programs that can flow in smooth, natural sequences of computer/human interaction. The computer can be told to repeat a set number of times as in the password situation, repeat until something happens as in the “busy” icon scenario, or to repeat forever, as in the background music scenario. Each of these situations is the result of specific programming strategies. Let’s look at each one a bit more closely.

Repetition - A programming strategy that repeats a set of actions a specified number of times, until a condition is met, or without end.

Counter-controlled Loops

The password example from above, uses what is called a counter-controlled loop. Basically, the computer is told to repeat something a specific number of times. This type of loop is used when the number of repetitions is known before the action begins. In this password example, there is an additional feature that causes the program to escape the repetition if the password is entered correctly. You might have experienced counter-controlled loops in the animation or the sound effects of video games; the rabbit jumps three times for each correct answer or the sound of a cannon is exactly bang—bang—bang—bang. Sending two copies of a letter to your printer creates a counter-controlled loop also. While the exact syntax of each of these repetition loops is dependent upon the specific programming language being used, the pseudo-code can be generalized in order for you to understand the instructions given to a computer. The pseudo-code for a counter-controlled loop looks like this:

Repeat* *cannon sound 4 times

Sentinel-controlled Loops

The changing “busy” icon is a sentinel-controlled loop. The word “sentinel” in this situation means “controller.” The loop that shows each of three or four slightly changed icons continues until something occurs to make it stop. In this situation, the sentinel is the completion of the task being performed by the computer. You encounter sentinel-controlled loops in video games when the “Game Over” message is displayed after you earn a set number of points, you are destroyed by the enemy, or you run out of time. The program used by a credit card company might contain a sentinel loop; you will continue to receive a bill until the balance due is zero.

The pseudo-code for a sentinel-controlled loop looks like this:

Until* the balance == 0 repeat *sending a bill

Forever Loops

The annoying background music is the result of a “forever” loop—sometimes called an “infinite” loop. In addition to being annoying, “forever” loops are generally considered poor programming style and should be avoided. Amateur web designers sometimes use a “forever” loop to animate little mailboxes or silly smiley faces. Ugh!

The pseudo-code of a forever loop looks like this:

Repeat* *playing annoying music

Notice that there is nothing to stop the action in this type of repetition.

Get Real

  1. Study the scenarios below and identify the repetition strategy you would use to solve the problem. Explain your answer.

    1. A text finding feature in Word.

    2. A banner message on a webpage.

    3. A packaging robot that puts 10 candy bars in a package.

    4. The gate on a $2.00 toll booth

    5. The noise of your car alarm.

    6. A ringing, but unanswered telephone.

Here are some possible answers:

    1. The repetition strategy used is a sentinel-controlled loop because it continues to search until is reaches the end of the document.

    2. This is likely a forever loop because it typically goes on and on.

    3. This robot uses a counter-controlled loop to place exactly 10 bars in a package every time.

    4. This is another sentinel-controlled loop. The gate is not raised until you have deposited exactly $2.00.

    5. If your car alarm is like mine it sounds for about 8 or 9 repetitions before stopping or until I turn the key. That would make it a counter-controlled loop with a sentinel-controlled condition. If it sounds forever, you would likely return to a dead battery or at least angry neighbors!

    6. My telephone works with a counter-controlled loop. The caller is asked to leave a message after 4 rings. This is one of the counter-controlled loops I control through programming my own phone.

Summary

Artificial Intelligence (AI) is the science of creating computers that mimic human abilities to process information and respond appropriately in given situations. Expert systems and neural networks are two forms of AI. Expert systems contain information supplied by experts on a single topic and usually involve a “dialog” with the user that relies upon true/false rules to identify the correct answer. “Fuzzy logic” evaluates conditions of “mostly true” or “mostly false” to determine the “most likely” correct answer. Neural networks are formed from interconnected processing units which resemble the structure of brain neurons. They are particularly useful in applications that analyze complex data. Robotic technology employs AI strategies and is increasing being used to perform a wide variety of tasks that are either difficult or dangerous for humans.

The “intelligence” of computers is the result of programming and engineering. The basic programming strategies which create machine intelligence are selection structures and repetition. Selection structures include variations of if-statements which direct the computer to select the appropriate action for specific conditions. The most basic if-statements take the form of:

  • If

  • If-else

  • If-else ladders

If-statements rely upon comparison and logical operators in order to evaluate conditions. Comparison operators include:

  • < (less than)

  • <= (less than or equal)

  • > (greater than)

  • >= (greater than or equal)

  • == (equal)

  • != (not equal)

Logical operators combine and clarify comparisons with these symbols:

  • && (and)

  • || (or)

  • ! (not)

Repetition strategies are used to repeat actions within a program. The basic repetition formats include:

  • Counter-controlled loops which repeat a set number of times.

  • Sentinel-controlled loops which repeat until some condition occurs.

  • Forever loops which never stop repeating.

Join us for Lesson 5 of Bits and Bytes when we explore how Graphic User Interfaces enable programmers to create dynamic and user-friendly computer experiences.