-
Notifications
You must be signed in to change notification settings - Fork 25
Random design
The second lesson shows how whole designs of random values with range constraints can be created using InPUT. We use the Examples project as a starting point and play with the code in here.
The design space someSpace.xml
could be extended by several numeric parameters with distinct ids. Many optimization algorithms come with a couple of at least 5 parameters. Now, we could do the following in the code after extending the space from lesson 1 appropriately and importing it:
int[] param1 = ds.next("paramId1"); float param2 = ds.next("paramId2"); double[][] param3 = ds.next("paramId3"); boolean[] param4 = ds.next("paramId4"); BigDecimal param5 = ds.next("paramId5");
Alternatively, we could call
IDesign design = ds.nextDesign("myDesign");
This will create a so called design, an object that contains the randomly created parameter values which can be retrived by the method getValue
as in
boolean[] param4 = design.getValue("paramId4");
Now, the IDesign container is not read-only, we can also set values to it, e.g.:
design.setValue("paramId4", param4);
But we have to ensure type safety! Otherwise we receive a runtime exception. In this example, using arrays, setting the value back does not change anything because the changes are automatically reflected in the container; arrays are returned as shallow copy. For primitives, the changes are not reflected in the container, it therefore makes sense to set values that way.
Parameters can be defined to be fixed. It follows an example in which parameter "paramId4" will in all dimensions remain true for the designs drawn from this design space:
<NParam id="paramId4" type="boolean[4]" fixed="true"/>
In case we are only interested in certain values of the array typed parameter, we can reach them by index (starting from 1 not 0!), so that
boolean interesting = design.getValue("paramId4.3");
returns the entry at array index 2, or
double[] interesting2 = design.get("paramId3.1");
the one dimensional double array on position 0 in the two dimensional array parameter (shallow!). The access also works for multidimensional indexing, e.g. "paramId3.1.3". Just make sure to not move out of bounds!
OK, now why would I want to store values in there? Well, designs can be exported to XML files. You might want to record your configuration and import it another time. You export the design to XML by
design.export(new XMLFileExporter("myDesign.xml"));
You can import it then again using the design space like
IDesign design = ds.impOrt(new XMLFileImporter("myDesign.xml"));
Even new designs can be created that we manually fill with values. This is done by
IDesign design = ds.nextEmptyDesign("myEmptyDesign");
In this lesson we learned how to create random and empty designs, and how to read values from and write values to them. We further learned how to import and export designs. In the next lesson, we will learn how to define value constraints in-between parameters for the creation of random designs.