How to implement importing data from csv file in optimization algorithm?
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I have attached the code used for PSO algorithm for 3D box packing. I am finding difficulty to modify the code to integrate with importing data from 'presents.csv' and also provide best solution for arrangements of the boxes on the grid, for example as '50 - GA (10-Apr-2024 03.48.47).csv'.
please do share some ideas on this. thanks.
pso.m
0 个评论
回答(1 个)
Samay Sagar
2024-4-25
Based on my understanding, the “CreateModel” function needs be modified to accept an array of box dimensions, transforming the approach to accommodate three-dimensional considerations.
Here’s how you can modify the “CreateModel” function:
%
% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPAP105
% Project Title: Solving Bin Packing Problem using PSO, FA and IWO
% Publisher: Yarpiz (www.yarpiz.com)
%
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
%
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
%
function model = CreateModel(boxes)
model.boxes = boxes; % Store the original box dimensions
model.v = prod(boxes, 2); % Calculate volume of each box as product of dimensions
model.n = size(boxes, 1); % Number of boxes
model.Vmax = 30;
end
In pso.m, you can use the “readtable” function to read data from CSV file and pass it to the “CreateModel” function.
opts = detectImportOptions('presents.csv');
presentsData = readtable('presents.csv', opts);
boxes = table2array(presentsData(:, {'boxLengths', 'boxWidths', 'boxHeights'}));
model = CreateModel(boxes); % Create Bin Packing Model
To save the best solution in a CSV file like '50 - GA (10-Apr-2024 03.48.47).csv', you can add a code snippet at the end of your PSO algorithm that writes the best solution to a CSV file. Assuming “BestSol” contains the best arrangement of boxes, including their positions, you can format this data into a table and use the “writetable” function:
bestSolTable = array2table(BestSol.Position); % Convert your solution to a table
filename = sprintf('%d - PSO (%s).csv', nPop, datestr(now, 'dd-mmm-yyyy HH.MM.ss'));
writetable(bestSolTable, filename);
For more information about “readtable”, “writetable” and “array2table”, you can refer the following documentation:
Hope this helps!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!