📄 ilpool.java
字号:
/* * * Copyright 2003,2004 The Watermill Team * * This file is part of Watermill. * * Watermill is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Watermill is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Watermill; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */package Watermill.kernel;import java.io.*;import java.util.*;import com.cflex.util.lpSolve.*;public class ILPool extends Pool implements Serializable{public static Identifiers identifiers;public static DependencyMatrix depMatrix;public static LpModel lpIn;public static int lc,seed,gd;public static Document doc;public static String request;public ILPool(String ipn,Document id,WSchema iws)throws Exception{ super(ipn,id,iws); doc = id; identifiers=id.getModifiableValuesIdentifiers(wschema.getLocalConstraints()); Msg.debug("Identifiers:"+identifiers.toString()); depMatrix=id.getDependency(wschema.getGlobalConstraints()); Msg.debug("DepMatrix:"+depMatrix.toString()); reductMatrix(); lpIn = lpModele(); lpIn.printLp(); }public static void reductMatrix ()throws Exception{ // choose a random subset of identifiers such that their values is larger than local distortion Random r=new Random(seed); DependencyMatrix dep=(DependencyMatrix)depMatrix.clone(); for (int j=0;j<dep.size();j++){ Identifiers id = (Identifiers)dep.get(j); for (int i=0; i<id.size(); i++){ boolean b=r.nextBoolean(); // XIdentifier ident = (XIdentifier)id.get(i) ; Identifier ident = (Identifier)id.get(i); ident.maxDistortion=lc; Value value = doc.getValue(ident); int x = value.get(); if (!b | x <=lc) { id.remove(ident); } } } depMatrix=dep;}public static LpModel lpModele ()throws Exception{ double globale = 0.0; int Ldistinf = 0; int indice = 0; int Ldistsup =0;int num = 0; try{ //reductMatrix(); for (int j=0;j<depMatrix.size();j++){ Identifiers id = (Identifiers)depMatrix.get(j); lpIn = new LpModel(0, id.size()); lpIn.debug = LpModel.FALSE; lpIn.verbose = LpModel.FALSE; lpIn.trace = LpModel.FALSE; double v1[] = new double[id.size()+1]; // objective function double v2[] = new double[id.size()+1]; // model of the global constraint for (int i=0; i<id.size(); i++){ double v[] = new double[id.size()+1]; // model of the local constraint Ldistsup = 2*lc ; double rhs; indice = i+1; v2[i+1] = 1 ; v1[i+1] = -1; lpIn.setObjFn(v1); lpIn.setMaximum(); num++; v[i+1]=0; v[indice] = 1; rhs = Ldistinf; lpIn.addConstraint(v, LpModel.GE, rhs); rhs = Ldistsup; lpIn.addConstraint(v, LpModel.LE, rhs); lpIn.setInt(indice, LpModel.TRUE); } globale = num*lc; lpIn.addConstraint(v2, LpModel.GE, globale); lpIn.addConstraint(v2, LpModel.LE, globale); }} catch (Exception ex){ ex.printStackTrace(); } return lpIn; } public Watermark watermark(Document doc,int is) { Watermark res=new Watermark(); seed = is; try { lpIn = lpModele(); // this command generates an exception Msg.debug("Watermark"); for (int i=0;i<depMatrix.size();i++){ Identifiers ids=(Identifiers)depMatrix.get(i); Msg.debug("identifiers : "+ids.toString()); LpSolver lpsolve = new LpSolver(lpIn); int q = lpsolve.solve(); int[]result = new int[ids.size()]; for(int j=0; j<ids.size();j++){ result[j] = (int)lpIn.getResult(j+1)-lc ; } for(int j=0;j<ids.size();j++){ Identifier id=(Identifier)ids.get(j); Msg.debug("watermarking in identifier "+id.toString()); int value = result[j]; if(value!= 0){ Value v1 = new Value(value); doc.addValue(id,v1); res.put(id,new Value(value));} } } } catch (Exception e) { Msg.debug("An exception has occured with resolving linear constraints"); } return res; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -