# Tutorial on GenOpt with Radiance

In this tutorial, I will set up an optimization problem of a building construction. The solution to this optimization problem will be provided by GenOpt. My intention is to show how to couple a new simulation engine (Radiance) to GenOpt, but it can be used as a reference to any optimization problem solved by GenOpt. I assume that you have a basic understanding on how to describe an optimization problem and some familiarity with GIGO (garbage in – garbage out) text simulation engines. Don’t expect tutorial photos because all work is done in text files.

GenOpt® is an optimization program for the minimization of a cost function that is evaluated by an external simulation program, such as EnergyPlus, TRNSYS, Dymola, IDA-ICE or DOE-2.

http://simulationresearch.lbl.gov/GO/

Radiance is a suite of programs for the analysis and visualization of lighting in design. The primary advantage of Radiance over simpler lighting calculation and rendering tools is that there are no limitations on the geometry or the materials that may be simulated. Radiance is used by architects and engineers to predict illumination, visual quality and appearance of innovative design spaces, and by researchers to evaluate new lighting and daylighting technologies.

Step 1 – Describing the optimization problem

I assume that I have an office room with dimensions of 5m X 7m with 3m height. The 5m wall is facing south and it has a window of 4.6m X 1.5m at 1m height from the floor. There are venetian blinds of 0.1m width outside of this window. I want to find the reflectance/angle of the blinds and the reflectance of the ceiling to get as close to 500lux as I can, at 2 points (2m and 3m distance from the window).

I don’t go on more details of the model because it is insignificant with the optimization problem itself. The problem isn’t very realistic (I should include glare dependence in my fitness function and a more accurate simulation model to get useful daylight optimization results) but I wanted to build a simple sample problem with 2 or more optimization variables and an objective function depending on 2 or more variables.

Building the objective function (or cost or fitness function):

I want as close to 500lux I can get at 2 points. If Poi1 is illuminance at point1 and Poi2 is illuminance at point2 then the simplest fitness function I can build is

f(x) = abs(Poi1-500)+abs(Poi2-500)

which we want to be as close to zero we can get (minimization problem). A real optimization problem might include a more sophisticated objective function which would be depended by glare and might increase exponential the objective function’s value outside desirable illuminance/glare values (implementation of constraints).

Design parameters:

The design parameters are independent continuous variables with box constraints.

1) Reflectance of the ceiling refl_ceil with 0 < refl_ceil < 1 with 0.2 step

2) Reflectance of the blinds refl_blinds with 0 < refl_blinds < 1 with 0.2 step

3) Angle of the blinds angle_blinds with -90 < angle_blinds < 90 (0 means horizontal slats) with 5 degrees step

Step 2 – Optimization simulation requirements

GenOpt is able to couple to any simulation engine that gets it’s input from text files and writes it’s output to text files as well. The basic GenOpt simulation steps are:

a) The selected optimization algorithm calculates the values of the design parameters (except for the initial start point which some algorithms make use of suggested values).

b) GenOpt creates the input text files (required by the coupled program) by changing keywords in some template text files to the values of the design parameters.

c) Then it starts the coupled simulation program (in a new file folder) using these newly created input files and waits for it to finish.

d) When the coupled program finishes, GenOpt retrieves result values from the output text files, calculates the objective function value and sends the result to the optimization algorithm to decide for the next values of the design parameters.

I’ll explain later how to setup GenOpt to do all that. My concern about coupling Radiance is how GenOpt will retrieve the result values from the output text files. Most simulation programs write output text files with delimiter keywords before the value. Energyplus, for example, creates a comma delimited file which uses a number before the result value and has an explanation line like this:

which means that there is line later like this:

241,5731670.34231873

which means that “241” which is Ideal Loads Air Heating Energy has the value of 5731670.34231873 [J]

Our (delimiter) keyword in that example (if we want to retrieve Ideal Loads Air Heating Energy for RunPeriod) will be “241,” and GenOpt will be smart enough to look for the last occurrence of “241,” and it will retrieve the value after that keyword. Version 3.1 of GenOpt has introduced a “FirstCharacterAt” setup which will let know the position of the value in the same line (if multiple values are in the same line where the delimiter keyword is).

