SlideShare a Scribd company logo
1 of 10
Download to read offline
Object-OrientedModeling
with OptimJ
OptimJ is a radically new optimization modeling language designed as
a Java extension with Eclipse integration, in contrast with all existing
modeling languages that are home-brewed domain-specific languages.
In this whitepaper, we show how the object-oriented techniques
inherited from Java enable abstraction and reuse of optimization code.
OptimJ technical overview
OptimJ™ is an extension of the Java™ programming language with language support for writing
optimization models and powerful abstractions for bulk data processing. The language is supported by
programming tools under the Eclipse™ 3.2 environment.
• OptimJ is a programming language :
o OptimJis an extension of Java 5
o OptimJ operates directly on Java objects and can be combined with any other Java classes
o The whole Java library is directly available from OptimJ
o OptimJ is interoperable with standard Java-based programming tools such as team
collaboration, unit testing or interface design.
• OptimJ is a modeling language :
o All concepts found in modeling languages such as AIMMS™, AMPL™, GAMS™,
MOSEL™, MPL™, OPL™, etc., are expressible in OptimJ.
o OptimJcan target any optimization engine offering a C or Java API.
• OptimJ is part of a product line of numerous domain-specific Java language extensions, a novel
approach of “integration at the language level” made possible by Ateji proprietary technology.
www. .com
(C) Ateji. All rights reserved. 2008-02-27 Page 1
The Diet Model
Our running example will be the Diet model, a canonical example that appears in the frst pages of most
textbook about optimization techniques. The diet problem consists in finding an appropriate mix of foods
satisfying minimum and maximum weekly intakes of vitamins, for a minimal cost.
Textbook version
Here is how the Diet model is typically presented in a textbook. We have adapted the version given in "The
AMPL book" to the OptimJ syntax, changing a few surface details but keeping the same overall
presentation. First we have the model data:
Then the decision variables, constraints and objective:
This formulation is fine for a textbook and is a perfect example of the power of algebraic modeling
languages: it is concise while easily readable, close to the mathematical formulation of the problem.
(C) Ateji. All rights reserved. 2008-02-27 Page 2
// vitamin names
String[] VTMN = { "A", "B1", "B2", "C" };
// food names
String[] FOOD = { "BEEF", "CHK", "FISH", "HAM",
"MCH", "MTL", "SPG", "TUR" };
// cost of each food
double[] cost = { 3.19, 2.59, 2.29, 2.89,
1.89, 1.99, 1.99, 2.49 };
// minimum and maximum intakes of each food
float[] f_min = { 0, 0, 0, 0, 0, 0, 0, 0 };
float[] f_max = { 100, 100, 100, 100, 100, 100, 100, 100 };
// minimum and maximum intakes of each vitamin
float[] n_min = { 700, 700, 700, 700 };
float[] n_max = { 10000, 10000, 10000, 10000 };
// amount of each vitamin brought by each food
float[][] amt = {
{ 60, 8, 8, 40, 15, 70, 25, 60 },
{ 20, 0, 10, 40, 35, 30, 50, 20 },
{ 10, 20, 15, 35, 15, 15, 25, 15 },
{ 15, 20, 10, 10, 15, 15, 15, 10 }};
var double[] Buy[int j : FOOD] in f_min[j] .. f_max[j];
minimize
sum {int j : FOOD} { cost[j] * Buy[j] };
constraints {
forall(int i : VTMN) {
n_min[i] <= sum {int j : FOOD} {amt[i][j] * Buy[j]};
sum {int j : FOOD} {amt[i][j] * Buy[j]} <= n_max[i];
}
}
One thing that is missing in the initial textbook version is the code to actually solve this model and print a
solution. In OptimJ, a model is just a specialized Java class, and like any other class it can have fields,
methods and constructors. First we define a model containing the data and the problem given above, and
provide this code inside a main() method as follows:
Identify model parameters
In the previous version, all model parameters are given inline as field initializations in the source code:
• no difference is made between which fields are parameters and which are not
• the only parameter-passing mechanism is modifying fields from the outside
Indeed, this is how today's modeling languages are designed. Most are interpreted languages where
parameter-passing is actually a text-replacement operation, generating a new source code with different
inline parameters, after what the whole source code is interpreted again.
When a mechanism is available for modifying fields from the outside (this is required for instance in
column generation algorithms where one must deal with multiple instances of multiple models), this leads
to spaghetti code where it quickly becomes impossible to track what has been initialized or not. This is a
sure recipe for introducing very nasty bugs.
(C) Ateji. All rights reserved. 2008-02-27 Page 3
model DietModel // a model a just a special kind of class
solver lpsolve // the solver name is provided here
{
... data ...
... variables, constraints and objective ...
public static void main(String[] args)
{
// instanciate the model (this is like instanciating a class)
DietModel m = new DietModel();
// extract the model into the solver
m.extract();
// solve the model
if (m.solve()) {
// print the objective value
System.out.println("objective value " + m.objValue());
// print the value of each variable
for(int j : m.FOOD) {
System.out.println(
m.FOOD[j] +
" " +
m.value(m.Buy[j])
);
}
} else {
System.out.println("no solution");
}
}
}
The parameter-passing mechanism of OptimJ is the same as in Java: define a constructor for the model.
Now the model parameters are not given inline in the model code anymore, but passed via the constructor.
As a first example, we define them inline in the main() method:
(C) Ateji. All rights reserved. 2008-02-27 Page 4
// define a constructor for the diet model
DietModel(
String[] VTMN,
String[] FOOD,
double[] cost,
float[] f_min,
float[] f_max,
float[] n_min,
float[] n_max,
float[][] amt)
{
...
}
public static void main(String[] args)
{
// model parameters are now defined here
// rather than inside the model
String[] VTMN = { "A", "B1", "B2", "C" };
String[] FOOD = { "BEEF", "CHK", "FISH", "HAM",
"MCH", "MTL", "SPG", "TUR" };
double[] cost = { 3.19, 2.59, 2.29, 2.89,
1.89, 1.99, 1.99, 2.49 };
float[] f_min = { 0, 0, 0, 0, 0, 0, 0, 0 };
float[] f_max = { 100, 100, 100, 100, 100, 100, 100, 100 };
float[] n_min = { 700, 700, 700, 700 };
float[] n_max = { 10000, 10000, 10000, 10000 };
float[][] amt = {
{ 60, 8, 8, 40, 15, 70, 25, 60 },
{ 20, 0, 10, 40, 35, 30, 50, 20 },
{ 10, 20, 15, 35, 15, 15, 25, 15 },
{ 15, 20, 10, 10, 15, 15, 15, 10 }};
// the parameters are now passed explicitely
// to the model via the constructor call
DietModel m = new DietModel(VTMN, FOOD, cost, f_min,
f_max, n_min, n_max, amt);
... same as before ...
}
Abstract the model parameters
The diet model constructor now takes eight different parameters. This doesn't sound right, as the eight
parameters are closely related. A better formulation is to group all parameters of the diet model into a
DietData class:
The final keywords in this example are not mandatory but are a good programming practice. They allow
the compiler to check that everything has been initialized properly, thus helping catching potential bugs
early. The model constructor must be updated in order to accomodate the change:
All parameters must now be prefixed, for instance FOOD is replaced with d.FOOD.
An alternative approach that does not require prefixing is to cache a local copy of the parameters in fields.
Again, the final keyword is here for ensuring that everything is properly initialized:
(C) Ateji. All rights reserved. 2008-02-27 Page 5
public class DietData
{
final String[] VTMN;
final String[] FOOD;
final double[] cost;
final float[] f_min;
final float[] f_max;
final float[] n_min;
final float[] n_max;
final float[][] amt;
... class constructors come here ...
}
public DietModel(DietData d)
{
this.d = d;
}
var double[] Buy[int j : d.FOOD] in d.f_min[j] .. d.f_max[j];
public DietModel(DietData d)
{
// cache a local copy of the parameters
FOOD = d.FOOD;
f_min = d.f_min;
f_max = d.fmax;
...
}
var double[] Buy[int j : FOOD] in f_min[j] .. f_max[j];
Access model data using any Java API
Now that the model parameters have been abstracted away from the model, adding a new data source is
simply a matter of writing a new constructor for the DietData class. There will be no change in the model
itself.
In the following sections, we show three examples where model data is read from three different classical
data sources, using standard Java APIs:
– JDBC for accessing SQL databases
– HSSF for accessing Excel sheets
– SAX for accessing XML files
Of course any other available Java API , whether standard or home-grown, can be used as well.
Sending solutions back to the application is done in a similar way using the same APIs, we have omitted
this part for brievity.
Abstracting away the diet model parameters into a DietData class marks the limit between the
'optimization' part and the 'programming' part. A properly designed model data class provides a perfect
means of communication between an optimization expert and a programming expert.
Import data from an SQL database
If this example, the diet model data is stored in a SQL database as three tables 'Foods', 'Amounts' and
'Vitamins'. Here we create a new constructor for the DietData class using the standard JDBC API.
(C) Ateji. All rights reserved. 2008-02-27 Page 6
public DietData(Connection c) throws SQLException
{
// Read table 'Foods'
int i = 0;
Statement statement = c.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT * FROM Foods");
statement.close();
while (resultSet.next()) {
FOOD[i] = resultSet.getString("food");
cost[i] = resultSet.getDouble("cost");
f_min[i] = resultSet.getFloat("fmin");
f_max[i] = resultSet.getFloat("fmax");
i++;
}
resultSet.close();
// Read table 'Amounts'
... similar code, omitted for brievity ...
// Read table 'Vitamins'
... similar code, omitted for brievity ...
}
Import data from an Excel sheet
In this example, the model data is stored in an Excel sheet as shown below:
Having our diet model read data from this sheet is simply a matter of adding the appropriate constructor in
the DietData class. Here we use the standard HSSF API from the Apache project.
(C) Ateji. All rights reserved. 2008-02-27 Page 7
class DietData
{
// A new constructor to read data from an Excel sheet
// based on the HSSF API (wb is a reference to the sheet)
DietData(HSSFWorkbook wb)
{
VTMN = HSSFArea.make(wb, "Vitamins").toStringArray();
FOOD = HSSFArea.make(wb, "Foods").toStringArray();
cost = HSSFArea.make(wb, "Cost").toDoubleArray();
f_min = HSSFArea.make(wb, "FMin").toFloatArray();
f_max = HSSFArea.make(wb, "FMax").toFloatArray();
n_min = HSSFArea.make(wb, "VMin").toFloatArray();
n_max = HSSFArea.make(wb, "VMax").toFloatArray();
amt = HSSFArea.transpose(HSSFArea.make(wb,"Amount")
.toFloatArrayArray());
}
... as before ...
}
Import data from an XML file
Once again, we define an additional constructor to the DietData class, using a standard Java XML API.
Abstract the logical structure
A further improvement, typical of object-oriented programming, is to abstract over the logical structure of
the model parameters.
Our model parameters consist of eight different arrays with no apparent structure of relationship. The fact
that the i-th entry in the f_min array is the minimum amount for the i-th entry in the FOOD array is stated
nowhere. It is only implicit in the mind of the developer.
This kind of formulation is favored by mathematicians: it is concise, and the possible semantic ambiguities
are not considered relevant because of the small size of problems.
From a software engineering perspective however, this is terrible:
– what is implicit in the mind of a programmer may not be implicit, or differently implicit, in the mind of
its colleague
– the large size of programs makes it difficult to keep so many implicit assumptions in mind
– any information that is not made explicit cannot be used by the compiler or other software development
tools, for instance checking for common mistakes or performing high-level code optimizations
(C) Ateji. All rights reserved. 2008-02-27 Page 8
public DietData(Document document)
{
NodeList nodes = document.getElementsByTagName("vitamins");
// locate 'vitamin' nodes and read their properties
int pos = 0;
for (int i = 0; i < nodes.getLength(); i++) {
NodeList vitamins = nodes.item(i).getChildNodes();
for (int j = 0; j < vitamins.getLength(); j++) {
Node v= vitamins.item(j);
if (v.hasAttributes()) {
NamedNodeMap attributes = v.getAttributes();
VTMN[pos] = v.getNodeName();
n_min[pos] = new Float(attributes
.getNamedItem("vmin").getNodeValue());
n_max[pos] = new Float(attributes
.getNamedItem("vmax").getNodeValue());
pos++;
}
}
}
... similar code for amounts and vitamins ...
}
Let us try to improve things. The first question to ask is "what is vitamin" ? In an object-oriented
perspective, a vitamin is defined by its observable properties. A vitamin has a name, a minimal and a
maximal amount. In Java, this is expressed with an interface:
An interface has the additional advantage of not committing to a particular implementation choice. A food
is defined similarly:
The model data now consists simply of a collection of vitamins and a collection of foods:
An Object-Oriented diet model
With the abstractions we have just defined, we can now write our diet model in a truly object-oriented way:
var double[] Buy[int j : d.foods]
in d.foods[j].minQuantity() .. d.foods[j].maxQuantity();
minimize
sum {int j : d.foods} { d.foods[j].cost() * Buy[j] };
constraints {
forall(Vitamin v : d.vitamins) {
v.minAmount() <= sum {int j : d.foods}
{d.foods[j].amount(v) * Buy[j]};
sum {int j : d.foods} {d.foods[j].amount(v) * Buy[j]}
<= v.maxAmount();
}
}
The model is still the same as the initial one, but its formulation does not use hard-wired data structures and
arrays anymore. This independence from data representation is what allows us to input the model data from
any source that can be modeled in Java.
(C) Ateji. All rights reserved. 2008-02-27 Page 9
interface Vitamin
{
String name();
float minAmount();
float maxAmount();
}
interface Food
{
String name();
double cost();
float minQuantity();
float maxQuantity();
float amount(Vitamin v);
}
Try OptimJ
The complete code of the examples shown here is included in the OptimJ samples libraries, available for
download at www.ateji.com, where you can also request a free evaluation licence of OptimJ.
Ateji helps you jump-start your first OptimJ projects by providing consulting and education services as
needed. Contact us at info@ateji.com
(C) Ateji. All rights reserved. 2008-02-27 Page 10

