Skip to content

Instantly share code, notes, and snippets.

@naimurhasan
Last active February 17, 2025 13:48
Show Gist options
  • Save naimurhasan/91e000cb86161d1fcece54626f419e45 to your computer and use it in GitHub Desktop.
Save naimurhasan/91e000cb86161d1fcece54626f419e45 to your computer and use it in GitHub Desktop.

AI Concepts for Exam Preparation

Table of Contents

Genetic Algorithms (GA)

Definition

A Genetic Algorithm is an optimization and search technique based on the principles of natural selection and genetics. It is inspired by the process of evolution, where the fittest individuals are selected for reproduction to produce the next generation.

Process of GA

Genetic Algorithms work through the following steps:

  1. Initialization – Generate an initial population of possible solutions (chromosomes).
  2. Evaluation – Assess the fitness of each chromosome using a fitness function.
  3. Selection – Choose the best individuals for reproduction (e.g., roulette wheel selection, tournament selection).
  4. Crossover (Recombination) – Combine two parent chromosomes to create offspring.
  5. Mutation – Introduce small changes in the offspring to maintain diversity.
  6. Replacement – Replace the old population with new individuals.
  7. Termination – Stop when a stopping condition is met (e.g., a good enough solution is found).

Simple Example (Math)

Let's say we are trying to maximize the function:
[ f(x) = x^2 ]
where ( x ) is a binary string of length 4.

  1. Initial Population (Randomly Generated):

    Chromosome Decimal (x) f(x) = x²
    1010 10 100
    1100 12 144
    0110 6 36
    0011 3 9
  2. Selection: Choose the best two based on fitness: 1100 (144) and 1010 (100).

  3. Crossover (Single-point at middle):

    • Parent 1: 1100
    • Parent 2: 1010
    • Offspring: 1110, 1000
  4. Mutation: Randomly flip a bit (e.g., 1000 → 1001).

  5. New Generation:

    Chromosome Decimal (x) f(x) = x²
    1110 14 196
    1001 9 81

    The process repeats until an optimal solution is found.


Genetic Algorithm vs. Traditional Optimization

Feature Genetic Algorithm Traditional Optimization (e.g., Gradient Descent)
Search Method Population-based Single-point search
Flexibility Works with any function Requires differentiable function
Global vs Local Finds global optima May get stuck in local optima
Mutation Yes, introduces diversity No mutation mechanism
Example Use Machine learning, game AI Convex optimization, deep learning

Knowledge Representation

Definition

Knowledge Representation (KR) is a field of Artificial Intelligence (AI) that focuses on how knowledge can be stored, structured, and used in a way that allows machines to process and make intelligent decisions. It defines how information is represented in a format that a computer can understand.

Types of Knowledge Representation

There are four major ways to represent knowledge:

  1. Logical Representation – Uses formal logic (e.g., Propositional and First-Order Logic).

    • Example:
      • "If it rains, the ground will be wet."
      • ( \text{Rain} \rightarrow \text{WetGround} )
  2. Semantic Networks – A graph-based representation where nodes represent concepts and edges represent relationships.

    • Example:
      • A "Dog" is a "Mammal" → Dog → Mammal
      • "A dog has a tail" → Dog → has → Tail
  3. Frames – A data structure similar to object-oriented programming, where entities have attributes and values.

    • Example:
      • Object: Car
        • Color: Red
        • Speed: 120 km/h
        • Fuel: Petrol
  4. Production Rules – Uses "If-Then" rules to store knowledge.

    • Example:
      • Rule 1: IF the temperature is below 0°C, THEN water freezes.
      • Rule 2: IF a person has a fever, THEN suggest medicine.

Process of Knowledge Representation in AI

  1. Acquire Knowledge – Information is collected from experts, books, or databases.
  2. Represent Knowledge – The information is stored using one of the KR methods (Logic, Frames, Semantic Networks, or Rules).
  3. Inference Mechanism – AI applies reasoning (deduction, induction, abduction) to derive new knowledge.
  4. Update Knowledge – The system must adapt when new information is added.

Simple Example

Imagine an AI that determines whether an animal is a bird:

Feature Eagle Penguin Sparrow
Has Wings Yes Yes Yes
Can Fly Yes No Yes
Lives in Cold No Yes No
  • Production Rule Example:
    • IF an animal has wings AND can fly THEN it is a flying bird.
    • IF an animal has wings AND NOT can fly THEN it is a non-flying bird.

Using these rules, the AI can infer that an eagle and a sparrow are flying birds, while a penguin is a non-flying bird.


Knowledge Representation vs. Database Systems

