How to implement importing data from csv file in optimization algorithm?
조회 수: 5 (최근 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에서 Direct Search에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!