soft-computing-lab-manual1
soft-computing-lab-manual1
soft-computing-lab-manual1
EX.NO: 1
IMPLEMENTATION OF FUZZY CONTROL/INFERENCE SYSTEM
DATE:
AIM:
To understand the concept of fuzzy control inference system using python programming
language.
ALGORITHM:
Step 1: Define Fuzzy Sets input and output variables.
Step 2: Create Fuzzy Rules
Step 3: Perform Fuzzy Inference
Step 4: Defuzzify the output fuzzy sets to obtain a crisp output value.
Step 5: Use the defuzzified output as the control action.
Step 6: Implement Control Action.
Step 7: Repeat the above steps in a loop as needed for real-time control.
PROGRAM:
import numpy as np
import skfuzzy as fuzz
from skfuzzy
import control as ctrl
OUTPUT:
lOMoAR cPSD| 47590012
RESULT:
Thus the above program for fuzzy control interface system executed successfully withdesired
output.
lOMoAR cPSD| 47590012
EX.NO: 2
PROGRAMMING EXERCISE ON CLASSIFICATION WITH A
DATE: DISCRETE PERCEPTION
AIM:
To understand the concept of classification with discrete perceptron using pythonprogramming
language.
ALGORITHM:
Step 1: Initialize weights W and bias b to small random values
Step 2: Define learning rate
Step 3: Define the number of training epochs
Step 4: Define the training data (features and labels
Step 5: Define the perceptron training algorithm
Step 6: The perceptron is now trained, and you can use it to make predictions
PROGRAM:
import numpy as np
class DiscretePerceptron:
def init (self, input_size):
self.weights = np.zeros(input_size)
self.bias = 0
# Combine the data points and create labels (0 for class 0, 1 for class 1)
inputs = np.vstack((class_0, class_1))
targets = np.array([0, 0, 0, 1, 1, 1])
OUTPUT:
RESULT:
Thus the above program classification with discrete perceptron executed successfullywith
desired output.
lOMoAR cPSD| 47590012
EX.NO: 3
IMPLEMENTATION OF XOR WITH BACKPROPAGATION
DATE: ALGORITHM
AIM:
To understand the concept of XOR with backpropagation algorithm using pythonprograming
language.
ALGORITHM:
1. Initialize the neural network with random weights and biases.
2. Define the training data for XOR
3. Set hyperparameters:
Learning rate (alpha)
Number of epochs (iterations)
Number of hidden layers and neurons per layer
Activation function (e.g., sigmoid)
4. Repeat for each epoch:
a. Initialize the total error for this epoch to 0.
b. For each training example in the dataset:
i. Forward propagation:
ii. Compute the error between the predicted output and the actual output for the
current training example.
iii. Update the total error for this epoch with the squared error from step ii.
iv. Backpropagation:
c. Calculate the average error for this epoch by dividing the total error by the number of
training examples.
d. Check if the average error is below a predefined threshold or if the desired accuracy
is reached.
- If yes, exit the training loop.
5. Once training is complete, you can use the trained neural network to predict XOR values
for new inputs.
6. End.
PROGRAM:
import numpy as np
def sigmoid_derivative(x):
lOMoAR cPSD| 47590012
return x * (1 - x)
# Training loop
for _ in range(epochs):
# Forward propagation
hidden_layer_activation = np.dot(input_data, hidden_weights)
hidden_layer_output = sigmoid(hidden_layer_activation)
# Calculate error
error = target_data - predicted_output
# Backpropagation
output_delta = error * sigmoid_derivative(predicted_output)
hidden_layer_error = output_delta.dot(output_weights.T)
hidden_layer_delta = hidden_layer_error * sigmoid_derivative(hidden_layer_output)
# Update weights
output_weights += hidden_layer_output.T.dot(output_delta) * learning_rate
hidden_weights += input_data.T.dot(hidden_layer_delta) * learning_rate
OUTPUT:
RESULT:
Thus the above program classification with discrete perception executed successfully withdesired
output.
lOMoAR cPSD| 47590012
EX.NO: 4
IMPLEMENTATION OF SELF ORGANIZING MAPS FOR A SPECIFIC
DATE: APPLICATION
AIM:
To understand the concept of self-organizing maps for a specific application usingpython
programming language.
ALGORITHM:
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
# Generate some sample data (replace this with your own dataset)
np.random.seed(42)
data = np.random.rand(100, 2)
# SOM parameters
grid_size = (10, 10) # Grid size of the SOM
input_dim = 2 # Dimensionality of the input data
learning_rate = 0.2
num_epochs = 1000
# Training loop
for epoch in range(num_epochs):
for input_vector in data:
# Find the Best Matching Unit (BMU)
distances = np.linalg.norm(weight_matrix - input_vector, axis=-1)
bmu_coords = np.unravel_index(np.argmin(distances), distances.shape)
OUTPUT:
RESULT:
Thus the above program for self-organizing map executed successfully with desiredoutput.
lOMoAR cPSD| 47590012
EX.NO: 5
PROGRAMMING EXERCISES ON MAXIMIZING A FUNCTION USING
DATE: GENETIC ALGORITHM
AIM:
To understand the concept of maximizing function using Genetic algorithm using python
programming.
ALGORITHM:
1. Initialize the population with random solutions.
2. Define the fitness function to evaluate how good each solution is.
3. Set the maximum number of generations.
4. Set the mutation rate (probability of changing a gene in an individual).
5. Set the crossover rate (probability of two individuals mating).
6. Repeat for each generation:
a. Evaluate the fitness of each individual in the population using the fitness function.
b. Select the best individuals based on their fitness to become parents.
c. Create a new generation by crossover (mixing) the genes of the parents.
d. Apply mutation to some individuals in the new generation.
e. Replace the old population with the new generation.
7. Repeat for the specified number of generations.
8. Find and return the individual with the highest fitness as the best solution.
PROGRAM:
import random
# Genetic Algorithm
def genetic_algorithm(generations, pop_size, lower_bound, upper_bound):
population = initialize_population(pop_size, lower_bound, upper_bound)
population = new_population
OUTPUT:
RESULT:
Thus the above program maximizing function using genetic algorithm executed successfully
with desired output.
lOMoAR cPSD| 47590012
EX.NO: 6
IMPLEMENTATION OF TWO INPUT SINE FUNCTION
DATE:
AIM:
To understand the concept of implementation of two input sine function using Genetic
algorithm.
ALGORITHM:
# Genetic Algorithm for Two-Input Sine Function Optimization
1. Define the fitness function
2. Initialize the population
3. Define functions for genetic operations
4. Implement the main genetic algorithm loop
5. Print the final best solution found by the genetic algorithm.
PROGRAM:
import random
import math
# Define the fitness function (sine function with two inputs)
def fitness_function(x, y):
return math.sin(x) + math.sin(y)
# Genetic Algorithm
def genetic_algorithm(generations, pop_size, lower_bound, upper_bound):
population = initialize_population(pop_size, lower_bound, upper_bound)
population = new_population
OUTPUT:
RESULT:
Thus the above program implementation of two input sine function using genetic algorithm executed
successfully.
lOMoAR cPSD| 47590012
EX.NO: 7
IMPLEMENTATION OF THREE INPUT NON LINEAR FUNCTION
DATE:
AIM:
To understand the concept of implementation of three input non linear function
ALGORITHM:
# Genetic Algorithm for Three-Input Nonlinear Function Optimization
1. Define the fitness function.
2. Initialize the population.
3. Define functions for genetic operations.
4. Implement the main genetic algorithm loop.
5. Print the final best solution found by the genetic algorithm.
PROGRAM:
import random
# Genetic Algorithm
def genetic_algorithm(generations, pop_size, lower_bound, upper_bound):
population = initialize_population(pop_size, lower_bound, upper_bound)
population = new_population
generations = 50
pop_size = 100
lower_bound = -1
upper_bound = 1
OUTPUT:
RESULT:
Thus the above program genetic algorithm for three input non-linear function optimization
executed successfully.
lOMoAR cPSD| 47590012