How to use values from excel file in genetic algorithm

10 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 6 Mar 2024
Use the readmatrix function instead of readtable since there are no variable names.
It produces a matrix that can be addressed just as any other matrix.
For example, to create the ‘D’ vector —
A = readmatrix('CSVdemand.csv')
A = 1200x40
21 37 1 16 8 5 10 18 20 27 21 35 11 44 2 34 21 28 8 10 41 49 16 35 44 45 5 2 9 44 50 38 15 40 6 23 46 15 15 7 1 34 11 14 25 3 29 8 30 35 6 21 35 21 3 27 34 26 48 30 45 32 38 18 14 45 22 49 34 32 6 48 23 29 21 12 46 29 1 31 17 27 45 18 46 32 1 47 35 50 1 2 2 13 44 27 28 43 7 14 30 49 29 1 41 12 41 20 44 38 28 7 3 7 3 6 12 36 28 1 12 25 31 42 8 1 4 25 31 29 16 50 29 20 28 38 34 14 4 19 32 11 38 4 14 41 10 32 27 47 48 28 46 33 20 25 31 28 47 46 20 49 9 7 7 26 2 48 42 1 9 17 7 41 18 48 30 44 43 46 16 45 29 10 40 31 3 22 34 46 1 49 19 49 31 42 29 32 15 30 38 43 38 35 44 17 34 23 20 21 44 46 34 14 13 43 27 41 29 37 26 39 29 24 18 4 19 4 50 10 41 44 35 29 9 24 18 12 30 16 21 31 12 6 26 24 8 32 28 33 8 38 12 26 40 2 17 44 43 27 44 48 42 43 5 33 36 31 40 2 16 8 46 28 49 32 50 28 27 7 18 2 9 38 2 19 44 35 35 10 23 30 50 11 13 14 38 23 3 26
D = A(:,1) % Read Column Vector
D = 1200x1
21 50 45 1 12 48 16 44 21 16
D = A(:,1).' % Transpose To Row Vector
D = 1x1200
21 50 45 1 12 48 16 44 21 16 48 31 17 41 2 34 5 37 22 10 45 9 19 8 50 17 3 15 16 22
.
.
  2 Comments
Gauri
Gauri on 11 Mar 2024
Edited: Gauri on 11 Mar 2024
thank you for your response!
just for the record,
I needed a 30x1 matrix, so i used :
A = readmatrix('CSVdemand.csv')
A = 1200×40
21 37 1 16 8 5 10 18 20 27 21 35 11 44 2 34 21 28 8 10 41 49 16 35 44 45 5 2 9 44 50 38 15 40 6 23 46 15 15 7 1 34 11 14 25 3 29 8 30 35 6 21 35 21 3 27 34 26 48 30 45 32 38 18 14 45 22 49 34 32 6 48 23 29 21 12 46 29 1 31 17 27 45 18 46 32 1 47 35 50 1 2 2 13 44 27 28 43 7 14 30 49 29 1 41 12 41 20 44 38 28 7 3 7 3 6 12 36 28 1 12 25 31 42 8 1 4 25 31 29 16 50 29 20 28 38 34 14 4 19 32 11 38 4 14 41 10 32 27 47 48 28 46 33 20 25 31 28 47 46 20 49 9 7 7 26 2 48 42 1 9 17 7 41 18 48 30 44 43 46 16 45 29 10 40 31 3 22 34 46 1 49 19 49 31 42 29 32 15 30 38 43 38 35 44 17 34 23 20 21 44 46 34 14 13 43 27 41 29 37 26 39 29 24 18 4 19 4 50 10 41 44 35 29 9 24 18 12 30 16 21 31 12 6 26 24 8 32 28 33 8 38 12 26 40 2 17 44 43 27 44 48 42 43 5 33 36 31 40 2 16 8 46 28 49 32 50 28 27 7 18 2 9 38 2 19 44 35 35 10 23 30 50 11 13 14 38 23 3 26
D = A(1:30,1)
D = 30×1
21 50 45 1 12 48 16 44 21 16
D = A(1:30,1).'
D = 1×30
21 50 45 1 12 48 16 44 21 16 48 31 17 41 2 34 5 37 22 10 45 9 19 8 50 17 3 15 16 22
Star Strider
Star Strider on 11 Mar 2024
As always, my pleasure!
Thank you for the following up with your corrections as well.

Sign in to comment.

More Answers (1)

Dheeraj
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.
  1 Comment
Gauri
Gauri on 6 Mar 2024
Thank you for your response
However my csv file doesnt have any variable names. It is just a 1200x90 block of numbers. all of these numbers denote quantity of bread units sold per day (demand). I want a way to link the csv file into MATLAB so that i can avoid copying and pasting 30 odd such numbers each time when i want a result for a different demand stream. While i can use a randi with seed to get such values, my teacher requires me to use numbers from this 1200x90 block for my result (ie. the objective value) to be deemed acceptable.
Because of the absence of variable names, or different types of variables as in any normal tables, im having diffculty following the example that you have very kindly linked. Should I try to assign random names to columns to call the numbers more easily?
If you could please provide further guidance on my situation it would be very helpful.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!