Pascal/Matlab/Structured Text & Genetic Algorithms

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1683 Views - Last Post: 31 August 2016 - 11:56 AM Rate Topic: -----

#1 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 01:41 AM

Hi guys,

I need a bit of a push in the right direction. I come from an electrical engineering background and I'm trying to develop a little control system as a home project. The main idea behind this project is that I want a genetic algorithm to be able to tune my controllers parameters.

The only problem is, my coding experience goes so far as Java, however the hardware I wish to implement the algorithm on only reads in Structured Text (Syntactically the same as Pascal). The other way I thought of implementing it is through matlab and then having that read/write to my hardware.

My question is, does anyone know of any good places to start learning Pascal, as there is very little information on Structured Text. Also, I hate to ask..does anyone know or have any example code for a very basic genetic algorithm that I could work from? To at least see the base structure of the code?

Thank you in advance
Max

Is This A Good Question/Topic? 0
  • +

Replies To: Pascal/Matlab/Structured Text & Genetic Algorithms

#2 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1248
  • View blog
  • Posts: 4,042
  • Joined: 12-May 09

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 05:48 AM

I don't know much about learning Pascal (I learned and forgot it over decade ago) or trying to learn about structured text, but I've done a fair bit of genetic algorithms.

I want to clarify what you're trying to do to see if I can help.

There is a thin line between genetic algorithms and genetic programming. Genetic programming can be thought of us a subset of genetic algorithms, where you are actually creating valid programs. It is much more difficult than genetic algorithms - in genetic algorithms, if you can define a good "string"ish representation of your problem and a good fitness function, the rest is "easy" - your output is just a state, a configuration, an input, rather than necessarily a program.

It *sounds* like what you plan to do is write a program to generate a program, which is genetic programming. Is that the only way to do this, or is it possible to generate a set of configuration values that will do what you want instead?

It's also worth mentioning that there are several methods of optimization like this that are called "metaheuristics" which we could apply to the problem at hand if you can provide more details. Parameter tuning does not, at the surface, sound like you need to do genetic programming.
Was This Post Helpful? 0
  • +
  • -

#3 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:15 AM

Hi Xclite,

Thank you for your reply. To break my problem down, I will be utilizing a Fuzzy Logic Controller. Now this controller has 25 parameters to it. Seeing as you know about GA's I'm sure you know the basics of FLC's.

What I was planning on doing was having an initial population of say, 5 and flashing an initial parameter set to each one. Each individual would go through a random mutation process and then be fed into my controller for evaluation...which would run for about 90 seconds. After which its score would be recorded and the next one would be passed through until all 5 have been done and the 5 different scores have been recorded. Now the part I'm kind of confused on is this fitness function, I was planning on just evaluating the parameter sets based on the mean square error in relation to the set point. Is that a valid option?

While I know there are better suited optimization processes for this, I'm just really keen on learning about GA's and would like to get one up and running and see it working.

I hope that made sense,
Max
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13553
  • View blog
  • Posts: 54,085
  • Joined: 12-June 08

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:23 AM

What is the fuzzy controller for? What does it do? What is the feedback? That is a large part of determining fitness.
Was This Post Helpful? 1
  • +
  • -

#5 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:29 AM

The controller will aim to maintain tank level. It will increase/decrease pump power based on the error.
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1248
  • View blog
  • Posts: 4,042
  • Joined: 12-May 09

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:31 AM

Your fitness function sounds like an ok starting point to me.

If you want to do this with genetic algorithms, which sounds like a fun experiment to me, you actually have a bit of flexibility.

It sounds like you're planning on evaluating a single generation. The power of genetic algorithms generally lies in its ability to approach a potentially optimal solution - to do so, you need to "evolve" your samples over time.

A bare-bones genetic algorithm:

1. generate a random population
2. for each member of population, evaluate fitness
3. if fitness is good enough OR number of iterations completed, return the most fit sample
4. randomly group population into pairs
5. for each pair, generate two children by "crossing" the genes. A common crossing method is to pick a point in the middle of your problem representation and combine the first half of one parent with the second half of the other for the first child, and then the opposite for the other.
6. iterate over each child's members, and mutate with some probably each "feature"
7. go to 2

The key there is to do this for quite a few generations, giving the algorithm time to do its work.

You can get way more elaborate:
1. Cull threshold: This can be used to eliminate from the gene pool any insufficiently fit samples. Accelerates pace towards *an* optimal solution, but may keep you stuck a local maximum
2. Elite children: If a solution hits a certain threshold, you can pass the exact same solution into the next generation. Prevents you from straying too far from a good solution, but also runs the risk of keeping you away from a more global maximum
3. You can introduce random spawns on occasion to prevent yourself from getting stuck.

I'd be interested to see how this turns out - the success of the algorithm generally depends on how suited the problem is to it - e.g. if you can take advantage of the fact that things located nearby are often related (though this isn't necessary), and if you can encode the problem well.