For our Radiance coupling example, we are not able to retrieve result values by delimiter keywords because Radiance creates an output file with the values itself in each line without delimiter keywords. So I have to build a simple program to wrap Radiance, retrieve the results line by line and write a new output text file with delimiter keywords. GenOpt instead of starting Radiance, it will start my Radiance wrapper program and retrieve output values from my program. This way, my program will be general enough to be used to another situation like this. Most situations which use output files with delimiter keywords won’t need this step.

Edit Oct 4, 2012: As Khaled Nassar showed at 11th Radiance Workshop (2012 Copenhagen) there is a way to get delimited (formatted) results from Radiance by using -o option in rcalc.

Step 3 – Radiance simulation setup

I have built 3 text input Radiance files and one executive (.bat) with the start commands and options:

• room.rad – describes the materials, the geometry and the blinds of the scene.
• clear_sky.rad – describes the light source.
• pp1.pts – describes the 2 points where I want the result values.
• run.bat – executes the command “oconv clear_sky.rad room.rad>all.oct” which builds the file “all.oct” and then executes rtrace with options to calculate illuminance values at the 2 points (from pp1.pts) and print the values to the file results.dat. It can have an option to output results with headers but nothing I could find useful coupling to GenOpt (so I use the option without the headers).

When I execute run.bat I get output the all.oct and results.dat with the result values as text.

Step 4 – GenOpt simulation setup

I’ll try to explain in more details here, but I have to say that I use windows XP/7 and Unix simulations might be slightly different to setup. As far as I know, the only differences would be the configuration file .cfg which describes how to start the coupled program.

GenOpt needs 3 files (and the template files) to run the simulation:

1. the .ini file – has the current simulation options on how to retrieve/change values from the files and sets up the calculation of the objective function.
2. the .cfg file – which is the configuration file on how to execute the coupled program. Once configured, it remains the same for all simulations with the same coupled program.
3. the command file – which describes the design parameters with their initial values and sets up the optimization algorithm’s options.

.ini File:

This text file has a simulation section and an optimization section.

“Template” subsection (of files subsection….of simulation section) describes the input files which GenOpt will search to change some text keywords with the design parameters values. My example has all the design parameters keywords in the room_Template.RAD but the other files are needed for the simulation as well.

In “Files” subsection of the simulation section I put these texts which are explained at the end of each line with comments starting with “//”:

//GenOpt will use these files to create the Radiance simulation input files.

Template {

File1 = room_Template.RAD;         //I’ll explain later for this file

File2 = clear_sky_Template.rad;  //Just a renamed copy

File3 = pp1_Template.pts;             //Just a renamed copy

File4 = run_Template.bat;            //Just a renamed copy

}

//These files will be created for each Radiance simulation.

Input {

SavePath1 = Save;     //Optional parameter which means that it will save a copy of the input files in a “Save” folder.

SavePath2 = Save;

File3 = pp1.pts;

SavePath3 = Save;

File4 = run.bat;

SavePath4 = Save;

}

Log {

File1 = results.dat;      //GenOpt will search here for error keywords. I don’t know how Radiance prints any errors so I just put the results file. This means that we will not be prompted for any Radiance simulation errors (not very nice, I know).

}

Output {

File1 = delimitedResults.dat; //GenOpt will search for this file to retrieve the result values.

SavePath1 = Save; //Optional parameter to save a copy

}

Configuration {

File1 = “RadianceWinXP.cfg”;   //This file has the coupling information (how GenOpt can start a Radiance simulation)

}

CallParameter { // optional section

//Suffix = ********;

}

I describe here my objective function. It will be constructed from the bottom up, which means that GenOpt will search in the Output file (delimitedResults.dat) for delimiter “result1,” and “result2,” and the values will be set to Poi1 and Poi2. Next, it will substract 500 from each value. Next it will get the absolute value of the substraction, and finally it will add the 2 values to calculate the fitness value.

ObjectiveFunctionLocation

