SWE2017 - Lab Assignment 1pages-7
SWE2017 - Lab Assignment 1pages-7
4 Global Combination: The main process combines the local accumulators into a final
accumulator array, detecting prominent lines across the image.
19.
Code:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define POPULATION_SIZE 4
#define MAX_ITERATIONS 10
#define MUTATION_RATE 0.1
#define MIGRATION_INTERVAL 2
#define VALUE_RANGE 1000
typedef struct {
double x, y, z;
double fitness;
} Candidate;
SWE2017 - Parallel Programming Lab Assignment – 6
if (rank % 2 == 0) {
MPI_Send(&population[0], sizeof(Candidate), MPI_BYTE, neighbor, 0,
MPI_COMM_WORLD);
MPI_Recv(&population[POPULATION_SIZE - 1], sizeof(Candidate),
MPI_BYTE, (rank - 1 + num_procs) % num_procs, 0, MPI_COMM_WORLD, &status);
} else {
MPI_Recv(&population[POPULATION_SIZE - 1], sizeof(Candidate),
MPI_BYTE, (rank - 1 + num_procs) % num_procs, 0, MPI_COMM_WORLD, &status);
MPI_Send(&population[0], sizeof(Candidate), MPI_BYTE, neighbor, 0,
MPI_COMM_WORLD);
}
}
Candidate population[POPULATION_SIZE];
initialize_population(population, POPULATION_SIZE);
Candidate global_best;
global_best.fitness = -INFINITY;
// Migration step
if (iter % MIGRATION_INTERVAL == 0 && num_procs > 1) {
migrate_population(population, rank, num_procs);
}
SWE2017 - Parallel Programming Lab Assignment – 6
if (rank == 0) {
printf("\nGlobal Best Solution:\n");
printf("x = %.2f, y = %.2f, z = %.2f, fitness = %.2f\n",
global_best.x, global_best.y, global_best.z, global_best.fitness);
}
MPI_Finalize();
return 0;
}
Output:
SWE2017 - Parallel Programming Lab Assignment – 6
To maximize the given function efficiently using a parallel genetic algorithm, we'll
divide the task across 4 processors, each managing its own population. The Island
Migration model will facilitate occasional sharing of solutions between processors,
which improves the genetic diversity and helps avoid local optima.
1. Population Initialization:
2. Selection: