Artificial Intelligence (AI) is the simulation of human intelligence in machines. It encompasses learning, reasoning, problem-solving, perception, and language understanding.
class SimpleReflexAgent:
def __init__(self, rules):
self.rules = rules
def perceive_and_act(self, percept):
# Simple rule-based decision making
for condition, action in self.rules:
if condition(percept):
return action
return None
class Problem:
def __init__(self, initial_state, goal_state):
self.initial_state = initial_state
self.goal_state = goal_state
def actions(self, state):
# Return possible actions from state
pass
def result(self, state, action):
# Return new state after action
pass
def goal_test(self, state):
return state == self.goal_state
from collections import deque
def bfs(graph, start, goal):
queue = deque([(start, [start])])
visited = set()
while queue:
node, path = queue.popleft()
if node == goal:
return path
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
queue.append((neighbor, path + [neighbor]))
return None
import heapq
def a_star(graph, start, goal, heuristic):
frontier = [(0, start, [start])]
visited = set()
while frontier:
cost, node, path = heapq.heappop(frontier)
if node == goal:
return path
if node not in visited:
visited.add(node)
for neighbor, edge_cost in graph[node]:
new_cost = cost + edge_cost
f = new_cost + heuristic(neighbor, goal)
heapq.heappush(frontier, (f, neighbor, path + [neighbor]))
return None
def minimax(node, depth, alpha, beta, maximizing):
if depth == 0 or is_terminal(node):
return evaluate(node)
if maximizing:
value = float('-inf')
for child in get_children(node):
value = max(value, minimax(child, depth-1, alpha, beta, False))
alpha = max(alpha, value)
if alpha >= beta:
break # Beta cutoff
return value
else:
value = float('inf')
for child in get_children(node):
value = min(value, minimax(child, depth-1, alpha, beta, True))
beta = min(beta, value)
if alpha >= beta:
break # Alpha cutoff
return value
# Prisoner's Dilemma payoff matrix
payoff_matrix = {
('Cooperate', 'Cooperate'): (-1, -1),
('Cooperate', 'Defect'): (-10, 0),
('Defect', 'Cooperate'): (0, -10),
('Defect', 'Defect'): (-5, -5)
}
def nash_equilibrium(payoffs):
# Find strategies where no player can improve
# This is a simplified version
return 'Defect', 'Defect' # Both defect is Nash equilibrium
# Semantic Network representation
class SemanticNetwork:
def __init__(self):
self.nodes = {}
self.relations = {}
def add_concept(self, concept, properties):
self.nodes[concept] = properties
def add_relation(self, concept1, relation, concept2):
if concept1 not in self.relations:
self.relations[concept1] = []
self.relations[concept1].append((relation, concept2))
# Example: "Birds can fly"
network = SemanticNetwork()
network.add_concept("Bird", {"type": "animal", "has_wings": True})
network.add_relation("Bird", "can", "fly")
# Truth table for AND operator
def truth_table_and():
print("A\tB\tA AND B")
print("-" * 20)
for a in [True, False]:
for b in [True, False]:
result = a and b
print(f"{a}\t{b}\t{result}")
# Logical inference
def modus_ponens(premise1, premise2):
# If P → Q and P, then Q
if premise1 and premise2:
return True
return False
# First-order logic example
# ∀x (Student(x) → Studies(x))
# ∃x (Student(x) ∧ Smart(x))
class FirstOrderLogic:
def __init__(self):
self.domain = set()
self.predicates = {}
def add_predicate(self, name, arity):
self.predicates[name] = arity
def evaluate(self, formula, interpretation):
# Simplified evaluation
if formula.startswith("∀"):
return self.universal_quantifier(formula, interpretation)
elif formula.startswith("∃"):
return self.existential_quantifier(formula, interpretation)
return True
# Example: "All students study"
fol = FirstOrderLogic()
fol.add_predicate("Student", 1)
fol.add_predicate("Studies", 1)
# Forward chaining example
knowledge_base = [
("bird", "can_fly"),
("penguin", "bird"),
("penguin", "cannot_fly")
]
def forward_chain(facts, rules):
new_facts = set(facts)
changed = True
while changed:
changed = False
for rule in rules:
if all(premise in new_facts for premise in rule[0]):
if rule[1] not in new_facts:
new_facts.add(rule[1])
changed = True
return new_facts
# STRIPS-style action representation
class Action:
def __init__(self, name, preconditions, effects, cost=1):
self.name = name
self.preconditions = set(preconditions)
self.effects = set(effects)
self.cost = cost
# Example: Move action
move_action = Action(
name="move",
preconditions=["at_location_A", "has_energy"],
effects=["at_location_B", "not_at_location_A"],
cost=1
)
def is_applicable(action, state):
return action.preconditions.issubset(state)
# Map coloring CSP
variables = ['WA', 'NT', 'SA', 'Q', 'NSW', 'V', 'T']
domains = {var: ['red', 'green', 'blue'] for var in variables}
constraints = [
('WA', 'NT'), ('WA', 'SA'), ('NT', 'SA'), ('NT', 'Q'),
('SA', 'Q'), ('SA', 'NSW'), ('SA', 'V'), ('Q', 'NSW'),
('NSW', 'V')
]
def is_consistent(assignment, var, value):
for neighbor in [c[1] for c in constraints if c[0] == var]:
if neighbor in assignment and assignment[neighbor] == value:
return False
return True
import numpy as np
from sklearn.linear_model import LinearRegression
# Simple ML example
X = np.array([[1], [2], [3], [4], [5]]) # Features
y = np.array([2, 4, 6, 8, 10]) # Labels
model = LinearRegression()
model.fit(X, y) # Training
prediction = model.predict([[6]]) # Prediction
print(f"Prediction for input 6: {prediction[0]}")
from sklearn.cluster import KMeans
from sklearn.ensemble import RandomForestClassifier
# Supervised Learning
X_train = [[1, 2], [2, 3], [3, 4], [4, 5]]
y_train = [0, 0, 1, 1] # Labels
classifier = RandomForestClassifier()
classifier.fit(X_train, y_train)
# Unsupervised Learning
X = [[1, 2], [2, 3], [8, 9], [9, 10]]
kmeans = KMeans(n_clusters=2)
clusters = kmeans.fit_predict(X)
import numpy as np
class SimpleNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.W1 = np.random.randn(input_size, hidden_size) * 0.01
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.randn(hidden_size, output_size) * 0.01
self.b2 = np.zeros((1, output_size))
def forward(self, X):
# Forward propagation
z1 = np.dot(X, self.W1) + self.b1
a1 = np.tanh(z1) # Activation function
z2 = np.dot(a1, self.W2) + self.b2
return z2
# Create a simple neural network
nn = SimpleNeuralNetwork(input_size=2, hidden_size=4, output_size=1)
import tensorflow as tf
# Simple CNN for image classification
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(2),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
# Text preprocessing
text = "Natural Language Processing is amazing!"
tokens = word_tokenize(text.lower())
stemmer = PorterStemmer()
stems = [stemmer.stem(token) for token in tokens]
# Remove stop words
stop_words = set(stopwords.words('english'))
filtered_tokens = [token for token in tokens if token not in stop_words]
import cv2
import numpy as np
# Basic image processing
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Edge detection
edges = cv2.Canny(gray, 50, 150)
# Feature detection (SIFT)
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(gray, None)
# Object detection with Haar cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
class ExpertSystem:
def __init__(self):
self.knowledge_base = []
self.facts = set()
def add_rule(self, conditions, conclusion):
self.knowledge_base.append((conditions, conclusion))
def add_fact(self, fact):
self.facts.add(fact)
def infer(self):
changed = True
while changed:
changed = False
for conditions, conclusion in self.knowledge_base:
if all(cond in self.facts for cond in conditions):
if conclusion not in self.facts:
self.facts.add(conclusion)
changed = True
return self.facts
# Medical diagnosis expert system
expert = ExpertSystem()
expert.add_rule(['fever', 'cough'], 'flu')
expert.add_fact('fever')
expert.add_fact('cough')
diagnosis = expert.infer()
import numpy as np
class FuzzySet:
def __init__(self, name, membership_func):
self.name = name
self.membership_func = membership_func
def membership(self, x):
return self.membership_func(x)
# Triangular membership function
def triangular_mf(x, a, b, c):
if x <= a or x >= c:
return 0
elif a <= x <= b:
return (x - a) / (b - a)
else:
return (c - x) / (c - b)
# Fuzzy temperature sets
cold = FuzzySet("cold", lambda x: triangular_mf(x, 0, 10, 20))
warm = FuzzySet("warm", lambda x: triangular_mf(x, 15, 25, 35))
hot = FuzzySet("hot", lambda x: triangular_mf(x, 30, 40, 50))
# Fuzzy inference
temp = 25
cold_degree = cold.membership(temp)
warm_degree = warm.membership(temp)
hot_degree = hot.membership(temp)
import numpy as np
class BayesianNetwork:
def __init__(self):
self.nodes = {}
self.edges = {}
self.cpts = {}
def add_node(self, node, parents=None):
self.nodes[node] = parents or []
def add_cpt(self, node, cpt):
self.cpts[node] = cpt
def probability(self, node, value, evidence=None):
# Simplified probability calculation
if evidence is None:
evidence = {}
if node in self.cpts:
return self.cpts[node].get(value, 0.0)
return 0.5 # Default uniform probability
# Simple Bayesian network: Rain -> Wet Grass
bn = BayesianNetwork()
bn.add_node("Rain")
bn.add_node("WetGrass", parents=["Rain"])
bn.add_cpt("Rain", {"True": 0.2, "False": 0.8})
bn.add_cpt("WetGrass", {
("Rain", "True"): {"True": 0.9, "False": 0.1},
("Rain", "False"): {"True": 0.1, "False": 0.9}
})
import numpy as np
class QLearningAgent:
def __init__(self, states, actions, learning_rate=0.1, discount=0.9, epsilon=0.1):
self.q_table = np.zeros((states, actions))
self.lr = learning_rate
self.gamma = discount
self.epsilon = epsilon
def choose_action(self, state):
if np.random.random() < self.epsilon:
return np.random.randint(self.q_table.shape[1])
return np.argmax(self.q_table[state])
def learn(self, state, action, reward, next_state):
old_value = self.q_table[state, action]
next_max = np.max(self.q_table[next_state])
new_value = (1 - self.lr) * old_value + self.lr * (reward + self.gamma * next_max)
self.q_table[state, action] = new_value
# Simple grid world example
agent = QLearningAgent(states=4, actions=4)
import numpy as np
class Robot:
def __init__(self, x, y, theta):
self.x = x
self.y = y
self.theta = theta # Orientation
def move(self, linear_vel, angular_vel, dt):
# Simple motion model
self.x += linear_vel * np.cos(self.theta) * dt
self.y += linear_vel * np.sin(self.theta) * dt
self.theta += angular_vel * dt
def sense(self, landmarks):
# Simple sensor model - distance to landmarks
measurements = []
for landmark in landmarks:
distance = np.sqrt((self.x - landmark[0])**2 + (self.y - landmark[1])**2)
measurements.append(distance)
return measurements
# Robot with basic perception
robot = Robot(0, 0, 0)
landmarks = [(5, 5), (10, 0), (0, 10)]
robot.move(1.0, 0.1, 0.1) # Move forward with slight turn
measurements = robot.sense(landmarks)
# Bias detection example
import numpy as np
from sklearn.metrics import accuracy_score
def detect_bias(model, X_test, y_test, sensitive_attribute):
# Check performance across different groups
groups = np.unique(sensitive_attribute)
bias_report = {}
for group in groups:
mask = sensitive_attribute == group
group_accuracy = accuracy_score(y_test[mask], model.predict(X_test[mask]))
bias_report[group] = group_accuracy
# Check for significant differences
accuracies = list(bias_report.values())
max_diff = max(accuracies) - min(accuracies)
return bias_report, max_diff > 0.1 # Flag if difference > 10%
# Example usage
# bias_report, has_bias = detect_bias(model, X_test, y_test, gender)
# Common AI framework imports
import tensorflow as tf
import torch
import sklearn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import nltk
import gym
# TensorFlow example
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
# PyTorch example
import torch.nn as nn
class SimpleNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 64)
self.fc2 = nn.Linear(64, 1)
def forward(self, x):
x = torch.relu(self.fc1(x))
return self.fc2(x)