{

Name1 = fitness;

Function1 = “add( %absPoi1% , %absPoi2% )”;

Name2 = absPoi2;

Function2 = “abs( %Poi2-500% )”;

Name3 = absPoi1;

Function3 = “abs( %Poi1-500% )”;

Name4 = Poi2-500;

Function4 = “add( %Poi2% , -500 )”;

Name5 = Poi1-500;

Function5 = “add( %Poi1% , -500 )”;

Name6 = Poi2;

Delimiter6 = “result2,”;

Name7 = Poi1;

Delimiter7 = “result1,”;

}

Optimization {

Files {

Command {

File1 = command.txt; //This file has some more optimization information

}

}

} // end of configuration file

Someone who already knows how to use GenOpt will notice that GenOpt doesn’t have an abs() function. I had to add my own abs() function in GenOpt and recompile it. GenOpt’s manual has the details how to do this.

.cfg file:

This file has the error keywords of the coupling program and the command how to start the coupling program simulation (in command line). For my example I start my Radiance wrapper program to get delimited results. It’s a java program so it needs “java -jar” before it’s name to start.

command file:

This file describes the 3 design parameters and the optimization’s algorithm settings.

For example:

Parameter{

Name = refl_ceil;

Min = 0;

Ini = 0.5;

Max = 1;

Step = 0.2;

}

describes the design parameter of the ceiling reflection as a continuous parameter. GenOpt will look in the template files for a keyword named %refl_ceil% and it will change it to a value. This parameter has a minimum value of 0 and a maximum of 1. The initial algorithm value for this parameter will be set to 0.5, but not all algorithms use this setting. The value step will be set to 0.2. Again not all algorithms use this setting. Some algorithms like HookeJeeves will use this setting to “snap” values to a mesh grid of this value. If you want this kind of operation using the Genetic Algorithm then it will be wiser to describe this design parameter as a discrete parameter. Have in mind that this will not work for a hybrid operation between GA and HookeJeeves because HookeJeeves doesn’t use discrete parameters (it sets them to a fixed value extracted from the optimum solution). The solution is to do a global optimization with GA (or PSO) with the parameters described as discrete. When it finishes, change these to continuous at a finer mesh and do a local optimization with HookeJeeves, using as initial values the best values obtained by global optimization.

At this section:

OptimizationSettings{

MaxIte = 2500;

MaxEqualResults = 1000;

WriteStepNumber = false;

UnitsOfExecution = 0;

}

MaxIte is the maximum iteration number.

MaxEqualResults is self-explanatory.

WriteStepNumber is an advanced topic for optimization problems where you want to use penalty functions.

UnitsOfExecution is the suggested number for parallel simulations. If it’s equal to 0 then GenOpt will set this to the threads number of your workstation.

I don’t describe the algorithm’s settings here because it’s algorithm depended and I’ve used a genetic algorithm which hasn’t been implemented yet in GenOpt. I’ve coupled GenOpt with JGAP which is a genetics algorithm and genetics programming package. This again is an advanced topic which I might describe in another tutorial. You may copy the Algorithm settings from a command.txt file in GenOpt’s examples.

Finally, I have to build my templates files. I have only one file where the design parameters values exist, which is room_Template.RAD.

In this file I changed the values of the ceiling reflectance to the string %refl_ceil% , the blinds material reflectance changed to %refl_blinds% and blinds angle to %angle_blinds%.

I haven’t mentioned how I built my Radiance wrapper program (radWrap.jar) to get delimited results. This is a simple programming concept and you may find more information in programming tutorials. You can download all files I’ve used with the implementation code of radWrap.

To start the optimization simulation I need these Radiance settings files:

and these GenOpt settings files:

Start GenOpt, and open the optWinXP.ini. You will find some self-explanatory text files with the results when it’s finished. Be aware that depending on your machine and the optimization algorithm’s settings, the optimization runtime will vary from minutes to weeks.

Building engineer with optimization skills.
This entry was posted in Engineering Research and tagged . Bookmark the permalink.

### 16 Responses to Tutorial on GenOpt with Radiance

1. Salvo says:

Thank you, Vasilis!
I use GenOpt + E+, but your tutorial is very clear and effective!
I would ask you if you could explain how to couple GenOpt with Jgap, since it is quite tricky…
thank you so much!

• Hi Salvo,
JGAP is a java code package and coupling it requires a new major tutorial. The information needed to couple a new optimization algorithm in GenOpt is to find the variables the algorithm needs to know (ie pop_size, mutation rate etc.) and how to start that optimization algorithm. You then build a new algorithm class which extends GenOpt’s optimizer class. You will use the methods of the optimizer class to retrieve the input values of GenOpt’s input files, to send the design variables for fitness function calculation, retrieve that results and print them to the screen.
If you need a genetic algorithm for GenOpt send me a email and I can send you the JGAP algorithm or some others depending your problem.

2. Amir says:

Dear Vasilis,

I am interested in learning how to couple JGAP or any other genetic algorithm tool to GenOpt. Would you be able to send me some material to start with? I do not know anything about coding but I have a bit of experience in how to set up GenOpt with different software. I have used it with E+ and TRNSYS and plan to perform an optimization study in E+ right now with GA.

• Hi Amir,

Coupling GenOpt with an optimization package like JGAP requires programming skills. Coupling GenOpt with a simulation program is relatively easy and doesn’t require any coding, just text configuration files. Coupling an optimization package like JGAP requires deep understanding on how the specific package works and JAVA coding to extend the GenOpt classes. JGAP implementation has been used in a couple of research studies and I plan to upload the files in October with a worked example and some configuration guidence. If you are in a hurry or need help solving a specific problem, feel free to contact me via e-mail.

• Try using MOBO optimization software. It has number of genetic algorithms in it. It should be quite easy to use, if you have prior experiense with GenOpt. MOBO can be coupled with number of building simulation tools (like GenOpt).

Matti Palonen

3. navid says:

I mean GenOpt & Energyplus
thankyou alot buddy.

• Hi, I fixed a language presentation problem of my site. You may use the tutorial information and post back if you have any questions.

4. navid says:

ammmm,I am a new energyplus user,I am interested in learning how to model and optimize a building with E+ and GenOpt.
I will thank you to help or send me any link for tutorials or files to learn these softwares via my Gmail.
thankyou alot.

• You should start learning EnergyPlus and when you feel confident enough you may continue learning model optimization. You will find lots of information on EnergyPlus in the dedicated page where you download the program as well. You may start learning model optimization via GenOpt by using this tutorial and running the examples that come preinstalled with GenOpt. Feel free to post back if you have any specific questions/problems!

• navid says:

Hi dear Vasileios Machairas,Thanks very very much to guide me.Thanks.

5. Mikhail Ramos says:

Hi,
Really thanks for the tutorial it helps a lot even for DYMOLA, I don’t know if you could help me. GenOpt already makes the minimization, but only for one variable or in one point in time. Its a simple simulation of y=x1-x2 running from 0 to 2 seconds the lowest point being in second 1. But GenOpt only makes the minimization in second 2. Hope you could help. Thanks.

Is y=x1-x2 your cost function needing minimization?
Are x1 and x2 your design variables?
0 < x1,x2 < 2? If so, you minimum y would be at x1=0 and x2=2 (y=-2) What do you mean that it makes minimization in second 2? Do you have a multi-objective function? Are x1 and x2 your objective functions or your design variables? What is y?

6. Pierre says:

Hello,
thanks for the tutorial. Am working on GenOpt coupled with TRNSYS.
when you run an optimization GenOpt write an output file with optimisation variables and objectives function values obtain at each steps. how can specify more parameters to be write in the file for each steps? is it possible to write a different simulation result file for each steps of the optimization process.??
Pierre

• Hi Pierre,
If I remember correctly, you can specify as many design variables and as many functions you want to. GenOpt won’t check if you used a result in the objective function calculation. So, yes you can specify more result variables in “ObjectiveFunctionLocation“ section meaning more names and delimiters. Those names will be printed in the log file.
You need to alter GenOpt’s code (java) if you want them to be written to a different file. It’s not hard but it seems unnecessary because GenOpt’s results are delimited and easy to work on.

7. Pierre says:

thanks!!! I’ll check !!

8. Mario says:

Thanks a lot for the tutorial, I am working with TRNSYS and Genopt. Do you know a place to star to couple both using TRNSYS 17 and the latest version of GenOpt? Because I am getting a lot of error messages using TRNOPT, and it is caused by a type used for the Building Simulation within TRNSYS.
Mario