# Why am I getting a Parse error for a sequence?

1 view (last 30 days)
Bohdan Schatschneider on 27 Jul 2021
Commented: Star Strider on 28 Jul 2021
I get the following errors when trying to write the code below: "Parse error at 1:usage may be invalid Matlab syntax"
"File: RunGA.m Line: 44 Column: 12
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses."
Code:
for it 1:MaxIt
end
The 'it' stands for iteration, the 1 is for iteration 1, and MaxIt is for the maximum number of iterations (which is 100 defined elsewhere).
The complete 'for' loop is here if needed:
%Main Loop of GA: (this does crossover and mutation)
for it 1:MaxIt %this starts 'for' loop and runs for MaxIt times.
popc = repmat(empty_individual, nC/2, 2); %generates the population of children/offspring. Two's make it easire to sort in matrix.
%Crossovers to be performed:
for k = 1:nC/2
%Select Parents for Crossover/offspring production.
q = randperm(nPop); % here we are selecting to parents at random for nPop and also ensuring that they are different parents.
p1 = pop(q(1)); %this takes the 1st member of the population from the random permutation above as parent #1 (p1).
p2 = pop(q(2)); %this takes the 2nd member of the population from the random permutation above as parent #2 (p2).
%Perform Crossover: uses definitions from SinglPoinCrossover file to define offspring and parents.
[popc(k, 1).Position, popc(k, 2).Position] = SinglePointCrossover(p1.Position, p2.Position); %popc(k, 1).Position = y1 and popc(k, 2).Position = y2, and 'parent1.Position' and 'parent2.Position' is x1 and x2 from SinglePointCrossover.m.
%line above tells us the posiiton of the 1st offspring in the kth column and 1st row and the 2nd offspring in the kth column and 2nd row
end
%Covert popc to single-column matrix: get popc ready for mutation step.
popc = popc(:); %the '(:)' Fn turns any matrix into a single column matrix.
%Mutation: this randomly changes bits in offspring genetics.
for l = 1:nC %this is an iteration of all members of popc.
%Perform Mutation: This uses the code in 'Mutate.m' to change the value of the lth offspring.
popc(l).Position = Mutate(popc(l).Posiiton, mu); %this mutates the lth element of our offspring pop (popc)with mutation rate, mu.
%Evalute the Cost of popc(l):
popc(l).Cost = CostFunction(popc(l).Position);
%Compare Solution to Best Solution Ever Found:
if popc(l).Cost < bestsol.Cost
bestsol = popc(l); %this says if pop(l) < bestsol, pop(l) becomes bestsol(this is garaunteed because of bestsol is initially = infininty)%b/c of this, bestsol gets updated regularly as better solutions are found.
end
end
%Update best cost of iteration: this takes the costs from pop(i)and popc(l) for each iteration and establishes the best cost value.
bestcost(it) = bestsol.Cost;
%Merge Populations: Here we'll merge pop(i) and popc values to sort and evaluate.
pop = [pop; popc]; %here we concatonated the pop and popc 1x1 matrices vertically (note the ';' in the concatonation).
end

Star Strider on 28 Jul 2021
It’s missing an equal (=) sign:
for it = 1:MaxIt %this starts 'for' loop and runs for MaxIt times.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 28 Jul 2021
My pleasure!
.

R2021a

### Community Treasure Hunt

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

Start Hunting!