From 1701c16f739881097e5d660cf894ba0d3b4e1cd4 Mon Sep 17 00:00:00 2001 From: "lperron@google.com" Date: Thu, 17 Mar 2011 13:56:10 +0000 Subject: [PATCH] First java example --- Makefile | 50 ++++++++++++++- .../constraintsolver/FailException.java | 28 +++++++++ .../constraintsolver/JavaDecisionBuilder.java | 40 ++++++++++++ .../samples/RabbitsPheasants.java | 63 +++++++++++++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 com/google/ortools/constraintsolver/FailException.java create mode 100644 com/google/ortools/constraintsolver/JavaDecisionBuilder.java create mode 100644 com/google/ortools/constraintsolver/samples/RabbitsPheasants.java diff --git a/Makefile b/Makefile index bbf9fb4c0f..6f18d1b5a6 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ PROTOBUF_INC = -I$(PROTOBUF_DIR)/include # Compilation flags DEBUG=-O3 -DNDEBUG +JNIDEBUG=-O1 -DNDEBUG SYSCFLAGS=-fPIC CCC=g++ @@ -33,11 +34,17 @@ PROTOBUF_LNK = -Wl,-rpath $(PROTOBUF_DIR)/lib -L$(PROTOBUF_DIR)/lib -lprotobuf - # Detect 32 bit or 64 bit OS and define ARCH flags correctly. LBITS := $(shell getconf LONG_BIT) ifeq ($(LBITS),64) + JDK_EXT=64 ARCH=-DARCH_K8 else + JDK_EXT=32 ARCH= endif SYS_LNK=-lrt +JAVA_INC=-I/usr/local/buildtools/java/jdk-$(JDK_EXT)/include -I/usr/local/buildtools/java/jdk-$(JDK_EXT)/include/linux +JAVAC_BIN=/usr/local/buildtools/java/jdk-$(JDK_EXT)/bin/javac +JAVA_BIN=/usr/local/buildtools/java/jdk-$(JDK_EXT)/bin/java +JNILIBEXT=so endif ifeq ($(OS),Darwin) # Assume Mac Os X LD = ld -arch x86_64 -bundle -flat_namespace -undefined suppress @@ -46,17 +53,23 @@ ZLIB_LNK = -lz PROTOBUF_LNK = -L$(PROTOBUF_DIR)/lib -lprotobuf ARCH=-DARCH_K8 SYS_LNK= +JAVA_INC=-I/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bundle/Headers +JAVAC_BIN=javac +JAVA_BIN=java +JNILIBEXT=jnilib endif CFLAGS= $(SYSCFLAGS) $(DEBUG) -I. $(GFLAGS_INC) $(ARCH) \ -Wno-deprecated $(PROTOBUF_INC) +JNIFLAGS= $(SYSCFLAGS) $(JNIDEBUG) -I. $(GFLAGS_INC) $(ARCH) \ + -Wno-deprecated $(PROTOBUF_INC) $(CBC_INC) $(CLP_INC) $(GLPK_INC) LDFLAGS=$(GFLAGS_LNK) $(ZLIB_LNK) $(PROTOBUF_LNK) $(SYS_LNK) # Real targets all: @echo Please define target: - @echo " - constraint programming: cplibs, cpexe, pycp" + @echo " - constraint programming: cplibs, cpexe, pycp, javacp" @echo " - algorithms: algoritmlibs, pyalgorithms" @echo " - graph: graphlibs, pygraph" @echo " - misc: clean" @@ -461,3 +474,38 @@ objs/routing_wrap.o: constraint_solver/routing_wrap.cc _pywraprouting.so: objs/routing_wrap.o $(CPLIBS) $(BASE_LIBS) $(LD) -o _pywraprouting.so objs/routing_wrap.o $(CPLIBS) $(BASE_LIBS) $(LDFLAGS) + +# ---------- Java Support ---------- + +# javawrapcp + +javacp: com.google.ortools.constraintsolver.jar libjniconstraintsolver.$(JNILIBEXT) +constraint_solver/constraint_solver_java_wrap.cc: constraint_solver/constraint_solver.swig base/base.swig util/data.swig constraint_solver/constraint_solver.h + $(SWIG_BINARY) -c++ -java -o constraint_solver/constraint_solver_java_wrap.cc -package com.google.ortools.constraintsolver -outdir com/google/ortools/constraintsolver constraint_solver/constraint_solver.swig + sed -i -e 's/Tlong/T_long/g' com/google/ortools/constraintsolver/Solver.java + +objs/constraint_solver_java_wrap.o: constraint_solver/constraint_solver_java_wrap.cc + $(CCC) $(JNIFLAGS) $(JAVA_INC) -c constraint_solver/constraint_solver_java_wrap.cc -o objs/constraint_solver_java_wrap.o + +com.google.ortools.constraintsolver.jar: constraint_solver/constraint_solver_java_wrap.cc + $(JAVAC_BIN) com/google/ortools/constraintsolver/*.java + jar cf com.google.ortools.constraintsolver.jar com/google/ortools/constraintsolver/*.class + +libjniconstraintsolver.$(JNILIBEXT): objs/constraint_solver_java_wrap.o $(CPLIBS) $(BASE_LIBS) + $(LD) -o libjniconstraintsolver.$(JNILIBEXT) objs/constraint_solver_java_wrap.o $(CPLIBS) $(BASE_LIBS) $(LDFLAGS) + +# Java CP Examples + +compile_RabbitsPheasants: com/google/ortools/constraintsolver/samples/RabbitsPheasants.class + +com/google/ortools/constraintsolver/samples/RabbitsPheasants.class: javacp com/google/ortools/constraintsolver/samples/RabbitsPheasants.java + $(JAVAC_BIN) -cp com.google.ortools.constraintsolver.jar com/google/ortools/constraintsolver/samples/RabbitsPheasants.java + +run_RabbitsPheasants: compile_RabbitsPheasants + $(JAVA_BIN) -Djava.library.path=`pwd` -cp .:com.google.ortools.constraintsolver.jar com.google.ortools.constraintsolver.samples.RabbitsPheasants + + + + + + diff --git a/com/google/ortools/constraintsolver/FailException.java b/com/google/ortools/constraintsolver/FailException.java new file mode 100644 index 0000000000..6d4c06df7e --- /dev/null +++ b/com/google/ortools/constraintsolver/FailException.java @@ -0,0 +1,28 @@ +// Copyright 2010 Google +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.ortools.constraintsolver; + +/** + * This exceptions signal that a failure has been raised in the C++ world. + * + */ +public class FailException extends Exception { + public FailException() { + super(); + } + + public FailException(String message) { + super(message); + } +} diff --git a/com/google/ortools/constraintsolver/JavaDecisionBuilder.java b/com/google/ortools/constraintsolver/JavaDecisionBuilder.java new file mode 100644 index 0000000000..ac4136a24d --- /dev/null +++ b/com/google/ortools/constraintsolver/JavaDecisionBuilder.java @@ -0,0 +1,40 @@ +// Copyright 2010 Google +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.ortools.constraintsolver; + +/** + * This class acts as a intermediate step between a c++ decision builder + * and a java one. Its main purpose is to catch the java exception launched + * when a failure occurs during the Next() call, and to return silently + * a FailDecision that will propagate the failure back to the C++ code. + * + */ +public class JavaDecisionBuilder extends DecisionBuilder { + /** + * This methods wraps the calls to next() and catches fail exceptions. + */ + public final Decision nextWrap(Solver solver) { + try { + return next(solver); + } catch (FailException e) { + return solver.makeFailDecision(); + } + } + /** + * This is the new method to subclass when defining a java decision builder. + */ + public Decision next(Solver solver) throws FailException { + return null; + } +} diff --git a/com/google/ortools/constraintsolver/samples/RabbitsPheasants.java b/com/google/ortools/constraintsolver/samples/RabbitsPheasants.java new file mode 100644 index 0000000000..47cc2e4d95 --- /dev/null +++ b/com/google/ortools/constraintsolver/samples/RabbitsPheasants.java @@ -0,0 +1,63 @@ +// Copyright 2010 Google +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.ortools.constraintsolver.samples; + +import com.google.ortools.constraintsolver.DecisionBuilder; +import com.google.ortools.constraintsolver.IntVar; +import com.google.ortools.constraintsolver.Solver; + +import java.util.logging.Logger; + +/** + * Sample showing how to model using the constraint programming solver. + * + */ + +public class RabbitsPheasants { + private static Logger logger = + Logger.getLogger(RabbitsPheasants.class.getName()); + + static { + System.loadLibrary("jniconstraintsolver"); + } + + + /** + * Solves the rabbits + pheasants problem. We are seing 20 heads + * and 56 legs. How many rabbits and how many pheasants are we thus + * seeing? + */ + private static void solve() { + Solver solver = new Solver("RabbitsPheasants"); + IntVar rabbits = solver.makeIntVar(0, 100, "rabbits"); + IntVar pheasants = solver.makeIntVar(0, 100, "pheasants"); + solver.addConstraint(solver.makeEquality(solver.makeSum(rabbits, pheasants), + 20)); + solver.addConstraint(solver.makeEquality(solver.makeSum( + solver.makeProd(rabbits, 4), + solver.makeProd(pheasants, 2)), 56)); + DecisionBuilder db = solver.makePhase(rabbits, pheasants, + solver.CHOOSE_FIRST_UNBOUND, + solver.ASSIGN_MIN_VALUE); + solver.newSearch(db); + solver.nextSolution(); + logger.info(rabbits.toString()); + logger.info(pheasants.toString()); + solver.endSearch(); + } + + public static void main(String[] args) throws Exception { + RabbitsPheasants.solve(); + } +}