How to use values from excel file in genetic algorithm
11 views (last 30 days)
Show older comments
Hello
Im trying to minimise the total cost incurred to bread retailer by optimising maximum level of bread stored by retailer.
Im using genetic algorthim. I have randomly generated demand values in an excel file that i need to read into so i can change the sets of 30 days of demand. I used readtable function, but the end data type doesnt seem to be compatible with the genetic algorithm.
Error using ()
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
Error in Copy_2_of_totalCostFunction (line 9)
D = Demand(1:30);
Error in optim.problemdef.fcn2optimexpr
Error in optim.problemdef.fcn2optimexpr
Error in fcn2optimexpr
Caused by:
Function evaluation failed while attempting to determine output size. The function might contain an error, or might not be well-defined at the automatically-chosen point. To specify output size without function evaluation, use 'OutputSize'.
I want to avoid copying and pasting 50 sets of demand data from the source file.
note:
Ive attached the code:-
Copy_2_of_check2 is the genetic algorithm
Copy_2_of_totalCostFunction is the objective function (where demand data is going)
InventoryUpdate is the accessory to the objective function
CSVdemand is the file that has the numbers to take for demand
0 Comments
Accepted Answer
Star Strider
on 6 Mar 2024
It produces a matrix that can be addressed just as any other matrix.
For example, to create the ‘D’ vector —
A = readmatrix('CSVdemand.csv')
D = A(:,1) % Read Column Vector
D = A(:,1).' % Transpose To Row Vector
.
.
2 Comments
Star Strider
on 11 Mar 2024
As always, my pleasure!
Thank you for the following up with your corrections as well.
More Answers (1)
Dheeraj
on 4 Mar 2024
Hi,
I understand you want to access data from CSV file in MATLAB to use in a custom algorithm.
It seems like the error you're encountering is due to how you're trying to access data from the table obtained through “readtable”. You need to access the table's columns using variable names rather than indices.
There are multiple ways to access table data, these are few examples
% Using variable names
T.Var2(2) % access 2nd element in column Var2
T.Var2(5) = T.Var3(4); % assign 4th entry in column 3 to 5th entry in column 2.
% Using brace indexing
T{2,2} % access 2nd element in 2nd column
T{5,2) = T.{4,3}; % assign 4th entry in column 3 to 5th entry in column 2.
You could go through the below documentation to know how to access table data in MATLAB.
See Also
Categories
Find more on Genetic Algorithm 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!