B.4How To Implement New KI Class
B.4.1Implementing LKI class
We use an example of TTestLKI, containing a copy
of the best gene from the local subpopulation. See also
Reference Manual for detailed description of used methods.
- Register unique ID of your LKI in file kinames.h in module basic.
Decide which layer of child process will be responsible for creation of your LKI class. Insert the ID definition
in proper section (each layer has range of IDs). We suggest to implement only new KI
classes created in the external layer.
Our example: ID is KIL_TEST and it is in external layer section.
- Derive a new class from TKernelInfo class. Decide which type of information
will your LKI class contain and implement proper attributes.
Our example: attribute TGen *best; contains the copy of best gene.
- Derive the constructor to fill the proper information into the LKI object.
Call the predecessor's constructor with proper ID.
Our example: TTestLKI::TTestLKI(TGen& bg) : TKernelInfo(KIL_TEST)
- Implement virtual destructor.
- Implement (de)serialization methods to enable object
transportation via network.
- Add construction of your LKI object to CreateKI() method of layer responsible
for creating your LKI class. Since this action the layer know how to create your
new LKI object.
Our example: in TDiffusionLGA::CreateKI(uint id, int period) add
statement:
case KIL_TEST: return new TTestLKI(RetrPop(1));
Note: RetrPop(1) returns the best gene of subpopulation.
B.4.2 Implementing GKI class
We use an example of TTestGKI, containing a bag of
LKIs (just one LKI from every child) and adds global info about the global
best fitness. See also Reference Manual for detailed
description of used methods.
- Decide which LKIs will your GKI contain and implement them (see previous
section).
Our example: LKI with ID KIL_TEST
- Register unique ID of your GKI in file kinames.h in module basic. Insert it in external layer section.
Our example: ID is KIG_TEST.
- Derive a new class from TGlobalKI class.
Optional: decide which type of global information will your GKI (additional to LKI objects) contain
and implement it as attributes. Also implement method Compute()
for computation of global info from arrived LKIs (contained in bag). Use
methods TBag::Get(), TBag::GetFirst() and TBag::GetNext()
to retrieve LKIs stored in the bag.
Our example: We add attribute TFit bestfit; containing the
global best fitness value. In Compute() method we find the best
fitness value from copies of best genes contained in LKIs stored
in bag.
- Override constructor and destructor.
Our example:
TTestGKI::TTestGKI(int period) : TGlobalKI(KIG_TEST,period)
- Override method Init() and initialize the bag in it (here specify which
LKIs will your GKI request). This will automatically register these LKIs
in all local collectors.
Our example: we register only one LKI with ID KIL_TEST
-
Override method TGlobalKI::CreateKI() and deserialize there all
LKIs you have registered previously. So your GKI can deserialize all necessary
LKI incoming via network.
-
Implement (de)serialization methods of your class to enable its transport
via network.
-
In external layer of the parent process override
CreateKI() method and add construction of your GKI there. Since
this action the process know how to create your new GKI.
Our example: in TParentLayer::CreateKI(uint id, int period) add
statement:
case KIG_TEST: return new TTestGKI(period)
- In in application process that should receive your GKI override TAppCollector
class and override its DeserializeKI() method. Add deserialization
of your GKI there. Since this action the application know how to create
your new GKI.
Our example: in TAppLayer::DeserializeKI(uint id, BYTE** rawbytes,
uint len) add statement:
case KIG_TEST: return new TTestGKI(rawbyte,len)
-
Optional: You can override method TGlobalKI::IsReady()
to change the retrieval condition. The default condition is following:
GKI is sent to the requester process, whenever all LKIs have
arrived.
-
Optional: You can override method TGlobalKI::Spoil()
to change the way of discarding LKIs in the bag. By default all LKIs are
destructed.