this cost function is use for single optimization, can you change the cost function to be use as multi objective optimization. i want to optimize the user comfort and energy consumption in smart building. then after you change the cost function to be use as multi objective optimization, then use moema code
the cost function:
function cost = cost_function(dataSH,X)
a1=0.5;
a2=0.5;
Tc=dataSH(:,3)';
Lc=dataSH(:,4)';
Ac=dataSH(:,5)';
CI=zeros(48,1);
EC=zeros(48,1);
Ges=zeros(48,1);
Emin=zeros(48,1);
Emax=zeros(48,1);
p1=1/3;
p2=1/3;
p3=1/3;
Td=22.78; % Kalau ada max and min ambik max - min
Ld=800;
Ad=800;
% Ts=25.56-20;
% Ls=880-720;
% As=880-700;
% lb = [20 720 700];
% ub = [25.56 880 880];
pT=9;
pL=1;
pA=1;
% VarMin = [20 720 700];
% VarMax = [25.56 880 880];
Tmax=25.56;
Lmax=880;
Amax=880;
Tmin=20;
Lmin=720;
Amin=700;
cost1=zeros(48,1);
for k=1:48
Assistant
code
Ti(k)=(1-((X(3*k-2)-Td)/Tc(k))^2);
Li(k)=(1-((X(3*k-1)-Ld)/Lc(k))^2);
Ai(k)=(1-((X(3*k)-Ad)/Ac(k))^2);
% Combined comfort index with weights
CI(k)= p1*Ti(k) + p2*Li(k) + p3*Ai(k);
% Calculate the energy consumption
if Emax(k)<Emin(k)
temp = Emax(k);
Emax(k) = Emin(k);
Emin(k) = temp;
end
Ges(k)=(1-((EC(k)-Emin(k))/(Emax(k)-Emin(k)))^2);
cost1(k)=a1*CI(k)+a2*Ges(k);
end
%cost=a1mean(CI)+a2mean(Ges);
cost=mean(cost1);
% CI_avg=mean(CI);
% ECT=sum(EC);
end
the moema code:
% Main script to run MOEMA optimization
clc;
clear;
close all;
% Set problem parameters
NPop = 100; % 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
% Plot the Pareto front
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro');
xlabel('f1');
ylabel('f2');
title('Pareto Front for ZDT1 using MOEMA');
% Calculate and display metrics
disp(['Number of solutions in Pareto Front: ', num2str(size(paretoFront, 1))]);
disp(['Minimum f1: ', num2str(min(paretoFront(:,1)))]);
disp(['Minimum f2: ', num2str(min(paretoFront(:,2)))]);
function [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN)
% Multi-Objective Evolutionary Mating Algorithm (MOEMA)
% MOEMA attempts to solve multi-objective problems of the following form:
% min F(X) = [f1(X), f2(X), ..., fm(X)] subject to: lb <= X <= ub
%
% FITNESSFCN - function handle of the multi-objective fitness function
% lb - lower bounds on X
% ub - upper bounds on X
% T - number of iterations
% NPop - size of the population (class size)
% 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
% Generation of initial population
pop = initialization(NPop, dim, ub, lb);
pop_dad = pop(1:NPop/2, :); % Eqn (1) in the paper
pop_mum = pop(NPop/2+1:end, :); % Eqn (2) in the paper
% 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];
for k = 1:NPop/2
% Mating process
Imates = 1 + (var(k1x(k, :)) - var(k2x(k, :))); % Eqn (4) in the paper
p = randn(1, dim); % Eqn (6) in the paper
r = rand();
if Imates >= 0 % Upper part of Eqn (5) in the paper
temp1 = p .* k1x(k, :);
temp2 = (1-p) .* k2x(k, :);
pop_child = temp1 + temp2;
else % Lower part of Eqn (5) in the paper
temp1 = p .* k2x(k, :);
temp2 = (1-p) .* k1x(k, :);
pop_child = temp1 + temp2;
end
% Impact of best environment (using random non-dominated solution)
if ~isempty(archive.pop)
best_pop = archive.pop(randi(size(archive.pop, 1)), :);
K = rand(1, dim) < 0.8; % Eqn (8) in the paper
pop_child = best_pop .* (1-K) + pop_child .* K; % Eqn (7) in the paper
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; % Eqn (9) in the paper
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 pool dad and mum
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
disp(['Iteration #', num2str(gen), ' completed. Archive size: ', num2str(size(archive.pop, 1))]);
end
% Return Pareto front and Pareto set
paretoFront = archive.obj;
paretoSet = archive.pop;
end
% Helper Functions
function pop = initialization(NPop, dim, ub, lb)
pop = rand(NPop, dim) .* (ub - lb) + lb;
end
function obj = evaluate_population(pop, FITNESSFCN)
obj = zeros(size(pop, 1), 2); % Assuming 2 objectives
for i = 1:size(pop, 1)
obj(i, :) = FITNESSFCN(pop(i, :));
end
end
function [pop, obj] = update_population(pop, obj, child, child_obj, index)
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
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
end
end
function result = dominates(obj1, obj2)
result = all(obj1 <= obj2) && any(obj1 < obj2);
end
function f = zdt1(x)
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
Certainly! To transition your single-objective cost function to a multi-objective framework and integrate it with your MOEMA code, follow these steps:
Modify the Cost Function to Multi-Objective:
Objective 1 (f1): Maximize user comfort (CI).
Objective 2 (f2): Minimize energy consumption (EC).
Since MOEMA is designed to minimize objectives, we'll invert the user comfort to align with the minimization paradigm by setting f1 = -mean(CI).
Update the MOEMA Main Script:
Use the new multi-objective cost function.
Ensure that the decision variables (dim) and their bounds (lb, ub) are appropriately set based on your optimization problem.
Below is the complete MATLAB code with the necessary modifications:
1. Modified Multi-Objective Cost Function
matlab
function f = multi_objective_cost_function(X)
% Multi-Objective Cost Function for Smart Building Optimization
% Objectives:
% f1 = -mean(User Comfort Index) (to maximize comfort)
% f2 = sum(Energy Consumption) (to minimize energy)
% Load the data (assuming dataSH is available in the workspace)
% Alternatively, you can pass dataSH as an additional argument
dataSH = [
1.0000 60.0000 15.5600 711.0000 610.0000
2.0000 65.0000 18.3300 717.0000 670.0000
% ... (remaining data) ...
48.0000 80.0000 26.6700 898.0000 980.0000];
a1 = 0.5;
a2 = 0.5;
Tc = dataSH(:,3)';
Lc = dataSH(:,4)';
Ac = dataSH(:,5)';
CI = zeros(48,1);
EC = zeros(48,1);
Ges = zeros(48,1);
Emin = zeros(48,1);
Emax = zeros(48,1);
p1 = 1/3;
p2 = 1/3;
p3 = 1/3;
Td = 22.78;
Ld = 800;
Ad = 800;
pT = 9;
pL = 1;
pA = 1;
Tmax = 25.56;
Lmax = 880;
Amax = 880;
Tmin = 20;
Lmin = 720;
Amin = 700;
for k = 1:48
Ti(k) = (1 - ((X(3*k-2) - Td)/Tc(k))^2);
Li(k) = (1 - ((X(3*k-1) - Ld)/Lc(k))^2);
Ai(k) = (1 - ((X(3*k) - Ad)/Ac(k))^2);
% Combined comfort index with weights
CI(k) = p1 * Ti(k) + p2 * Li(k) + p3 * Ai(k);
% Calculate the energy consumption
EC(k) = pT * abs(X(3*k-2) - Tc(k)) + pL * abs(X(3*k-1) - Lc(k)) + pA * abs(X(3*k) - Ac(k));
Emin(k) = pT * abs(Tc(k) - Tmax) + pL * abs(Lc(k) - Lmax) + pA * abs(Ac(k) - Amax);
Emax(k) = pT * abs(Tc(k) - Tmin) + pL * abs(Lc(k) - Lmin) + pA * abs(Ac(k) - Amin);
if Emax(k) < Emin(k)
temp = Emax(k);
Emax(k) = Emin(k);
Emin(k) = temp;
end
Ges(k) = (1 - ((EC(k) - Emin(k))/(Emax(k) - Emin(k)))^2);
end
% Define the two objectives
f1 = -mean(CI); % Maximize user comfort (converted to minimization)
f2 = sum(EC); % Minimize total energy consumption
% Return as a column vector
f = [f1, f2];
end
2. Updated MOEMA Main Script
matlab
% Main Script to Run MOEMA Optimization for Multi-Objective Problem
clc;
clear;
close all;
% Load or define your dataSH here if not passed to the cost function
% For example:
% dataSH = [...]; % Your 48x5 data matrix
% Set problem parameters
NPop = 100; % Population size
T = 200; % Number of iterations
num_time = 48; % Number of time instances
variables_per_time = 3; % X has 3 variables per time instance
dim = num_time * variables_per_time; % Total decision variables
% Define lower and upper bounds based on your problem
% Assuming temperature, lux, and IAQ have specific ranges.
% Adjust these bounds as per your requirements.
% For example:
% Temperature bounds (in Celsius)
temp_lb = repmat(15, 1, num_time); % Minimum Temperature
temp_ub = repmat(30, 1, num_time); % Maximum Temperature
% Lux bounds
lux_lb = repmat(700, 1, num_time); % Minimum Lux
lux_ub = repmat(1000, 1, num_time); % Maximum Lux
% IAQ bounds
iaq_lb = repmat(600, 1, num_time); % Minimum IAQ
iaq_ub = repmat(1000, 1, num_time); % Maximum IAQ
% Combine bounds
lb = [temp_lb, lux_lb, iaq_lb];
ub = [temp_ub, lux_ub, iaq_ub];
% Run MOEMA with the new multi-objective fitness function
[paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, @multi_objective_cost_function);
% Plot the Pareto front
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro');
xlabel('-Mean User Comfort Index');
ylabel('Total Energy Consumption');
title('Pareto Front for User Comfort and Energy Consumption using MOEMA');
grid on;
% Calculate and display metrics
disp(['Number of solutions in Pareto Front: ', num2str(size(paretoFront, 1))]);
disp(['Maximum Mean User Comfort Index: ', num2str(-min(paretoFront(:,1)))]);
disp(['Minimum Total Energy Consumption: ', num2str(min(paretoFront(:,2)))]);
3. Ensure Compatibility in the MOEMA Function
Ensure that the MOEMA function is compatible with the multi-objective cost function. Specifically, the evaluate_population function inside MOEMA should handle vector outputs correctly. Here's a snippet ensuring this:
matlab
% Inside MOEMA Function
function obj = evaluate_population(pop, FITNESSFCN)
num_obj = 2; % Number of objectives
obj = zeros(size(pop, 1), num_obj);
parfor i = 1:size(pop, 1)
obj(i, :) = FITNESSFCN(pop(i, :));
end
end
Note: Using parfor can speed up evaluations, especially for large populations.
4. Helper Functions
Ensure all helper functions (initialization, update_population, update_archive, pareto_dominance, dominates) are correctly handling multi-objective outputs. The provided MOEMA code already accommodates two objectives, but make sure no assumptions limit it to specific objective types.
Final Notes:
Decision Variables (X): Ensure that the dim (number of decision variables) correctly represents all variables you intend to optimize. In the example above, it's set to 48 * 3 = 144 assuming three variables (Temperature, Lux, IAQ) per time instance.
Bounds (lb, ub): Adjust the lower and upper bounds based on realistic and practical ranges for your application.
Data Accessibility: The multi_objective_cost_function assumes that dataSH is accessible within its scope. If not, consider passing dataSH as an additional parameter or defining it within the function.
Optimization Goals:
User Comfort (f1): Higher values are better, thus it's negated to fit the minimization framework.
Energy Consumption (f2): Lower values are better and are directly used.
By following these steps, you can effectively perform multi-objective optimization for user comfort and energy consumption in smart buildings using the MOEMA algorithm.