This project aims to make java programs easier to call julia functions.
Java, along with its bro languages such as Scala, really sucks at numerical computing. Projects like Spark, Breeze, ND4j, Common-math and Optimizer made great efforts to facilitate numerical computing in JVM ecosystem. However, the whole Java community actually lags extremely behind Python and Julia with regard to numerical computing and machine learning. Developers and scientists using Java and Scala have been struggling to push Java into such arena, but it ultimately turns out that Java lacks floating point accuracy, performance, as well as enthusiasm, towards mathematical computation. This project tries to build a bridge from Java to Julia, which is perfectly suited to create fast and accurate numerical applications.
/**
* Call function named as 'greetings' on Julia server
*/
@Test
public void testRemoteGreeting() {
String ip = "127.0.0.1"; //Ip address of Ju4ja server
int port = 6996;//port of Ju4ja server
//Build a Ju4ja client to call function in Julia
Ju4jaClient client = new Ju4jaClient(ip, port);
//encapsulate arguments for remote function
Object[] as = {"Zhang fei"};
//invocation to the remote function
JavaCallResult result = client.invokeFunction("greetings", "Main", as);
if (result != null) {
//print out the result
System.out.println(result.getResultStr());
System.out.println(result.getStatus());
}
}
#set the path where the modules are located push!(LOAD_PATH, "./")#A examle function ready to be called from java using Ju4ja function greetings(name::String) str="Hi, $name" println(str) return str end
#g=greetings("test") #println(g)
#using Ju4ja and the example solver for linear programming using Ju4ja using RereDmlLpSolver
#start the Ju4ja server as a new coroutine @async begin startServer() end
/**
* call Julia server to solve a linear programming problem
* The function is named as 'solveDmlLp'
*/
@Test
public void testRemoteLinearProgramming() {
/**
* min c'x
* s.t. Ax >= b, x>=0
*/
double[] c = {-3, -1, -2};
double[][] A = {
{-1.0, -1.0, -3.0},
{-2.0, -2.0, -5.0},
{-4.0, -1.0, -2.0}
};
double[] b = {-30, -24, -36};
String ip = "127.0.0.1";
int port = 6996;
Ju4jaClient client = new Ju4jaClient(ip, port);
Object[] as = {c, A, b};
JavaCallResult result = client.invokeFunction("solveDmlLp", "RereDmlLpSolver", as);
//System.out.println(result);
if (result != null) {
System.out.println(result.getResultStr());
//System.out.println(result.getStatus());
}
}
Basic usage 2.2 - Code snippet in Julia defines "solveDmlLp" function to solve linear programming problem
#This module works as a example of solving linear programming problems for Ju4ja module RereDmlLpSolver using JuMP using GLPK export solveDmlLp
#solve linear programming function solveDmlLp(c::Vector,A::Matrix,b::Vector,ifReg::Bool=true) model = JuMP.Model(with_optimizer(GLPK.Optimizer)) #println(length(c)) @variable(model, x[i=1:length(c)]) @constraint(model, con, A * x .>= b) @constraint(model, con0, x .>= 0) @objective(model, Min, c'*x ) status = optimize!(model) result=JuMP.value.(x) obj=JuMP.objective_value(model) println("x = ", result) println("Objective value: ", obj) return result end end
Ju4Ja builds socket communications between Java (client) and Julia (Server), automatically transforms the numeric (float, vector, matrix) arguments of Java to Julia format, invokes Julia functions, and finally returns the results to Java caller.