Feature Knowledge Representation Database System
Focus Understanding and reasoning Storing and retrieving data
Structure Logical, semantic networks, frames, rules Tables, rows, columns
Processing Inference and decision-making Query execution
Example Use Expert systems, AI chatbots Banking, inventory management

Fuzzy Systems

Definition

A Fuzzy System is a mathematical framework used for dealing with uncertainty and imprecise information. It is based on Fuzzy Logic, which extends classical Boolean logic by allowing values between 0 and 1 instead of only true (1) and false (0). This helps in modeling real-world problems where decisions are not just black and white but have varying degrees of truth.

How Fuzzy Systems Work

A fuzzy system operates in the following steps:

  1. Fuzzification – Converts crisp (exact) inputs into fuzzy values using membership functions.
  2. Rule Evaluation (Inference Engine) – Uses "If-Then" rules to determine the fuzzy output.
  3. Defuzzification – Converts the fuzzy output back into a crisp value for decision-making.

Example: Fuzzy System for Temperature Control

Let's consider an air conditioning system that adjusts based on temperature.

1. Define Membership Functions:

  • Temperature Input:

    • Cold: ( 0 - 15^\circ C )
    • Warm: ( 10 - 30^\circ C )
    • Hot: ( 25 - 40^\circ C )
  • Fan Speed Output:

    • Slow: 0 - 30%
    • Medium: 20 - 70%
    • Fast: 60 - 100%

2. Rule Evaluation (Inference):

  • Rule 1: IF Temperature is Cold, THEN Fan Speed is Slow.
  • Rule 2: IF Temperature is Warm, THEN Fan Speed is Medium.
  • Rule 3: IF Temperature is Hot, THEN Fan Speed is Fast.

3. Defuzzification:

For a temperature of 27°C, it belongs partially to "Warm" and "Hot" categories. Using fuzzy logic, the system calculates the exact fan speed, say 65%, instead of just "medium" or "fast."


Fuzzy Logic vs. Classical Logic

Feature Classical Logic Fuzzy Logic
Values Strict 0 or 1 (True/False) Ranges between 0 and 1
Handling Uncertainty Not well-suited Handles gradual changes well
Example "Is the glass full? Yes/No" "The glass is 70% full"
Application Traditional computing AI, robotics, control systems

Applications of Fuzzy Systems

  • Control Systems: Air conditioners, washing machines, cruise control.
  • AI & Robotics: Decision-making in uncertain environments.
  • Medical Diagnosis: Diagnosing diseases based on symptoms.

Expert System

Definition

An Expert System is an AI-based computer system that mimics human expert decision-making. It uses knowledge and inference rules to solve complex problems in specific domains. Expert systems are widely used in fields like medicine, engineering, and business.


Components of an Expert System

  1. Knowledge Base – Contains facts and expert knowledge (rules, heuristics).
  2. Inference Engine – Applies logical reasoning to derive conclusions from the knowledge base.
  3. User Interface – Allows users to interact with the system and receive recommendations.
  4. Explanation System – Justifies decisions by explaining how conclusions were reached.

How an Expert System Works

  1. User Input – The system asks the user for relevant information.
  2. Inference Engine Processes the Knowledge Base
    • Uses IF-THEN rules to analyze input.
    • Uses reasoning methods like forward chaining or backward chaining.
  3. Output Generation – The system provides recommendations or solutions.
  4. Explanation – The system explains why it made that decision.

Example: Medical Diagnosis Expert System

Knowledge Base (Rules):

  • Rule 1: IF the patient has a fever AND cough, THEN they may have the flu.
  • Rule 2: IF the patient has high blood sugar, THEN they may have diabetes.
  • Rule 3: IF the patient has a fever AND sore throat, THEN they may have strep throat.

Inference Engine Process:

  1. User enters "fever and cough" as symptoms.
  2. The system matches this input with Rule 1.
  3. The system outputs: "The patient may have the flu."
  4. The system provides recommendations: "Take rest, drink fluids, and consider seeing a doctor."

Forward vs. Backward Chaining in Expert Systems

Method Process Example
Forward Chaining Starts with facts and applies rules to reach a conclusion. Given symptoms → Determine disease
Backward Chaining Starts with a goal and works backward to find supporting facts. Given a disease → Check if symptoms match

Expert System vs. Traditional Software

Feature Expert System Traditional Software
Purpose Mimics human decision-making Executes predefined instructions
Knowledge Base Uses rules and facts Uses hardcoded logic
Learning Ability Can be updated with new rules Requires reprogramming
Example Use Medical diagnosis, legal advice Payroll system, inventory management

Applications of Expert Systems

  • Medical Diagnosis (e.g., MYCIN for bacterial infections)
  • Business Decision Support
  • Engineering Troubleshooting
  • Fraud Detection in Banking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment