give me full code, try this code on zdt3
- User
give me full code, try this code on zdt3
- Assistant
The ZDT3 problem is one of a suite of benchmark problems designed to evaluate the performance of multi-objective optimization algorithms. It has a non-convex Pareto front with disconnected regions, which makes it a standard test case for many algorithms.
To solve ZDT3, you can use a variety of optimization algorithms. Here, I'll demonstrate how to use Non-dominated Sorting Genetic Algorithm II (NSGA-II) for solving ZDT3. We'll use Python and the DEAP library, which is great for creating evolutionary algorithms.
First, make sure you have the necessary libraries installed. You can install DEAP using pip if you haven't already:
pip install deap numpy matplotlibHere's the full code using DEAP:
import random import numpy as np import matplotlib.pyplot as plt from deap import base, creator, tools, algorithms # Define the ZDT3 problem def zdt3(individual): n = len(individual) g = 1 + 9 / (n - 1) * sum(individual[1:]) f1 = individual[0] f2 = g * (1 - (individual[0] / g)**0.5 - (individual[0] / g) * np.sin(10 * np.pi * individual[0])) return f1, f2 # Set up DEAP environment for NSGA-II creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0)) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() toolbox.register("attr_float", random.random) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=30) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=0.0, up=1.0, eta=20.0) toolbox.register("mutate", tools.mutPolynomialBounded, low=0.0, up=1.0, eta=20.0, indpb=1.0/30.0) toolbox.register("select", tools.selNSGA2) toolbox.register("evaluate", zdt3) def main(): population_size = 100 max_generations = 250 crossover_prob = 0.9 mutation_prob = 1.0 / 30.0 pop = toolbox.population(n=population_size) algorithms.eaMuPlusLambda(pop, toolbox, mu=population_size, lambda_=population_size, cxpb=crossover_prob, mutpb=mutation_prob, ngen=max_generations, stats=None, halloffame=None, verbose=True) # Extract variable and objective values for plotting front = np.array([ind.fitness.values for ind in pop]) plt.scatter(front[:,0], front[:,1], c="b") plt.xlabel("Objective 1") plt.ylabel("Objective 2") plt.title("Pareto Front Approximation of ZDT3") plt.grid(True) plt.show() if __name__ == "__main__": main()Explanation
-
Problem Definition:
zdt3function defines the ZDT3 problem that maps an individual's genes to its objective values using the functions specified for ZDT3. -
DEAP Setup:
creatorandtoolboxare components of DEAP to set up the problem.creator.FitnessMinis used because we are minimizing objective functions.- Individuals are created as lists of floating-point numbers, each initialized randomly.
-
Operators:
cxSimulatedBinaryBoundedandmutPolynomialBoundedare used for crossover and mutation, which are typical for real-valued genetic algorithms.selNSGA2is the selection operator implementing the NSGA-II algorithm.
-
Execution:
- Population is initialized and evolved over a set number of generations.
- At the end, the approximate Pareto front is plotted using matplotlib.
This example initializes a population of 100 individuals and runs for 250 generations to give a reasonable approximation of the Pareto front of the ZDT3 problem. Adjust
population_size,max_generations, and other parameters as needed based on the problem's complexity and available computation resources. -
- User
here is the code: % MOEMA_PerformanceMetrics_Full.m % Main script to run MOEMA optimization and evaluate performance metrics
clc; clear; close all;
%% Parameters NPop = 50; % Population size T = 200; % Number of iterations dim = 30; % Number of decision variables lb = zeros(1, dim); % Lower bound ub = ones(1, dim); % Upper bound
num_runs = 10; % Number of runs for statistical evaluation
% Initialize metrics storage GD_values = zeros(num_runs,1); best_GD = zeros(num_runs,1); average_GD = zeros(num_runs,1); median_GD = zeros(num_runs,1); worst_GD = zeros(num_runs,1);
IGD_values = zeros(num_runs,1); best_IGD = zeros(num_runs,1); average_IGD = zeros(num_runs,1); median_IGD = zeros(num_runs,1); worst_IGD = zeros(num_runs,1);
Convergence_values = zeros(num_runs,1); Diversity_values = zeros(num_runs,1);
%% Run MOEMA Multiple Times for run = 1:num_runs fprintf('Run #%d/%d\n', run, num_runs);
% Run MOEMA [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, @zdt1); % Get True Pareto Front true_pareto = get_true_pareto(); % Calculate Generational Distance (GD) GD = generational_distance(paretoFront, true_pareto); % Calculate Inverted Generational Distance (IGD) IGD = inverted_generational_distance(paretoFront, true_pareto); % Calculate Convergence Metric (using GD as an example) Convergence = GD; % Modify if a different convergence metric is desired % Calculate Diversity Metric Diversity = diversity_metric(paretoFront, true_pareto); % Store metrics GD_values(run) = GD; IGD_values(run) = IGD; Convergence_values(run) = Convergence; Diversity_values(run) = Diversity;end
%% Compute Statistics for GD best_GD_value = min(GD_values); average_GD_value = mean(GD_values); median_GD_value = median(GD_values); worst_GD_value = max(GD_values);
%% Compute Statistics for IGD best_IGD_value = min(IGD_values); average_IGD_value = mean(IGD_values); median_IGD_value = median(IGD_values); worst_IGD_value = max(IGD_values);
%% Display Metrics fprintf('\nPerformance Metrics over %d runs:\n', num_runs);
% Generational Distance (GD) Statistics fprintf('Generational Distance (GD):\n'); fprintf(' Best = %.4f\n', best_GD_value); fprintf(' Average = %.4f\n', average_GD_value); fprintf(' Median = %.4f\n', median_GD_value); fprintf(' Worst = %.4f\n', worst_GD_value);
% Inverted Generational Distance (IGD) Statistics fprintf('Inverted Generational Distance (IGD):\n'); fprintf(' Best = %.4f\n', best_IGD_value); fprintf(' Average = %.4f\n', average_IGD_value); fprintf(' Median = %.4f\n', median_IGD_value); fprintf(' Worst = %.4f\n', worst_IGD_value);
% Convergence Metric Statistics fprintf('Convergence Metric (GD as example):\n'); fprintf(' Mean = %.4f, Std = %.4f\n', mean(Convergence_values), std(Convergence_values));
% Diversity Metric Statistics fprintf('Diversity Metric:\n'); fprintf(' Mean = %.4f, Std = %.4f\n', mean(Diversity_values), std(Diversity_values));
%% Plot Pareto Front from the Last Run figure; plot(paretoFront(:,1), paretoFront(:,2), 'ro', 'MarkerSize',6, 'MarkerFaceColor','r'); hold on; plot(true_pareto(:,1), true_pareto(:,2), 'b--', 'LineWidth',1.5); xlabel('f1'); ylabel('f2'); title(sprintf('Pareto Front for ZDT1 using MOEMA (Run #%d)', num_runs)); legend('Obtained Pareto Front', 'True Pareto Front'); grid on; hold off;
%% FUNCTIONS
% ------------------- Main MOEMA Function ------------------- function [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN) % Multi-Objective Evolutionary Mating Algorithm (MOEMA) % Optimize the multi-objective problem defined by FITNESSFCN % % Inputs: % NPop - Population size % T - Number of iterations % lb - Lower bounds (1 x dim) % ub - Upper bounds (1 x dim) % dim - Number of decision variables % FITNESSFCN - Handle to the objective function % % Outputs: % paretoFront - Set of non-dominated solutions in objective space % paretoSet - Set of non-dominated solutions in decision space % Trajectories - Best solution trajectory over iterations % fitness_history - History of fitness values % position_history - History of population positions
% Preallocation fitness_history = zeros(NPop, T, 2); % Assuming 2 objectives position_history = zeros(NPop, T, dim); Trajectories = zeros(dim, T); % Generation of initial population pop = initialization(NPop, dim, ub, lb); pop_dad = pop(1:NPop/2, :); % Parent group 1 pop_mum = pop(NPop/2+1:end, :); % Parent group 2 % Evaluation of objective function for initialization obj_dad = evaluate_population(pop_dad, FITNESSFCN); obj_mum = evaluate_population(pop_mum, FITNESSFCN); obj = [obj_dad; obj_mum]; % Initialize archive with non-dominated solutions archive = update_archive([], [], pop, obj); for gen = 1:T % Start iterations k1 = randperm(NPop/2); k2 = randperm(NPop/2); k1x = pop_dad(k1, :); k2x = pop_mum(k2, :); for k = 1:NPop/2 % Mating process I_mates = 1 + (var(k1x(k, :)) - var(k2x(k, :))); p = randn(1, dim); r = rand(); if I_mates >= 0 % Upper mating strategy temp1 = p .* k1x(k, :); temp2 = (1-p) .* k2x(k, :); pop_child = temp1 + temp2; else % Lower mating strategy temp1 = p .* k2x(k, :); temp2 = (1-p) .* k1x(k, :); pop_child = temp1 + temp2; end % Impact of best environment if ~isempty(archive.pop) best_pop = archive.pop(randi(size(archive.pop, 1)), :); K = rand(1, dim) < 0.8; pop_child = best_pop .* (1-K) + pop_child .* K; end % Bounding of the solution pop_child = max(min(ub, pop_child), lb); % Evaluate child obj_child = FITNESSFCN(pop_child); % Update population using non-dominated sorting [pop_dad, obj_dad] = update_population(pop_dad, obj_dad, pop_child, obj_child, k1(k)); [pop_mum, obj_mum] = update_population(pop_mum, obj_mum, pop_child, obj_child, k2(k)); % Exploration process if r < 0.2 if ~isempty(archive.pop) temp1 = archive.pop(randi(size(archive.pop, 1)), :); pop_child = rand(1, dim) .* temp1; pop_child = max(min(ub, pop_child), lb); obj_child = FITNESSFCN(pop_child); % Update population using non-dominated sorting [pop_dad, obj_dad] = update_population(pop_dad, obj_dad, pop_child, obj_child, randi(NPop/2)); [pop_mum, obj_mum] = update_population(pop_mum, obj_mum, pop_child, obj_child, randi(NPop/2)); end end end % Combine updated dad and mum pools pop = [pop_dad; pop_mum]; obj = [obj_dad; obj_mum]; % Update archive archive = update_archive(archive.pop, archive.obj, pop, obj); % Record the history at each iteration fitness_history(:, gen, :) = obj; position_history(:, gen, :) = pop; % Update Trajectories (using a random non-dominated solution) if ~isempty(archive.pop) Trajectories(:, gen) = archive.pop(randi(size(archive.pop, 1)), :)'; end fprintf(' Iteration #%d completed. Archive size: %d\n', gen, size(archive.pop, 1)); end % Return Pareto front and Pareto set paretoFront = archive.obj; paretoSet = archive.pop;end
% ------------------- Initialization Function ------------------- function pop = initialization(NPop, dim, ub, lb) % Initialize population within the given bounds pop = rand(NPop, dim) .* (ub - lb) + lb; end
% ------------------- Evaluate Population Function ------------------- function obj = evaluate_population(pop, FITNESSFCN) % Evaluate objective functions for a population num_sol = size(pop, 1); obj = zeros(num_sol, 2); % Assuming 2 objectives parfor i = 1:num_sol % Parallel loop for efficiency obj(i, :) = FITNESSFCN(pop(i, :)); end end
% ------------------- Update Population Function ------------------- function [pop, obj] = update_population(pop, obj, child, child_obj, index) % Update the population with a child if it dominates or is non-dominated if dominates(child_obj, obj(index, :)) pop(index, :) = child; obj(index, :) = child_obj; elseif ~dominates(obj(index, :), child_obj) if rand() < 0.5 pop(index, :) = child; obj(index, :) = child_obj; end end end
% ------------------- Update Archive Function ------------------- function archive = update_archive(archive_pop, archive_obj, pop, obj) % Update the archive with non-dominated solutions from the combined population combined_pop = [archive_pop; pop]; combined_obj = [archive_obj; obj];
[is_dominated, ~] = pareto_dominance(combined_obj); non_dominated = ~is_dominated; archive.pop = combined_pop(non_dominated, :); archive.obj = combined_obj(non_dominated, :);end
% ------------------- Pareto Dominance Function ------------------- function [is_dominated, domination_count] = pareto_dominance(objectives) % Determine dominated solutions [n_solutions, ~] = size(objectives); is_dominated = false(n_solutions, 1); domination_count = zeros(n_solutions, 1); % Optional: count of dominations
for i = 1:n_solutions for j = i+1:n_solutions if dominates(objectives(i, :), objectives(j, :)) is_dominated(j) = true; domination_count(i) = domination_count(i) + 1; elseif dominates(objectives(j, :), objectives(i, :)) is_dominated(i) = true; domination_count(j) = domination_count(j) + 1; end end endend
% ------------------- Dominance Check Function ------------------- function result = dominates(obj1, obj2) % Check if obj1 dominates obj2 result = all(obj1 <= obj2) && any(obj1 < obj2); end
% ------------------- True Pareto Front Function ------------------- function true_pareto = get_true_pareto() % Generate the true Pareto front for ZDT1 num_points = 100; % Number of points in the true Pareto front f1 = linspace(0, 1, num_points)'; f2 = 1 - sqrt(f1); true_pareto = [f1, f2]; end
% ------------------- Generational Distance (GD) Function ------------------- function gd = generational_distance(obtained_pf, true_pf) % Calculate Generational Distance (GD) distances = sqrt(sum((obtained_pf - closest_point(obtained_pf, true_pf)).^2, 2)); gd = mean(distances); end
% ------------------- Inverted Generational Distance (IGD) Function ------------------- function igd = inverted_generational_distance(obtained_pf, true_pf) % Calculate Inverted Generational Distance (IGD) distances = sqrt(sum((true_pf - closest_point(true_pf, obtained_pf)).^2, 2)); igd = mean(distances); end
% ------------------- Diversity Metric Function ------------------- function spread = diversity_metric(obtained_pf, true_pf) % Calculate Diversity Metric (Spread) % Normalize the obtained Pareto front based on the true Pareto front
sorted_pf = sortrows(obtained_pf, 1); % Sort based on f1 num_obtained = size(sorted_pf, 1); % Handle edge case with insufficient points if num_obtained < 2 spread = Inf; return; end % Calculate gaps between consecutive points gaps = sqrt(sum(diff(sorted_pf).^2, 2)); spread = sum(gaps) / (sqrt(2) * (num_obtained - 1));end
% ------------------- Closest Point Helper Function ------------------- function closest = closest_point(src, target) % Find the closest point in target for each point in src num_src = size(src, 1); num_target = size(target, 1); closest = zeros(size(src));
for i = 1:num_src distances = sqrt(sum((target - src(i,:)).^2, 2)); [~, idx] = min(distances); closest(i,:) = target(idx,:); endend
% ------------------- ZDT1 Objective Function ------------------- function f = zdt1(x) % ZDT1 Test Problem f1 = x(1); g = 1 + 9 * sum(x(2:end)) / (length(x) - 1); h = 1 - sqrt(f1 / g); f2 = g * h; f = [f1, f2]; end
- Assistant
This MATLAB script defines a multi-objective evolutionary algorithm referred to as the Multi-Objective Evolutionary Mating Algorithm (MOEMA) and evaluates its performance on the ZDT1 problem. The script includes functions for calculating performance metrics such as Generational Distance (GD), Inverted Generational Distance (IGD), and a diversity metric.
Here's a brief breakdown of the components and functionality of the script:
Parameters
NPop: The size of the population for the optimization.T: The number of iterations the algorithm will run.dim: Number of decision variables.lbandub: Lower and upper bounds for the decision variables.num_runs: The number of times the algorithm will run to gather statistical data.
Metrics
- The script evaluates several performance metrics across multiple runs:
- Generational Distance (GD): Measures the proximity of the obtained Pareto front to the true Pareto front.
- Inverted Generational Distance (IGD): Measures how well the obtained Pareto front covers the true Pareto front.
- Convergence and Diversity Metrics: Provide additional evaluations on how the algorithm performs over iterations.
MOEMA Function
The core function
MOEMAhandles:- Initialization of the population randomly within given bounds.
- Objective Evaluation using the provided fitness function (e.g.,
zdt1). - An evolutionary mating process that creates offspring populations based on mating strategies.
- Archive Management to store non-dominated solutions.
- Metrics recording at each generation for analysis.
Performance Evaluation and Results
After running
MOEMAmultiple times, the script calculates and displays metrics such as the best, average, median, and worst values of GD and IGD. It also plots the final obtained Pareto front from the last run against the true Pareto front for visual comparison.How to Use
- Modify Parameters: You can change
NPop,T,dim,lb,ub, andnum_runsto match your problem requirements. - Add New Objectives: To test other functions (e.g., ZDT3), implement a similar objective function as
zdt1within the script. - Enhance Metrics: Expand the metrics and visualization as needed to further understand the behavior of your optimization algorithm.
Considerations
- This script assumes efficient handling of non-dominated sorting and numerical calculations, possibly using parallel loops (e.g.,
parfor) for performance. - The diversity metric (
diversity_metric) is computed based on the spread of obtained solutions; this can be adapted or extended for other diversity metrics.
By following these steps and understanding how the script is structured, you can implement and test the MOEMA algorithm on various multi-objective optimization problems beyond ZDT1, such as ZDT3 or others in the ZDT suite.