Ateji Newsletter - July 2009
Welcome to Ateji's newsletter.

In this issue, you will learn how to write unit tests for your OptimJ™ models, using solvers such as Gurobi™, ILOG CPLEX™, Mosek™, lp_solve and GLPK.

Optimization models deserve the same state-of-the-art tooling support as other software development projects. Writing unit tests is at the basis of any software quality process, leading to more confidence in the code and less debugging. With OptimJ, unit testing is now available for optimization experts. MORE>

-- The Ateji Team

PRODUCT NEWS

Multi-Core Programming

corail
Building up on its innovative language technology, Ateji has designed a Java™ language extension for multi-core programming, making parallel programs simple, efficient and provably correct. MORE>

TECHNICAL CORNER

Unit Testing with OptimJ

JUnit is the standard unit testing framework for Java. This article will show how to use JUnit to enhance the quality and reliability of your optimization models.
MORE>

Learn more

Focus on...

 

News and Events

Multi-Core Programming

With multi-core processors becoming ubiquitous, more and more programmers face the challenge of writing parallel code. But parallel programs are famous for being difficult to write, and impossible to read, test and debug. If you've ever tried, you know what I mean.

Most of the difficulties stem from the fact that we're trying to express parallelism using sequential programming languages, something they've never been designed for.

Since this is a language problem, Ateji provides a language-based solution. We have designed a Java™ language extension for parallel programming, where parallelism is now part of the language. This is an application of the same innovative language technology that made OptimJ possible.

Parallel programs become simple and intuitive, efficient, and guaranteed correct. The early access program gives you a chance to be among the first to master and apply this strategic technology. Contact us if you'd like to join the program, it's free.


Technical Corner

Unit Testing with OptimJ

Unit testing is a commonplace practice in software development, as it plays an important role in improving the quality of software and reducing development time. A unit test is a test for a small functional unit, typically a class or a method, that is run on a regular basis.

Unit tests serve two major purposes:

  • Relate the software and its specification, or provide a specification when there is none.
    They help convince the coders and all project stakeholders that the software is doing what it is supposed to do.
  • Ensure non-regression. It is quite common to correct a bug by introducing new bugs. Unit tests help catch those errors early.

What is good for software development is also good for optimization modeling. It's just too easy to mistype a coefficient or accidentaly delete a constraint. Unit tests checking that solutions are meaningful help catch such errors. In fact, unit tests are best written first, before even writing the model.

Unit tests may also be used to check various functional aspects of a system, such as memory usage or performance. A good practice of project management is to have unit tests describing all system requirements.

Automating Unit Tests

An important feature of unit tests is that they must be automated and run in a systematic manner. Running the tests can be part of the build process.

The standard JUnit tool does precisely this for Java code, and thus also for OptimJ code.

Here is a sample OptimJ model for which we will write JUnit tests:

public model M solver lpsolve {
var int x, y in 0 .. 10;
constraints {
x >= 5;
y == x + 1;
}
minimize x + y;
}

The expected solution is x = 5 and y = 6. Our first test will check that this is actually the case.

First, we create a test case:

new junit test

JUnit generates the following Java code:

import junit.framework.TestCase;

public class TestM extends TestCase {

}

A JUnit test case is a class extending TestCase. By convention, its name starts with 'Test', followed by the name of the class or model to test, hence TestM in our example.

Inside the test case, each method whose name starts with 'test' is considered a unit test by JUnit. Here we will solve the OptimJ model in the constructor of the test case, so that we can later define many differents tests againt this model.

public class TestM extends TestCase {

private final M model;

// Solve the model M once for the test campaign
public TestM()
{
model = new M();
com.ateji.optimj.lpsolvelib.LibraryLoader.load();
model.extract();
model.solve();
}

// This tests if we get the expected solution
public void testX()
{
int x = model.value(model.x);
int y = model.value(model.y);
assertEquals("check that x = 5", 5, x);
assertEquals("check that y = 6", 6, y);
}

// Additional tests about M can be added here
}

Running the test is similar to launching a Java application. Right-click on the test case and select Run As -> JUnit test:

run a junit test

If the Junit bar is visible, a green bar indicates a successfull test. A red bar indicates that an error occured:

a successful test a failed test

You are now able to write your own tests. Below are a few examples of things that you may wish to test about model M:

// test the objective value
public void testObjectiveValue() {
double expected = 11;
double result = model.objValue();
assertTrue("Objective value", result <= expected);
}

// remember to use an epsilon when
// comparing floating-point values for equality

public void testRC() {
double epsilon = 0.01;
double expected = 0;
double result = model.reducedCost(model.y);
assertEquals("Reduced cost for y", expected, result, epsilon);
}

// test the matrix shape using an lp_solve API call
public void testNonZero() {
// getNonzeros() returns the number of non-zero elements in the matrix.
int result = model.solver().getNonzeros();
int expected = 3;
assertEquals("Number of non-zero elements", expected,
result);
}

Further reading about JUnit



OptimJ Forums

Share your comments and feelings about this newsletter in the forums.

 

SEO by AceSEF

Newsletter

To request a free subscription to our bi-monthly newsletter, enter your e-mail address below:
To prevent spam, please answer this little quiz:
 
 

whitepaper


"Using OptimJ enabled a rapid development and integration of optimization models in Java-based applications",
says Médéric Suon, Industrial Engineer at PSA Peugeot Citroën.

"Even better, OptimJ made it easy to use results from different solvers and combine exact methods with metaheuristics coded in Java, for solving complex industrial problems."



Academic licenses
OptimJ academic licenses are free for teaching at degree-granting institutions. Contact usThis e-mail address is being protected from spambots. You need JavaScript enabled to view it for details.