More Related Content

What's hot

Mi0041 java and web design
Mi0041   java and web designMi0041   java and web design
Mi0041 java and web designsmumbahelp
 
ATL tutorial - EclipseCon 2009
ATL tutorial - EclipseCon 2009 ATL tutorial - EclipseCon 2009
ATL tutorial - EclipseCon 2009 William Piers
 
Mi0041 java and web design
Mi0041  java and web designMi0041  java and web design
Mi0041 java and web designStudy Stuff
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++Nimrita Koul
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
ATL tutorial - EclipseCon 2008
ATL tutorial - EclipseCon 2008ATL tutorial - EclipseCon 2008
ATL tutorial - EclipseCon 2008William Piers
 
CIS 406 Effective Communication - tutorialrank.com
CIS 406 Effective Communication - tutorialrank.comCIS 406 Effective Communication - tutorialrank.com
CIS 406 Effective Communication - tutorialrank.comBartholomew21
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04Terry Yoast
 
Cis 406 Extraordinary Success/newtonhelp.com
Cis 406 Extraordinary Success/newtonhelp.com  Cis 406 Extraordinary Success/newtonhelp.com
Cis 406 Extraordinary Success/newtonhelp.com amaranthbeg148
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design PatternAdeel Riaz
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1guest38bf
 
Cis 406 Enthusiastic Study - snaptutorial.com
Cis 406 Enthusiastic Study - snaptutorial.comCis 406 Enthusiastic Study - snaptutorial.com
Cis 406 Enthusiastic Study - snaptutorial.comStephenson01
 
CIS 406 Life of the Mind/newtonhelp.com   
CIS 406 Life of the Mind/newtonhelp.com   CIS 406 Life of the Mind/newtonhelp.com   
CIS 406 Life of the Mind/newtonhelp.com   bellflower5
 

What's hot (18)

Mi0041 java and web design
Mi0041   java and web designMi0041   java and web design
Mi0041 java and web design
 
ATL tutorial - EclipseCon 2009
ATL tutorial - EclipseCon 2009 ATL tutorial - EclipseCon 2009
ATL tutorial - EclipseCon 2009
 
Mi0041 java and web design
Mi0041  java and web designMi0041  java and web design
Mi0041 java and web design
 
6
66
6
 
Programming and problem solving with c++, 3rd edition
Programming and problem solving with c++, 3rd editionProgramming and problem solving with c++, 3rd edition
Programming and problem solving with c++, 3rd edition
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Modular programming in c
Modular programming in cModular programming in c
Modular programming in c
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
ATL tutorial - EclipseCon 2008
ATL tutorial - EclipseCon 2008ATL tutorial - EclipseCon 2008
ATL tutorial - EclipseCon 2008
 
