Dot indexing is not supported for variables of this type.
3 views (last 30 days)
Show older comments
Kindly help to resolve the error!! Thank You!!
classdef NSGA2Fed < handle
properties
nsga2 % modified this line
options
numObjectives
numDecisionVars
privacyConcerns
communicationOverhead
dataHeterogeneity
maxGenerations
paretoFront
paretoFrontMetrics
end
methods
function obj = NSGA2(varargin)
if nargin > 0
obj.options = varargin{1};
else
obj.options = struct();
end
end
function obj = NSGA2Fed(numObjectives, numDecisionVars, nsga2, privacyConcerns, communicationOverhead, dataHeterogeneity, varargin)
obj.numObjectives = numObjectives;
obj.numDecisionVars = numDecisionVars;
obj.nsga2 = nsga2; % modified this line
obj.privacyConcerns = privacyConcerns;
obj.communicationOverhead = communicationOverhead;
obj.dataHeterogeneity = dataHeterogeneity;
obj.maxGenerations = 50;
obj.paretoFront = [];
obj.paretoFrontMetrics = [];
if nargin > 6
for i = 1:2:length(varargin)
if strcmpi(varargin{i},'maxGenerations')
obj.maxGenerations = varargin{i+1};
end
end
end
obj.nsga2.options;
obj.options.PopulationSize = 100;
obj.options.MaxGenerations = obj.maxGenerations;
obj.options.ParetoFraction = 1.0;
obj.options.FunctionTolerance = 0.0001;
obj.options.OutputFcn = @nsga2_outputfcn;
function stop = nsga2_outputfcn(~, state, ~)
stop = false;
if strcmp(state,'done')
obj.paretoFront = obj.nsga2('paretofront');
obj.paretoFrontMetrics = obj.nsga2('paretoset');
end
end
end
function run(obj)
obj.nsga2('solve',[],[],obj.options);
end
function optimize(obj, decisionVariables, constraints, objectives)
% Calculate the number of clients
numClients = round(1 + obj.dataHeterogeneity * randn(1));
if numClients < 1
numClients = 1;
end
% Divide decision variables and constraints among the clients
clientDecisionVariables = cell(1, numClients);
clientConstraints = cell(1, numClients);
for i = 1:numClients
% Determine the size of the decision variables and constraints
clientSize = ceil(numel(decisionVariables) / numClients);
% Divide the decision variables and constraints among the clients
startIdx = (i - 1) * clientSize + 1;
endIdx = min(i * clientSize, numel(decisionVariables));
clientDecisionVariables{i} = decisionVariables(startIdx:endIdx);
clientConstraints{i} = @(x) constraints([x(1:(end-2)/2), x(end/2+1:end-2), x(end-1:end)]);
end
% Define the multi-objective optimization problem for each client
clientObjectives = cell(1, numClients);
for i = 1:numClients
clientObjectives{i} = @(x) objectives([x(1:(end-2)/2), x(end/2+1:end-2), x(end-1:end)]);
end
% Run the optimization algorithm for each client
for i = 1:numClients
fprintf('nsga2: %s\n', class(obj.nsga2));
obj.nsga2.solve(clientDecisionVariables{i}, clientConstraints{i}, clientObjectives{i});
end
% Combine the Pareto fronts of all clients
combinedParetoFront = obj.nsga2.ParetoFront;
combinedParetoFrontFitness = obj.nsga2.ParetoFrontFitness;
for i = 1:numClients
combinedParetoFront = [combinedParetoFront; obj.nsga2.Population{i}.ParetoFront];
combinedParetoFrontFitness = [combinedParetoFrontFitness; obj.nsga2.Population{i}.ParetoFrontFitness];
end
% Sort the combined Pareto front by fitness
[~, idx] = sortrows(combinedParetoFrontFitness);
obj.ParetoFront = combinedParetoFront(idx, :);
obj.ParetoFrontFitness = combinedParetoFrontFitness(idx, :);
% Apply privacy-preserving mechanisms if needed
if obj.privacyConcerns
obj.applyPrivacyPreservingMechanisms();
end
% Apply communication overhead
obj.applyCommunicationOverhead();
% Apply data heterogeneity
obj.applyDataHeterogeneity();
end
% function applyPrivacyPreservingMechanisms(obj)
% % Apply privacy-preserving mechanisms to the Pareto front
% % (e.g., differential privacy, securemulti-party computation, etc.)
% %
% % This is a placeholder function and can be implemented in
% % subclasses of NSGA2Fed.
% %
% % Inputs:
% % obj - the NSGA2Fed object
% % Apply the privacy-preserving mechanisms here
% % ...
% end
function applyCommunicationOverhead(obj)
% Apply communication overhead to the Pareto front
%
% Inputs:
% obj - the NSGA2Fed object
% Apply the communication overhead here
obj.ParetoFront(:, obj.communicationObjectives) = ...
obj.ParetoFront(:, obj.communicationObjectives) * (1 + obj.communicationOverhead);
end
function applyDataHeterogeneity(obj)
% Apply data heterogeneity to the Pareto front
%
% Inputs:
% obj - the NSGA2Fed object
% Apply the data heterogeneity here
obj.ParetoFront(:, obj.dataObjectives) = ...
obj.ParetoFront(:, obj.dataObjectives) .* (1 + obj.dataHeterogeneity * randn(size(obj.ParetoFront(:, obj.dataObjectives))));
end
end
end
test29
Generation 1: Number of Pareto fronts = 6
Generation 2: Number of Pareto fronts = 6
Generation 3: Number of Pareto fronts = 6
Generation 4: Number of Pareto fronts = 6
Generation 5: Number of Pareto fronts = 6
Generation 6: Number of Pareto fronts = 6
Generation 7: Number of Pareto fronts = 6
Generation 8: Number of Pareto fronts = 6
Generation 9: Number of Pareto fronts = 6
Generation 10: Number of Pareto fronts = 6
Dot indexing is not supported for variables of this type.
Error in NSGA2Fed (line 43)
obj.nsga2.options;
Error in test29 (line 21)
nsga2Fed = NSGA2Fed(numObjectives, numDecisionVars, nsga2, privacyConcerns, communicationOverhead, dataHeterogeneity,
'maxGenerations', 50);
0 Comments
Answers (2)
Gokul Nath S J
on 17 Apr 2023
Hi Sanjay,
Based on my understanding, it seems that you are facing an error at line number 43 of NSGA2Fed class. Since the test29 file is not available, I can only provide some workarounds which will be useful for debugging the code.
Place a breakpoint at line number 43 and print obj.nsga2 in the console. Make sure which all parameters are under it. It seems that obj.nsga2 is not a structure by itself. Kindly make sure the type of 'obj.nsga2' and call the required parameter in it accordingly.
with regards
Gokul Nath S J
Walter Roberson
on 18 Apr 2023
classdef NSGA2Fed < handle
...
methods
function obj = NSGA2(varargin)
NSGA2 does not match the name of the class, NSGA2Fed, and NSGA2 is not marked as a static method. Therefore NSGA2 is not a class constructor: it is an ordinary method of NSGA2Fed. As such it needs to accept an object (of class NSGA2Fed) as a parameter.
obj.nsga2 = nsga2; % modified this line
nsga2 here is some parameter that is being passed in to the NSGA2Fed constructor method, but we do not have any information about what its class is. In particular, as outside observers we have no reason to expect that it is a struct with a field named options or a class with a property or method named options . We just know that where the method NSGA2 assigned to obj.options, that that has nothing to do with whether there will be a options for whatever nsga2 is that is being passed in.
See Also
Categories
Find more on Multiobjective Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!