[OS] Java: how to start?

Olivier Cailloux olivier.cailloux at ecp.fr
Fri Jan 29 08:37:07 EST 2010


Hello,

Jun Ma a écrit :
> Olivier,
> Regarding the Java parts of your questions, I am not sure how you 
> downloaded the code and software.
> Either you may have built it yourself from branches/OSjava or 
> downloaded the a ready binary build "os-server-2.0.0.zip".
>
> Either way, if you decide to use the Java as part of your software 
> library, you should just look at only the OSCommon package,  i.e. 
> org.optimizationservices.oscommon.*, which has all the similar classes 
> and interfaces to the core C++ version to read, write OSxL files.
>
> Your error message indicates to me that you may have downloaded and 
> used os-server-2.0.0, which has other packages including services to 
> run readily made/distributed solver executables.
>
> Now, it does look like on COIN-OR we don't distribute an OSCommon.jar 
> binary that includes all org.optimizationservices.oscommon classes.
> We will discuss that and may put the build on soon.
> But for now you can may either just include all the OSCommon classes 
> on your classpath or zip up all the classes your self and call it 
> whatever jar you want to name (preferably OSCommon.jar) and put the 
> jar on the classpath.
>
> I hope the general comments gives some insights. Otherwise please be 
> more specific on your issues.
>
> Jun
1) Java code to use.
I searched for java binaries (i.e., .jar libraries) but I have been 
unable to find them. Indeed either distributing OSCommon.jar in the 
binaries download as you suggest, or saying something about how to 
access COIN-OR implementations from Java in the user's manual would be 
useful for your Java users, I feel.

I used the java source at 
https://projects.coin-or.org/svn/OS/branches/OSjava/, in the folders 
OSCommon and OSSolver. That was only when trying to call the solver from 
the Java code. I did not use os-server-2.0.0, my example with 
OSSolverService was using the binary linux executable from CoinAll 1.3.1.

2) About the input file not accepted by OSSolverService binary.
Kipp Martin tells me (I post my reply to the list so that Google might 
reference it) that the problems comes from that version (1.3.1, or 
2.0.0, I'm a bit lost with the versioning scheme) not recognizing the 
(XML correct) <instanceHeader/> semantic. Changing it to 
<instanceHeader></instanceHeader> solves the problem. It should have 
been obvious to me as the error message pointed to line number 7.

3) Building the OSiL file from Java.
Using the Java source from OSCommon, I have been able to build the OSiL 
input file without problem (except for the <instanceHeader/> part). Here 
is a working sample for interested users. (I added a setInstanceName 
call so that the instanceHeader tag does not trigger the bug any more.)
----
    final OSInstance inst = new OSInstance();
    inst.setInstanceName("firstProfile");
    inst.setVariableNumber(4);
    inst.addVariable(0, "gjb2", 0, Double.POSITIVE_INFINITY, 'C');
    inst.addVariable(1, "gjb3", 0, Double.POSITIVE_INFINITY, 'C');
    inst.addVariable(2, "s2", 0, Double.POSITIVE_INFINITY, 'C');
    inst.addVariable(3, "s3", 0, Double.POSITIVE_INFINITY, 'C');

    inst.setObjectiveNumber(1);
    final SparseVector v = new SparseVector();
    v.indexes = new int[] { 2, 3 };
    v.values = new double[] { 1, 1 };
    inst.addObjective(-1, "sumofslack", "min", 0d, 1d, v);
    inst.setConstraintNumber(2);
    final boolean conSet = inst.addConstraint(0, "lowCat", 3d, 3d, 0d);
    System.out.println(conSet);
    final boolean con1Set = inst.addConstraint(1, "highCat", 3.1d, 3.1d, 
0d);
    System.out.println(con1Set);
    final boolean coeffSet = inst.setLinearConstraintCoefficients(4, 
false, new double[] { 1, 1, 1, 1 }, 0, 3,
        new int[] { 0, 2, 1, 3 }, 0, 3, new int[] { 0, 2, 4 }, 0, 2);
    System.out.println(coeffSet);
    final String osil = inst.writeOSiL();
    IOUtil.writeStringToFile(osil, "osilFirstProfile.xml");