CIS 406 Effective Communication - tutorialrank.com
CIS 406 Effective Communication - tutorialrank.comCIS 406 Effective Communication - tutorialrank.com
CIS 406 Effective Communication - tutorialrank.com
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
Cis 406 Extraordinary Success/newtonhelp.com
Cis 406 Extraordinary Success/newtonhelp.com  Cis 406 Extraordinary Success/newtonhelp.com
Cis 406 Extraordinary Success/newtonhelp.com
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
Cis 406 Enthusiastic Study - snaptutorial.com
Cis 406 Enthusiastic Study - snaptutorial.comCis 406 Enthusiastic Study - snaptutorial.com
Cis 406 Enthusiastic Study - snaptutorial.com
 
Chap02
Chap02Chap02
Chap02
 
CIS 406 Life of the Mind/newtonhelp.com   
CIS 406 Life of the Mind/newtonhelp.com   CIS 406 Life of the Mind/newtonhelp.com   
CIS 406 Life of the Mind/newtonhelp.com   
 

Viewers also liked

Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017
Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017
Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017Bruce Pon
 
The Potential of Blockchain Technology
The Potential of Blockchain TechnologyThe Potential of Blockchain Technology
The Potential of Blockchain TechnologyPioneers.io
 
Project humix overview - For Raspberry pi community meetup
Project humix overview - For  Raspberry pi  community meetupProject humix overview - For  Raspberry pi  community meetup
Project humix overview - For Raspberry pi community meetupJeffrey Liu
 
Presentacion publicidad
Presentacion publicidadPresentacion publicidad
Presentacion publicidadcatiidiaz
 
Philadelphia Tech Summit: New Network & Wireless Technologies by Qlicket
Philadelphia Tech Summit: New Network & Wireless Technologies by QlicketPhiladelphia Tech Summit: New Network & Wireless Technologies by Qlicket
Philadelphia Tech Summit: New Network & Wireless Technologies by QlicketQlicket
 
Presentacion publicidad
Presentacion publicidadPresentacion publicidad
Presentacion publicidadcatiidiaz
 
Cosas que suceden_en_este_momento-37762
Cosas que suceden_en_este_momento-37762Cosas que suceden_en_este_momento-37762
Cosas que suceden_en_este_momento-37762arroyostr
 
Dia de la boyasencianidad
Dia de la boyasencianidadDia de la boyasencianidad
Dia de la boyasencianidadJAVIER ALDANA
 
Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02
Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02
Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02Ernesto Adaos
 

Viewers also liked (20)

Ateji PX for Java
Ateji PX for JavaAteji PX for Java
Ateji PX for Java
 
Ateji PX manual
Ateji PX manualAteji PX manual
Ateji PX manual
 
Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017
Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017
Blockchain is a Utility for the World - Dublin Tech Summit Feb 2017
 
The Potential of Blockchain Technology
The Potential of Blockchain TechnologyThe Potential of Blockchain Technology
The Potential of Blockchain Technology
 
