News and EventsMulti-Core ProgrammingWith 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 CornerUnit Testing with OptimJUnit 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:
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 TestsAn 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: 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:
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
If the Junit bar is visible, a green bar indicates a successfull test. A red bar indicates that an error occured:
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 JUnitOptimJ ForumsShare your comments and feelings about this newsletter in the forums. |
"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 us for details.






