Soft Computing Lab File
Soft Computing Lab File
Index
Theory:
Rough Sets are a mathematical tool to deal with vagueness and uncertainty, introduced by Zdzisław
Pawlak. The core idea is to approximate a set by a pair of lower and upper bounds using equivalence
relations on the data. Objects that surely belong to a concept form the lower approximation, while
objects that possibly belong form the upper approximation.
Fuzzy Logic, developed by Lotfi Zadeh, extends classical logic by allowing truth values between 0 and
1. A fuzzy set represents data where elements have degrees of membership. This logic is useful when
boundaries between concepts are not crisp.
Python Code:
lower = set()
upper = set()
if eq_class.issubset(target_set):
lower.update(eq_class)
upper.update(eq_class)
U = {1, 2, 3, 4, 5, 6}
target = {1, 2, 3}
# Intersection (min)
intersection = {x: min(A[x], B[x]) for x in A}
# Union (max)
Theory:
Discretization is a preprocessing technique in which continuous data attributes are divided into
discrete intervals or categories. It is particularly important in rough set theory where attribute values
need to be categorical.
Python Code:
import pandas as pd
import numpy as np
discrete_data = equal_width_discretization(data, 3)
Experiment 3: Implement Decisive Discernibility Matrix and Function for Decision Rules
Theory:
The discernibility matrix is a table that identifies which attributes differentiate objects with different
decisions. The discernibility function, derived from this matrix, is expressed in disjunctive normal
form (DNF) and helps in determining reducts and decision rules.
Python Code:
import numpy as np
n = len(data)
for i in range(n):
for j in range(i+1, n):
if decision[i] != decision[j]:
return matrix
decision = [1, 1, 0]
dm = discernibility_matrix(data, decision)
print("Discernibility Matrix:")
print(row)
Theory:
In rough set theory, discretization is required to transform continuous attributes into intervals. This
enables equivalence class formation and approximation calculations. Proper discretization enhances
rule generation and accuracy.
Pseudocode:
Python Code:
import pandas as pd
cut_points = []
for i in range(len(sorted_df) - 1):
return sorted(set(cut_points))
for i, cp in enumerate(cut_points):
return f"bin_{i}"
return f"bin_{len(cut_points)}"
discretized_df = df.copy()
return discretized_df
# Example usage
data = {
df = pd.DataFrame(data)
print(discretized_df)
Experiment 5: Implement Arithmetic Operations in Fuzzy Sets
Theory:
Fuzzy set arithmetic enables combining or comparing fuzzy values using operations such as union,
intersection, complement, addition, subtraction, multiplication, and division.
Complement: 1 - μA(x)
Python Code:
import numpy as np
Theory:
Genetic Algorithms (GAs) simulate natural evolution. They maintain a population of candidate
solutions and evolve them through:
Python Code:
import random
def fitness(x):
return x ** 2
def create_chromosome():
point = random.randint(1, 4)
chrom ^= bit
return chrom
for g in range(generations):
next_gen = pop[:size//2]
p1, p2 = random.sample(next_gen, 2)
pop = next_gen
return pop[0]
best = genetic_algorithm()
Theory:
Ant Colony Optimization (ACO) is a probabilistic technique that mimics the behavior of ants laying
down pheromone trails to discover shortest paths in a graph.
Python Code:
import numpy as np
n = dist.shape[0]
pheromone = np.ones_like(dist)
best_path = None
best_length = float('inf')
for _ in range(n_iter):
for _ in range(n_ants):
path = [0]
i = path[-1]
probs = [(pheromone[i][j] ** alpha) * ((1 / dist[i][j]) ** beta) if j not in path else 0 for j in
range(n)]
probs = np.array(probs)
path.append(np.random.choice(range(n), p=probs))
best_length = length
best_path = path
pheromone *= (1 - evap)