Project humix overview - For Raspberry pi community meetup
Project humix overview - For  Raspberry pi  community meetupProject humix overview - For  Raspberry pi  community meetup
Project humix overview - For Raspberry pi community meetup
 
La represión en 1976
La represión en 1976La represión en 1976
La represión en 1976
 
Presentacion publicidad
Presentacion publicidadPresentacion publicidad
Presentacion publicidad
 
Biodiversidad
BiodiversidadBiodiversidad
Biodiversidad
 
La tardor2
La tardor2La tardor2
La tardor2
 
Philadelphia Tech Summit: New Network & Wireless Technologies by Qlicket
Philadelphia Tech Summit: New Network & Wireless Technologies by QlicketPhiladelphia Tech Summit: New Network & Wireless Technologies by Qlicket
Philadelphia Tech Summit: New Network & Wireless Technologies by Qlicket
 
Presentacion angel
Presentacion angelPresentacion angel
Presentacion angel
 
Presentacion publicidad
Presentacion publicidadPresentacion publicidad
Presentacion publicidad
 
Brecha digital
Brecha digitalBrecha digital
Brecha digital
 
Cosas que suceden_en_este_momento-37762
Cosas que suceden_en_este_momento-37762Cosas que suceden_en_este_momento-37762
Cosas que suceden_en_este_momento-37762
 
Dia de la boyasencianidad
Dia de la boyasencianidadDia de la boyasencianidad
Dia de la boyasencianidad
 
Liderazgo y grupos
Liderazgo y  gruposLiderazgo y  grupos
Liderazgo y grupos
 
Navegadores web
Navegadores webNavegadores web
Navegadores web
 
Liderazgo y grupos
Liderazgo y  gruposLiderazgo y  grupos
Liderazgo y grupos
 
Presentación yu
Presentación yuPresentación yu
Presentación yu
 
Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02
Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02
Reunion mayo 2010nsumodedrogasyotrasconductas 090310103419-phpapp02
 

Similar to Object-oriented Modeling with OptimJ

An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++TemplatesGanesh Samarthyam
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG Greg.Helton
 
IRJET- Machine Learning Techniques for Code Optimization
IRJET-  	  Machine Learning Techniques for Code OptimizationIRJET-  	  Machine Learning Techniques for Code Optimization
IRJET- Machine Learning Techniques for Code OptimizationIRJET Journal
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
PowerShell Cmdlets Test Cases Auto Generation .2
PowerShell Cmdlets Test Cases Auto Generation .2PowerShell Cmdlets Test Cases Auto Generation .2
PowerShell Cmdlets Test Cases Auto Generation .2Tao Zhou
 
CIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comCIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comKeatonJennings91
 
Start machine learning in 5 simple steps
Start machine learning in 5 simple stepsStart machine learning in 5 simple steps
Start machine learning in 5 simple stepsRenjith M P
 
Assignment 2 linear regression predicting car mpg
Assignment 2 linear regression predicting car mpg Assignment 2 linear regression predicting car mpg
Assignment 2 linear regression predicting car mpg ssuserf9c51d
 
LNCS 5050 - Bilevel Optimization and Machine Learning
LNCS 5050 - Bilevel Optimization and Machine LearningLNCS 5050 - Bilevel Optimization and Machine Learning
LNCS 5050 - Bilevel Optimization and Machine Learningbutest
 
MDE=Model Driven Everything (Spanish Eclipse Day 2009)
MDE=Model Driven Everything (Spanish Eclipse Day 2009)MDE=Model Driven Everything (Spanish Eclipse Day 2009)
MDE=Model Driven Everything (Spanish Eclipse Day 2009)Jordi Cabot
 
Final report mobile shop
Final report   mobile shopFinal report   mobile shop
Final report mobile shopViditsingh22
 
Model Comparison for Delta-Compression
Model Comparison for Delta-CompressionModel Comparison for Delta-Compression
Model Comparison for Delta-CompressionMarkus Scheidgen
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptBhuvanaR13
 
Using Mathematica for computational learning
Using Mathematica for computational learningUsing Mathematica for computational learning
Using Mathematica for computational learningMiles Ford
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6comp274
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6solutionjug4
 

Similar to Object-oriented Modeling with OptimJ (20)

C programming session7
C programming  session7C programming  session7
C programming session7
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
 
IRJET- Machine Learning Techniques for Code Optimization
IRJET-  	  Machine Learning Techniques for Code OptimizationIRJET-  	  Machine Learning Techniques for Code Optimization
IRJET- Machine Learning Techniques for Code Optimization
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
PowerShell Cmdlets Test Cases Auto Generation .2
PowerShell Cmdlets Test Cases Auto Generation .2PowerShell Cmdlets Test Cases Auto Generation .2
PowerShell Cmdlets Test Cases Auto Generation .2
 
CIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comCIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.com
 
Start machine learning in 5 simple steps
Start machine learning in 5 simple stepsStart machine learning in 5 simple steps
Start machine learning in 5 simple steps
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
 