Really, what your challenge generally is:
1. Encode the solution you're trying to optimize as a string or other easily manipulated source
2. Evaluate the fitness.

You can easily tweak #2 if you aren't reaching optimal solutions.
Was This Post Helpful? 1
  • +
  • -

#7 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:41 AM

Yeah, for the moment I plan on just implementing a bare bones version. But there's my problem..while I can put it down on paper as pseudo-code, my lack of programming knowledge limits me from progressing further haha.

My main problems are:
1.) I'm not sure how to store the recorded values until the round is over.
2.) I'm not sure how to induce random numbers for mutation. I've looked at tutorials for random number generation on youtube, and they all utilized libraries within compilers (A luxury I don't have)
3.) Similarly to point 1. I'm not sure how to retain the parameter sets and bring it back for another round.

My Java experience is equal to 1 semester while I wan in uni and that was some years ago.

Thank you for your reply!
Max
Was This Post Helpful? 0
  • +
  • -

#8 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1248
  • View blog
  • Posts: 4,042
  • Joined: 12-May 09

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:46 AM

I shall quote Rick and Morty:

Quote

Show me what you gotttt!


We can try to help you with keeping track of your results.
Was This Post Helpful? 0
  • +
  • -

#9 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 07:59 AM

Not a problem Xclite, let me just compile my work. At the moment its all just scribbles on paper. I will report back asap.

Thank you again! Appreciate your time.
Was This Post Helpful? 0
  • +
  • -

#10 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 08:25 AM

ParameterSetCount:=1
RoundCount:=1
Set_Score:= Real

****Flashing Initial Population****
FOR Individuals:= 1 TO 5 DO
Gene1:=0
Gene2:=0
Gene3:=-5
Gene4:=-10
Gene5:=10
Gene6:=5
Gene7:=0
Gene8:=-5
Gene9:=-10
Gene10:=0
Gene11:= Set_Score

END FOR

****Mutate Procedure****

FOR Individuals:= 4 TO 5 DO
	FOR Gene:= 1 TO 10 DO
		Gene*RandomConstant  ****Const will be between 0.97-1.03****
Gene1:=MGene1:= a
Gene2:=MGene2:=b
Gene3:=MGene3:=c
Gene4:=MGene4:=d
Gene5:=MGene5:=e
Gene6:=MGene6:=f	****Whatever the numbers are****
Gene7:=MGene7:=g
Gene8:=MGene8:=h
Gene9:=MGene9:=i
Gene10:=MGene10:=j

	END FOR
END FOR

****Write to Controllers****

CtrlP1:=MGene1
CtrlP2:=MGene2
CtrlP3:=MGene3
CtrlP4:=MGene4
CtrlP5:=MGene5
CtrlP6:=MGene6
CtrlP7:=MGene7
CtrlP8:=MGene8
CtrlP9:=MGene9
CtrlP10:=MGene10

RunTest:=True


****System Check****

If TestDone THEN
	Gene11:= Set_score;
	IF (ParameterSetCount=5) THEN
		***Sort Operation***
		FOR Individuals:=1-5



Hi XClite, this is what I have so far..its a mix of Structured Text/Psuedo :/
Was This Post Helpful? 0
  • +
  • -

#11 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1248
  • View blog
  • Posts: 4,042
  • Joined: 12-May 09

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 09:39 AM

So you're looping from 1 to 5 and setting each Gene to a value, overwriting it each time. Are you familiar with arrays?
Was This Post Helpful? 0
  • +
  • -

#12 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 09:45 AM

I'm familiar with the concept (An object that can store a group of values). Having a quick read from one of my old Java books on how to actually use it.
Was This Post Helpful? 0
  • +
  • -

#13 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1248
  • View blog
  • Posts: 4,042
  • Joined: 12-May 09

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 10:09 AM

The reason I ask is that we probably want something to store our population in, unless you want to declare N variables (N being the size of your population) instead.
Was This Post Helpful? 0
  • +
  • -

#14 EEng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-August 16

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 10:17 AM

For the moment, I want to keep the code fairly primitive...Structured Text isn't a very advanced language. If I cant get it all to work, then I'll go back and refine the code. As for the variables, so I would have to declare Gene1 like so:

Gene1=Int
etc
etc
?

Sorry I got confused there. I meant individuals.
What would I declare the 'individuals' as though?
Was This Post Helpful? 0
  • +
  • -

#15 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1248
  • View blog
  • Posts: 4,042
  • Joined: 12-May 09

Re: Pascal/Matlab/Structured Text & Genetic Algorithms

Posted 27 August 2016 - 10:21 AM

So each candidate for a given solution consists of each gene. Each individual is a collection of genes. If you plan to declare them individually, you have to create a record type "individual" and then give it a gene for each one. If that's too complicated, you're going to have to do:

Gene1_1 = Int
...
Gene1_13 = Int

Gene2_1 = Int



Which will be very tedious.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2