![]() | A step by step guide | Editing Optimization Objects | ![]() |
When defining your optimization problem as chromosomes and genes, you will probably wish to define constraints on the acceptable (legal) genes values. There are two ways of defining constraints:
1- Defining constraints using Optimization Properties
The chromosome and gene properties pages allow the user to impose the following constraints:
·The sequence chromosome has a set of mutually constrained genes. Each gene has to have a unique value between 1 and the number of genes.
·Multiple sequence chromosomes allow the definition of a number of groups of genes with each group having unique genes values within it.
·The number of genes within a sequence chromosome can be adjusted.
·The values of genes in non-sequence chromosomes can be constrained between minimum and maximum values.
·The sum of genes in a non-sequence chromosome can be constrained to a value.
Some of the properties are exposed and can be accessed at runtime. Typically these commands would be in a procedure prior to the Optimisation object or in the object's Optimization Start event. The easiest way to enter the properties is to use the command builder. In the procedure or event, right mouse click and select the Optimization object which will then be displayed. Add a full stop to the end of the name and press <Ctrl> and <Space> simultaneously to select a property from the list. If the property is an array then add the element and repeat the procedure. For example, using the name costOpt the following would appear in the editor:
costOpt
Add a full stop, press <Ctrl> <Space> and select chromosomes[ which would result in:
costOpt.chromosomes[
If we are interested in the first chromosome, which is a Variables type, then add the element and the a full stop:
costOpt.chromosomes[1].
And then press <Ctrl> <Space> and select genes[
costOpt.chromosomes[1].genes[
Add 1]. to reference the first gene of this chromosome
costOpt.chromosomes[1].genes[1].
Press <Ctrl> <Space> and select max. This will allow you to assign a value to the maximum range property
costOpt.chromosomes[1].genes[1].max = 5000
The following properties can be accessed at runtime:
·The number of genes within a Sequence chromosome. For example:
@Assign costOpt.chromosomes[1].nGenes = 5
·The values of genes in Variables (non-sequence) chromosomes can be constrained between minimum and maximum values using the min and max properties. For example:
@Assign costOpt.chromosomes[1].genes[1].max = 5000
@Assign costOpt.chromosomes[1].genes[1].min = 50
·The sum of genes in a Variables (non-sequence) chromosome can be constrained to a value:
@Assign costOpt.chromosomes[1].hasSum = true
@Assign costOpt.chromosomes[1].sum = 50
When Property constraints are defined the evolution engine within Knowledge Builder ensures that any chromosome generated randomly, by crossover or by mutation is legal (i.e. it satisfies constraints). It effectively achieves this by repairing illegal chromosomes or by using operators which ensures legal children from legal parents.
2- Defining constraints as part of the variable to be optimized
In some cases the facilities for defining constraints within the optimization view are unable to express the constraints. Alternatively, the constraints on the genes may be soft, i.e. not "lethal" but are less desirable. In both of the previous cases you can achieve your objectives using the procedural script commands tied to the attribute to be optimized. For example, if we wish to minimise the variable expenditure, then we first define how it is calculated from the genes. The next stage is to modify the calculations to allow for constraints. For lethal constraints, we can use the IF command in Knowledge Builder to add a very large value to expenditure if certain conditions are true. This in effect gives this chromosome such a bad cost that it will be overlooked in the parent selection process and ultimately 'killed off'.
Adding soft constraints needs more thought. First we have to modify the calculations so as to add a gradual penalty to expenditure as the genes deviate from certain desirable constraints. However, we have to ensure that the penalties we add are not out of proportion to achieving our main objective, which is to minimise expenditure. Often the best way of achieving this is to put a monetary value (cost) on any subjective constraints we wish to add. If your optimization problem involves maximising a variable then constraints can be introduced by adding negative penalties.
As an alternative to using Knowledge Builder @Commands for the main cost function (procedure), you can use VBScript syntax commands. These are recommended when large sequences of commands are needed in one procedure and the maximum execution speed at run time is required - typically the case with optimization applications.
Note that introducing constraints using the script commands of the attribute to be optimized is a far slower process during run time than defining these constraints (if possible) via the optimization window.