Assignment 2 linear regression predicting car mpg
Assignment 2 linear regression predicting car mpg Assignment 2 linear regression predicting car mpg
Assignment 2 linear regression predicting car mpg
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
 
LNCS 5050 - Bilevel Optimization and Machine Learning
LNCS 5050 - Bilevel Optimization and Machine LearningLNCS 5050 - Bilevel Optimization and Machine Learning
LNCS 5050 - Bilevel Optimization and Machine Learning
 
MDE=Model Driven Everything (Spanish Eclipse Day 2009)
MDE=Model Driven Everything (Spanish Eclipse Day 2009)MDE=Model Driven Everything (Spanish Eclipse Day 2009)
MDE=Model Driven Everything (Spanish Eclipse Day 2009)
 
Final report mobile shop
Final report   mobile shopFinal report   mobile shop
Final report mobile shop
 
Model Comparison for Delta-Compression
Model Comparison for Delta-CompressionModel Comparison for Delta-Compression
Model Comparison for Delta-Compression
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.ppt
 
Using Mathematica for computational learning
Using Mathematica for computational learningUsing Mathematica for computational learning
Using Mathematica for computational learning
 
Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6Cis 355 ilab 1 of 6
Cis 355 ilab 1 of 6
 
Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6Cis 355 i lab 1 of 6
Cis 355 i lab 1 of 6
 

Recently uploaded

VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 

Recently uploaded (20)

VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 

