- 'randsample' function: https://www.mathworks.com/help/stats/randsample.html
Error using randsample (line 89) W must have length 3.
    10 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
function [fuzzy_rules, pheromone] = aco_fuzzy_rules(X, y, num_rules, num_ants, max_iter, alpha, beta, rho, Q)
    num_features = size(X, 2);
    num_membership_functions = 3; % Assuming 3 membership functions per feature
    % Convert table to numeric array
    X = table2array(X);
    % Initialize pheromone matrix
    pheromone = ones(num_features, num_membership_functions * num_rules);
    % Initialize best solution
    best_solution = [];
    best_fitness = -inf;
    for iter = 1:max_iter
        % Construct solutions (fuzzy rules) using ants
        solutions = cell(num_ants, 1);
        for k = 1:num_ants
            solution = [];
            for j = 1:num_features
                p = pheromone(j, :) .^ alpha .* (1 ./ (1 + abs(X(:, j)))) .^ beta;
                p = p / sum(p);
                if length(p) < num_membership_functions
                    p = [p, zeros(1, num_membership_functions - length(p))];
                end
                for r = 1:num_rules
                    solution = [solution, randsample(1:num_membership_functions, 1, true, p')];
                end
            end
            solutions{k} = solution;
        end
        % Evaluate fitness of solutions
        fitnesses = cellfun(@(s) fuzzy_fitness(s, X, y), solutions);
        % Update best solution
        [best_fitness, best_idx] = max([best_fitness, fitnesses]);
        best_solution = solutions{best_idx};
        % Update pheromone matrix
        pheromone = (1 - rho) * pheromone;
        for k = 1:num_ants
            solution = solutions{k};
            fitness = fitnesses(k);
            pheromone = pheromone + Q * fitness * (solution == reshape(repmat(1:num_membership_functions, num_rules, 1), 1, []));
        end
    end
    % Convert best solution to fuzzy rules
    fuzzy_rules = reshape(best_solution, num_features, num_rules);
end
The above code is for ACO to be used to learn new rules
can someone help how to fix this error - Error using randsample (line 89)
W must have length 3.
Error in AcoGafuzzyNew>aco_fuzzy_rules (line 103)
solution = [solution, randsample(1:num_membership_functions, 1,
true, p')];
Error in AcoGafuzzyNew (line 16)
[fuzzy_rules, pheromone] = aco_fuzzy_rules(X, y, num_rules, num_ants, max_iter, alpha, beta, rho, Q);
0 Commenti
Risposte (1)
  Balavignesh
      
 il 30 Mag 2024
        Hi Michael, 
It looks like you are encountering an error with 'randsample' MATLAB function. The error suggests that the probability weights vector 'p' you are passing doesn't have the same length as 'num_membership_functions'. This inconsistency in length likely arises during the calculation of 'p', especially if the 'pheromone' matrix dimensions or its manipulation does not align perfectly with the expected sizes.
for k = 1:num_ants
    solution = [];
    for j = 1:num_features
        p = pheromone(j, :) .^ alpha .* (1 ./ (1 + abs(X(:, j)))) .^ beta;
        p = p / sum(p);
        % Ensure p has the correct length by resizing it if necessary
        expected_length = num_membership_functions * num_rules;
        if length(p) < expected_length
            p = [p, zeros(1, expected_length - length(p))];
        elseif length(p) > expected_length
            p = p(1:expected_length);
        end
        % Now generate rules for each feature across all rules
        for r = 1:num_rules
            idx_start = (r-1)*num_membership_functions + 1;
            idx_end = r*num_membership_functions;
            rule_p = p(idx_start:idx_end); % Extract the probabilities for the current rule
            solution = [solution, randsample(1:num_membership_functions, 1, true, rule_p)];
        end
    end
    solutions{k} = solution;
end
In this adjusted code snippet, I've added a step to ensure that 'p' is resized to match the expected length 'expected_length'. This length is calculated based on the number of membership functions and the number of rules, ensuring that p aligns with the structure you're working with. When selecting a membership function for each rule, it's important to correctly partition the probability vector of the correct length of 'num_membership_functions', which should resolve the error you encountered in my opinion. 
Kindly have a look at the following documentation links to have more information on:
Hope that helps!
Balavignesh
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