----

4) Calling a solver from Java.
Kipp Martin tells me
"The OSSolverService is C++ so at the minimum you will have to call a 
C++ executable from Java. We routinely do this, (i.e. call the C++ 
OSSolverService from Java) and it has not caused us problems. As Jun Ma 
has pointed out, you can use our OSCommon package for building OSiL 
files and then sending the OSiL file to the C++ executables. However, 
you might just want to use your current Java code to access the data and 
directly build the OSiL file to send to the OSSolverService."

I thought I would find in the OSSolver folder the way to call the 
solvers from Java. But as mentioned previously, I can't make it call a 
local binary, it wants to call a (possibly local, i.e. running on 
127.0.0.1) web service. Should I write myself the code to build a 
command line calling OSSolverService, or can I reuse the code you 
mention is working for you? Also, Jun Ma, you suggest to use only the 
OSCommon source folder, does that imply that other parts are considered 
not ready / unstable / unusable?
Olivier

>
> --------------------------------------------------
> From: "Olivier Cailloux" <olivier.cailloux at ecp.fr>
> Sent: Thursday, January 28, 2010 10:21 AM
> To: <os at list.coin-or.org>
> Cc: "Jun Ma @ NWU" <maj at northwestern.edu>
> Subject: Re: [OS] Java: how to start?
>
>> Horand Gassmann a écrit :
>>> Quoting Olivier Cailloux <olivier.cailloux at ecp.fr>:
>>>
>>>> Oops, missing the xml file, sorry.
>>>>
>>>> Thank you for your fast answer.
>>>>
>>>> Kipp Martin a écrit :
>>>>> Hi Olivier:
>>>>>
>>>>> First, thank you very much for your interest in OS.
>>>>>
>>>
>>>> I made several more tries but am still unable to find what is my
>>>> mistake. I am trying to input the following model.
>>>> /* Decision variables */
>>>> var gjb2 >=0;
>>>> var gjb3 >=0;
>>>> var s2 >= 0;
>>>> var s3 >= 0;
>>>> /* Objective function */
>>>> minimize sumofslack: s2 + s3;
>>>> /* Constraints */
>>>> s.t. lowcat : gjb2 + s2 = 3;
>>>> s.t. highcat : gjb3 - s3 - 0.1 = 3;
>>>> end;
>>>>
>>>> Attached is the xml osil file I obtained with one of my tries. I don't
>>>> see why it is incorrect. (It uses column layout instead of row but I
>>>> also tried with row layout with no success.)
>>>
>>> I am not sure what you mean when you say "it is incorrect". The file 
>>> is valid
>>> under the OSiL schema and, from what I can tell, transcribes the 
>>> given problem.
>>> What sort of errors do you experience? Does the parser report an 
>>> error? Is the
>>> solution nonsensical?
>> Indeed I should have included this. Running OSSolverService with this 
>> input file produces the following error (while running it with the 
>> parincLinear example file produces no error).
>>
>> ~$ ...OSSolverService -osil osil1.xml
>> <?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet 
>> type="text/xsl" href="../stylesheets/OSrL.xslt"?>
>> <osrl xmlns="os.optimizationservices.org" 
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
>> xsi:schemaLocation="os.optimizationservices.org 
>> http://www.optimizationservices.org/schemas/2.0/OSrL.xsd" >
>> <general>
>> <generalStatus type="error"/>
>> <message>PARSER ERROR:  Input is either not valid or well formed: 
>> improperly formed <instanceData> element
>> Here are 20 characters currently being pointed to in the input 
>> string: ?xml version="1.0" e
>> See line number: 7
>> </message>
>> </general>
>> <job>
>> </job>
>> </osrl>
>>
>> Olivier
>>
>>



More information about the OS mailing list