Object-oriented Modeling with OptimJ

  • 1. Object-OrientedModeling with OptimJ OptimJ is a radically new optimization modeling language designed as a Java extension with Eclipse integration, in contrast with all existing modeling languages that are home-brewed domain-specific languages. In this whitepaper, we show how the object-oriented techniques inherited from Java enable abstraction and reuse of optimization code. OptimJ technical overview OptimJ™ is an extension of the Java™ programming language with language support for writing optimization models and powerful abstractions for bulk data processing. The language is supported by programming tools under the Eclipse™ 3.2 environment. • OptimJ is a programming language : o OptimJis an extension of Java 5 o OptimJ operates directly on Java objects and can be combined with any other Java classes o The whole Java library is directly available from OptimJ o OptimJ is interoperable with standard Java-based programming tools such as team collaboration, unit testing or interface design. • OptimJ is a modeling language : o All concepts found in modeling languages such as AIMMS™, AMPL™, GAMS™, MOSEL™, MPL™, OPL™, etc., are expressible in OptimJ. o OptimJcan target any optimization engine offering a C or Java API. • OptimJ is part of a product line of numerous domain-specific Java language extensions, a novel approach of “integration at the language level” made possible by Ateji proprietary technology. www. .com (C) Ateji. All rights reserved. 2008-02-27 Page 1
  • 2. The Diet Model Our running example will be the Diet model, a canonical example that appears in the frst pages of most textbook about optimization techniques. The diet problem consists in finding an appropriate mix of foods satisfying minimum and maximum weekly intakes of vitamins, for a minimal cost. Textbook version Here is how the Diet model is typically presented in a textbook. We have adapted the version given in "The AMPL book" to the OptimJ syntax, changing a few surface details but keeping the same overall presentation. First we have the model data: Then the decision variables, constraints and objective: This formulation is fine for a textbook and is a perfect example of the power of algebraic modeling languages: it is concise while easily readable, close to the mathematical formulation of the problem. (C) Ateji. All rights reserved. 2008-02-27 Page 2 // vitamin names String[] VTMN = { "A", "B1", "B2", "C" }; // food names String[] FOOD = { "BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR" }; // cost of each food double[] cost = { 3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49 }; // minimum and maximum intakes of each food float[] f_min = { 0, 0, 0, 0, 0, 0, 0, 0 }; float[] f_max = { 100, 100, 100, 100, 100, 100, 100, 100 }; // minimum and maximum intakes of each vitamin float[] n_min = { 700, 700, 700, 700 }; float[] n_max = { 10000, 10000, 10000, 10000 }; // amount of each vitamin brought by each food float[][] amt = { { 60, 8, 8, 40, 15, 70, 25, 60 }, { 20, 0, 10, 40, 35, 30, 50, 20 }, { 10, 20, 15, 35, 15, 15, 25, 15 }, { 15, 20, 10, 10, 15, 15, 15, 10 }}; var double[] Buy[int j : FOOD] in f_min[j] .. f_max[j]; minimize sum {int j : FOOD} { cost[j] * Buy[j] }; constraints { forall(int i : VTMN) { n_min[i] <= sum {int j : FOOD} {amt[i][j] * Buy[j]}; sum {int j : FOOD} {amt[i][j] * Buy[j]} <= n_max[i]; } }
  • 3. One thing that is missing in the initial textbook version is the code to actually solve this model and print a solution. In OptimJ, a model is just a specialized Java class, and like any other class it can have fields, methods and constructors. First we define a model containing the data and the problem given above, and provide this code inside a main() method as follows: Identify model parameters In the previous version, all model parameters are given inline as field initializations in the source code: • no difference is made between which fields are parameters and which are not • the only parameter-passing mechanism is modifying fields from the outside Indeed, this is how today's modeling languages are designed. Most are interpreted languages where parameter-passing is actually a text-replacement operation, generating a new source code with different inline parameters, after what the whole source code is interpreted again. When a mechanism is available for modifying fields from the outside (this is required for instance in column generation algorithms where one must deal with multiple instances of multiple models), this leads to spaghetti code where it quickly becomes impossible to track what has been initialized or not. This is a sure recipe for introducing very nasty bugs. (C) Ateji. All rights reserved. 2008-02-27 Page 3 model DietModel // a model a just a special kind of class solver lpsolve // the solver name is provided here { ... data ... ... variables, constraints and objective ... public static void main(String[] args) { // instanciate the model (this is like instanciating a class) DietModel m = new DietModel(); // extract the model into the solver m.extract(); // solve the model if (m.solve()) { // print the objective value System.out.println("objective value " + m.objValue()); // print the value of each variable for(int j : m.FOOD) { System.out.println( m.FOOD[j] + " " + m.value(m.Buy[j]) ); } } else { System.out.println("no solution"); } } }
  • 4. The parameter-passing mechanism of OptimJ is the same as in Java: define a constructor for the model. Now the model parameters are not given inline in the model code anymore, but passed via the constructor. As a first example, we define them inline in the main() method: (C) Ateji. All rights reserved. 2008-02-27 Page 4 // define a constructor for the diet model DietModel( String[] VTMN, String[] FOOD, double[] cost, float[] f_min, float[] f_max, float[] n_min, float[] n_max, float[][] amt) { ... } public static void main(String[] args) { // model parameters are now defined here // rather than inside the model String[] VTMN = { "A", "B1", "B2", "C" }; String[] FOOD = { "BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR" }; double[] cost = { 3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49 }; float[] f_min = { 0, 0, 0, 0, 0, 0, 0, 0 }; float[] f_max = { 100, 100, 100, 100, 100, 100, 100, 100 }; float[] n_min = { 700, 700, 700, 700 }; float[] n_max = { 10000, 10000, 10000, 10000 }; float[][] amt = { { 60, 8, 8, 40, 15, 70, 25, 60 }, { 20, 0, 10, 40, 35, 30, 50, 20 }, { 10, 20, 15, 35, 15, 15, 25, 15 }, { 15, 20, 10, 10, 15, 15, 15, 10 }}; // the parameters are now passed explicitely // to the model via the constructor call DietModel m = new DietModel(VTMN, FOOD, cost, f_min, f_max, n_min, n_max, amt); ... same as before ... }
  • 5. Abstract the model parameters The diet model constructor now takes eight different parameters. This doesn't sound right, as the eight parameters are closely related. A better formulation is to group all parameters of the diet model into a DietData class: The final keywords in this example are not mandatory but are a good programming practice. They allow the compiler to check that everything has been initialized properly, thus helping catching potential bugs early. The model constructor must be updated in order to accomodate the change: All parameters must now be prefixed, for instance FOOD is replaced with d.FOOD. An alternative approach that does not require prefixing is to cache a local copy of the parameters in fields. Again, the final keyword is here for ensuring that everything is properly initialized: (C) Ateji. All rights reserved. 2008-02-27 Page 5 public class DietData { final String[] VTMN; final String[] FOOD; final double[] cost; final float[] f_min; final float[] f_max; final float[] n_min; final float[] n_max; final float[][] amt; ... class constructors come here ... } public DietModel(DietData d) { this.d = d; } var double[] Buy[int j : d.FOOD] in d.f_min[j] .. d.f_max[j]; public DietModel(DietData d) { // cache a local copy of the parameters FOOD = d.FOOD; f_min = d.f_min; f_max = d.fmax; ... } var double[] Buy[int j : FOOD] in f_min[j] .. f_max[j];
  • 6. Access model data using any Java API Now that the model parameters have been abstracted away from the model, adding a new data source is simply a matter of writing a new constructor for the DietData class. There will be no change in the model itself. In the following sections, we show three examples where model data is read from three different classical data sources, using standard Java APIs: – JDBC for accessing SQL databases – HSSF for accessing Excel sheets – SAX for accessing XML files Of course any other available Java API , whether standard or home-grown, can be used as well. Sending solutions back to the application is done in a similar way using the same APIs, we have omitted this part for brievity. Abstracting away the diet model parameters into a DietData class marks the limit between the 'optimization' part and the 'programming' part. A properly designed model data class provides a perfect means of communication between an optimization expert and a programming expert. Import data from an SQL database If this example, the diet model data is stored in a SQL database as three tables 'Foods', 'Amounts' and 'Vitamins'. Here we create a new constructor for the DietData class using the standard JDBC API. (C) Ateji. All rights reserved. 2008-02-27 Page 6 public DietData(Connection c) throws SQLException { // Read table 'Foods' int i = 0; Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery( "SELECT * FROM Foods"); statement.close(); while (resultSet.next()) { FOOD[i] = resultSet.getString("food"); cost[i] = resultSet.getDouble("cost"); f_min[i] = resultSet.getFloat("fmin"); f_max[i] = resultSet.getFloat("fmax"); i++; } resultSet.close(); // Read table 'Amounts' ... similar code, omitted for brievity ... // Read table 'Vitamins' ... similar code, omitted for brievity ... }
  • 7. Import data from an Excel sheet In this example, the model data is stored in an Excel sheet as shown below: Having our diet model read data from this sheet is simply a matter of adding the appropriate constructor in the DietData class. Here we use the standard HSSF API from the Apache project. (C) Ateji. All rights reserved. 2008-02-27 Page 7 class DietData { // A new constructor to read data from an Excel sheet // based on the HSSF API (wb is a reference to the sheet) DietData(HSSFWorkbook wb) { VTMN = HSSFArea.make(wb, "Vitamins").toStringArray(); FOOD = HSSFArea.make(wb, "Foods").toStringArray(); cost = HSSFArea.make(wb, "Cost").toDoubleArray(); f_min = HSSFArea.make(wb, "FMin").toFloatArray(); f_max = HSSFArea.make(wb, "FMax").toFloatArray(); n_min = HSSFArea.make(wb, "VMin").toFloatArray(); n_max = HSSFArea.make(wb, "VMax").toFloatArray(); amt = HSSFArea.transpose(HSSFArea.make(wb,"Amount") .toFloatArrayArray()); } ... as before ... }
  • 8. Import data from an XML file Once again, we define an additional constructor to the DietData class, using a standard Java XML API. Abstract the logical structure A further improvement, typical of object-oriented programming, is to abstract over the logical structure of the model parameters. Our model parameters consist of eight different arrays with no apparent structure of relationship. The fact that the i-th entry in the f_min array is the minimum amount for the i-th entry in the FOOD array is stated nowhere. It is only implicit in the mind of the developer. This kind of formulation is favored by mathematicians: it is concise, and the possible semantic ambiguities are not considered relevant because of the small size of problems. From a software engineering perspective however, this is terrible: – what is implicit in the mind of a programmer may not be implicit, or differently implicit, in the mind of its colleague – the large size of programs makes it difficult to keep so many implicit assumptions in mind – any information that is not made explicit cannot be used by the compiler or other software development tools, for instance checking for common mistakes or performing high-level code optimizations (C) Ateji. All rights reserved. 2008-02-27 Page 8 public DietData(Document document) { NodeList nodes = document.getElementsByTagName("vitamins"); // locate 'vitamin' nodes and read their properties int pos = 0; for (int i = 0; i < nodes.getLength(); i++) { NodeList vitamins = nodes.item(i).getChildNodes(); for (int j = 0; j < vitamins.getLength(); j++) { Node v= vitamins.item(j); if (v.hasAttributes()) { NamedNodeMap attributes = v.getAttributes(); VTMN[pos] = v.getNodeName(); n_min[pos] = new Float(attributes .getNamedItem("vmin").getNodeValue()); n_max[pos] = new Float(attributes .getNamedItem("vmax").getNodeValue()); pos++; } } } ... similar code for amounts and vitamins ... }
  • 9. Let us try to improve things. The first question to ask is "what is vitamin" ? In an object-oriented perspective, a vitamin is defined by its observable properties. A vitamin has a name, a minimal and a maximal amount. In Java, this is expressed with an interface: An interface has the additional advantage of not committing to a particular implementation choice. A food is defined similarly: The model data now consists simply of a collection of vitamins and a collection of foods: An Object-Oriented diet model With the abstractions we have just defined, we can now write our diet model in a truly object-oriented way: var double[] Buy[int j : d.foods] in d.foods[j].minQuantity() .. d.foods[j].maxQuantity(); minimize sum {int j : d.foods} { d.foods[j].cost() * Buy[j] }; constraints { forall(Vitamin v : d.vitamins) { v.minAmount() <= sum {int j : d.foods} {d.foods[j].amount(v) * Buy[j]}; sum {int j : d.foods} {d.foods[j].amount(v) * Buy[j]} <= v.maxAmount(); } } The model is still the same as the initial one, but its formulation does not use hard-wired data structures and arrays anymore. This independence from data representation is what allows us to input the model data from any source that can be modeled in Java. (C) Ateji. All rights reserved. 2008-02-27 Page 9 interface Vitamin { String name(); float minAmount(); float maxAmount(); } interface Food { String name(); double cost(); float minQuantity(); float maxQuantity(); float amount(Vitamin v); }
  • 10. Try OptimJ The complete code of the examples shown here is included in the OptimJ samples libraries, available for download at www.ateji.com, where you can also request a free evaluation licence of OptimJ. Ateji helps you jump-start your first OptimJ projects by providing consulting and education services as needed. Contact us at info@ateji.com (C) Ateji. All rights reserved. 2008-02-27 Page 10