Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Soft Computing Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

1.

//Union, Intersection and Complement of Fuzzy set


#include <stdio.h>
int main()
{
float a[10],b[10],max[10],min[10];
int n,i,j;
printf("Enter no. of elements=");
scanf("%d",&n);
printf("Enter fuzzy set A=");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("Enter fuzzy set B=");
for(i=0;i<n;i++)
scanf("%f",&b[i]);
for(i=0;i<n;i++)
{
if(a[i]>b[i])
max[i]=a[i];
else
max[i]=b[i];
}
printf("Union of Fuzzy sets A & B is=");
for(i=0;i<n;i++)
printf("%f\t",max[i]);
printf("\n************************************************************
*************\n");
for(i=0;i<n;i++)
{
if(a[i]<b[i])
min[i]=a[i];
else
min[i]=b[i];
}
printf("\nIntersection of Fuzzy sets A & B is=");
for(i=0;i<n;i++)
printf("%f\t",min[i]);
printf("\n************************************************************
*************\n");
printf("\nComplement of Fuzzy sets A is=");
for(i=0;i<n;i++)
printf("%f\t",1-a[i]);
printf("\n************************************************************
*************\n");
printf("\nComplement of Fuzzy sets B is=");
for(i=0;i<n;i++)
printf("%f\t",1-b[i]);
}
2// A program to implement lambda cut
#include<stdio.h>
int main()
{
float a[10],lambda;
int n,i;
printf("Enter no. of elements=");
scanf("%d",&n);
printf("Enter fuzzy set A=");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("\n Enter value of lambda:-");
scanf("%f",&lambda);
printf("Lambda Cut Set is given by=");
for(i=0;i<n;i++)
{
if(a[i]>=lambda)
printf("\t %f",a[i]);
}
}
3// A program to implement lambda cut for matrix
#include<stdio.h>
int main()
{
float a[10][10],lambda;
int r,c,i,j;
printf("Enter no. of rows & Columns=");
scanf("%d%d",&r,&c);
printf("Enter fuzzy set A=");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%f",&a[i][j]);

printf("\n Enter value of lambda:-");


scanf("%f",&lambda);
printf("Lambda Cut Set is given by=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]>=lambda)
printf("\t 1");
else
printf("\t 0");
}
printf("\n");
}
}
4// Program for cartesian product
#include <stdio.h>
int main()
{
float a[10],b[10],cartesian[10][10];
int r,c,i,j;
printf("Enter no. of elements in A & B=");
scanf("%d%d",&r, &c);
printf("Enter fuzzy set A=");
for(i=0;i<r;i++)
scanf("%f",&a[i]);
printf("Enter fuzzy set B=");
for(i=0;i<c;i++)
scanf("%f",&b[i]);
printf("\n cartesian product of AXB is=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i]<b[j])
cartesian[i][j]=a[i];
else
cartesian[i][j]=b[j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%f",cartesian[i][j]);
}
printf("\n");
}
}

5
6Write a program to implement sigmoid Activation Function
#include <stdio.h>
#include <math.h>
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
int main() {
double input, output;
// Read the input value from user
printf("Enter the input value: ");
scanf("%lf", &input);
// Compute the sigmoid activation function
output = sigmoid(input);
// Print the output value to the console
printf("Output: %lf\n", output);
return 0;
}
7. Write a program to implement Gaussian Activation Function
#include <stdio.h>
#include <math.h>

double gaussian(double x, double mu, double sigma) {


double exponent = -1.0 * ((x - mu) * (x - mu)) / (2.0 * sigma * sigma);
return exp(exponent);
}

int main() {
// Test the Gaussian Activation Function
double x = 0.5;
double mu = 0.0;
double sigma = 1.0;
double output = gaussian(x, mu, sigma);

// Print the output value


printf("Output: %lf\n", output);

return 0;
}
8. Write a program to implement Bipolar Activation Function

#include <stdio.h>
double bipolar(double x) {
if (x >= 0) {
return 1.0;
} else {
return -1.0;
}
}
int main() {
// Test the bipolar Activation Function
double x = 0.5;
double output = bipolar(x);

// Print the output value


printf("Output: %lf\n", output);

return 0;
}
9. Write a program to implement binary Activation Function
#include <stdio.h>
int binary(double x) {
if (x >= 0) {
return 1;
} else {
return 0;
}
}
int main() {
// Test the binary Activation Function
double x = -0.5;
int output = binary(x);

// Print the output value


printf("Output: %d\n", output);

return 0;
}
10.Write a program to implement Feed Forward Network
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NUM_INPUTS 2
#define NUM_HIDDEN 3
#define NUM_OUTPUTS 1

double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}

int main() {
// Initialize the input values
double input[NUM_INPUTS] = {0.05, 0.10};

// Initialize the weights and biases for the hidden layer


double hidden_weights[NUM_INPUTS][NUM_HIDDEN] = {{0.15, 0.25, 0.35},
{0.20, 0.30, 0.40}};
double hidden_bias[NUM_HIDDEN] = {0.35, 0.35, 0.60};

// Initialize the weights and biases for the output layer


double output_weights[NUM_HIDDEN][NUM_OUTPUTS] = {{0.45},
{0.50},
{0.55}};
double output_bias[NUM_OUTPUTS] = {0.60};

// Forward pass through the network


double hidden_output[NUM_HIDDEN];
double output[NUM_OUTPUTS];
for (int i = 0; i < NUM_HIDDEN; i++) {
double node_input = 0.0;
for (int j = 0; j < NUM_INPUTS; j++) {
node_input += input[j] * hidden_weights[j][i];
}
node_input += hidden_bias[i];
hidden_output[i] = sigmoid(node_input);
}
for (int i = 0; i < NUM_OUTPUTS; i++) {
double node_input = 0.0;
for (int j = 0; j < NUM_HIDDEN; j++) {
node_input += hidden_output[j] * output_weights[j][i];
}
node_input += output_bias[i];
output[i] = sigmoid(node_input);
}
// Print the output values
printf("Output: %lf\n", output[0]);
return 0;
}

11Write a C program for Linear Regression Supervised Learning Algorithm


#include <stdio.h>
#define LEARNING_RATE 0.01
#define MAX_ITERATIONS 1000
double dot_product(double x[], double w[], int n) {
double result = 0.0;
for (int i = 0; i < n; i++) {
result += x[i] * w[i];
}
return result;
}
int main() {
// Training dataset for the linear regression algorithm
double X[5][2] = {{1, 1}, {2, 3}, {3, 5}, {4, 7}, {5, 9}};
double y[5] = {3, 7, 11, 15, 19};
// Initialize the weights and bias
double w[2] = {0.0, 0.0};
double b = 0.0;
// Train the linear regression model using the training dataset
int iterations = 0;
double mse = 0.0;
while (iterations < MAX_ITERATIONS) {
mse = 0.0;
for (int i = 0; i < 5; i++) {
double z = dot_product(X[i], w, 2) + b;
double error = y[i] - z;
w[0] += LEARNING_RATE * error * X[i][0];
w[1] += LEARNING_RATE * error * X[i][1];
b += LEARNING_RATE * error;
mse += error * error;
}
mse /= 5.0;
iterations++;
}
// Print the trained weights and bias
printf("Trained weights: w1 = %lf, w2 = %lf\n", w[0], w[1]);
printf("Trained bias: b = %lf\n", b);
// Test the linear regression model on new input
double input[2];
printf("Enter the input values: ");
scanf("%lf %lf", &input[0], &input[1]);
double output = dot_product(input, w, 2) + b;
printf("Output: %lf\n", output);
return 0;
}
12. //Write a program to implement Hebb’s Rule
#include <stdio.h>
#define N 3 // Number of inputs
#define M 2 // Number of outputs
int main() {
// Define the input and output arrays
double input[N][M] = {
{1.0, 0.0},
{0.0, 1.0},
{1.0, 1.0}
};
double output[N] = {1.0, 0.5, 0.0};
// Define the weight matrix
double weights[M] = {1.0, 0.0};
// Train the weight matrix using Hebb's Rule
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
weights[j] += input[i][j] * output[i];
}
}
// Print the trained weight matrix
printf("Trained Weight Matrix:\n");
for (int i = 0; i < M; i++) {
printf("%lf ", weights[i]);
}
printf("\n");
return 0;
}
13 C program for max product composition
#include <stdio.h>
#define N 3 // Number of elements in the input sets
// Define the fuzzy relations
double R1[N][N] = {
{0.6, 0.2, 0.1},
{0.3, 0.7, 0.4},
{0.1, 0.5, 0.8}
};
double R2[N][N] = {
{0.5, 0.3, 0.2},
{0.2, 0.6, 0.5},
{0.1, 0.4, 0.7}
};
// Define the result relation
double R3[N][N];
int main() {
// Compute the max product composition of R1 and R2
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
double max_product = 0.0;
for (int k = 0; k < N; k++) {
double product = R1[i][k] * R2[k][j];
if (product > max_product) {
max_product = product;
}
}
R3[i][j] = max_product;
}
}
// Print the result relation
printf("Result Relation:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%lf ", R3[i][j]);
}
printf("\n");
}
return 0;
}
Output:

You might also like