SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Search.java
1/*
2 * Class: Search
3 * Description:
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2018 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Maxime Godin and Pierre Marion
9 * @since August 2018
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.latnetbuilder;
26
27import java.io.BufferedReader;
28import java.io.IOException;
29import java.io.InputStreamReader;
30import java.io.File;
31import java.io.FileReader;
32
33import java.util.List;
34import java.util.ArrayList;
35
36// import com.google.gson.Gson;
37
38import umontreal.ssj.hups.PointSet;
39
47abstract public class Search {
48
49 String pathToOutputFolder;
50
51 String pathToLatNetBuilder = "latnetbuilder"; // path to the latnetbuilder executable
52
53 String sizeParameter;
54 String dimension;
55 boolean multilevel;
56 String combiner;
57 String explorationMethod;
58 String figure;
59 String normType;
60 ArrayList<String> weights;
61 ArrayList<String> filters;
62
63 boolean successful;
64 double merit;
65 double time;
66
70 protected Search() {
71 this.pathToOutputFolder = "latnetbuilder_results";
72 this.multilevel = false;
73 this.weights = new ArrayList<String>();
74 this.filters = new ArrayList<String>();
75 this.successful = false;
76 this.merit = 0;
77 this.time = 0;
78 }
79
80 @Override
84 public String toString() {
85 return "Point Set Type: " + pointSetType() + "\n" + "Construction method: " + construction() + "\n"
86 + "Size parameter: " + sizeParameter + "\n" + "Multilevel: " + multilevel + "\n" + "Dimension: " + dimension
87 + "\n" + "Interlacing: " + interlacing() + "\n" + "Exploration method: " + explorationMethod + "\n"
88 + "Figure of merit: " + figure + "\n" + "Norm-type: " + normType + "\n" + "Combiner: " + combiner + "\n"
89 + "Filters: " + filters + "\n" + "Weights: " + weights + "\n" + "Output folder: " + pathToOutputFolder;
90
91 }
92
96 abstract public String pointSetType();
97
101 abstract public String interlacing();
102
103 abstract public String construction();
104
108 private String constructCommandLine() {
109 StringBuffer sb = new StringBuffer(pathToLatNetBuilder + " -v 0");
110 sb.append(" -t " + pointSetType());
111 sb.append(" -c " + construction());
112 sb.append(" -M " + multilevel);
113 sb.append(" -s " + sizeParameter);
114 sb.append(" -f " + figure);
115 sb.append(" -q " + normType);
116 sb.append(" -e " + explorationMethod);
117 sb.append(" -d " + dimension);
118 sb.append(" -i " + interlacing());
119 sb.append(" --output-folder " + pathToOutputFolder);
120 sb.append(" -w");
121 for (String w : weights) {
122 sb.append(" " + w);
123 }
124 if (filters.size() > 0)
125 sb.append(" -F ");
126 for (String f : filters) {
127 sb.append(" " + f);
128 }
129 if (multilevel) {
130 sb.append(" -C " + combiner);
131 }
132 return sb.toString();
133 }
134
139 protected ArrayList<String> executeCommandLine() {
140 String cmd = constructCommandLine();
141 BufferedReader br = null;
142 try {
143 Process p = Runtime.getRuntime().exec(cmd);
144 BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
145
146 try {
147 p.waitFor();
148 String res = "";
149 if (p.exitValue() != 0) {
150 while (stderr.ready())
151 res = res + "\n" + stderr.readLine();
152 throw new RuntimeException(
153 "LatNet Builder exited with status " + p.exitValue() + "\nCOMMAND LINE: " + cmd + res);
154 }
155 } catch (InterruptedException e) {
156 throw new RuntimeException("LatNet Builder interrupted");
157 }
158 File f = new File(pathToOutputFolder, "outputMachine.txt");
159 br = new BufferedReader(new FileReader(f));
160
161 ArrayList<String> res = new ArrayList<String>();
162 String line;
163 while ((line = br.readLine()) != null) {
164 res.add(line);
165 }
166 return res;
167 } catch (IOException e) {
168 throw new RuntimeException("Error in the communication with LatNet Builder");
169 } finally {
170 try {
171 if (br != null) {
172 br.close();
173 }
174 } catch (IOException e) {
175 throw new RuntimeException("Error in the communication with LatNet Builder");
176 }
177 }
178 }
179
183 abstract public PointSet search() throws RuntimeException;
184
188 public double merit() {
189 return this.merit;
190 }
191
195 public double time() {
196 return this.time;
197 }
198
202 public boolean successful() {
203 return this.successful;
204 }
205
206 public String getExplorationMethod() {
207 return this.explorationMethod;
208 }
209
215 public void setPathToLatNetBuilder(String path) {
216 pathToLatNetBuilder = path;
217 }
218
224 public void setDimension(String dimension) {
225 this.dimension = dimension;
226 }
227
234 public void setSizeParameter(String sizeParameter) {
235 this.sizeParameter = sizeParameter;
236 }
237
243 public void setMultilevel(boolean multilevel) {
244 this.multilevel = multilevel;
245 }
246
252 public void setCombiner(String combiner) {
253 this.combiner = combiner;
254 }
255
261 public void setExplorationMethod(String explorationMethod) {
262 this.explorationMethod = explorationMethod;
263 }
264
270 public void setFigureOfMerit(String figure) {
271 this.figure = figure;
272 }
273
279 public void setNormType(String normType) {
280 this.normType = normType;
281 }
282
288 public void setWeights(List<String> weights) {
289 this.weights = new ArrayList<String>(weights);
290 }
291
297 public void addWeight(String weight) {
298 this.weights.add(weight);
299 }
300
306 public void setFilters(List<String> filters) {
307 this.filters = new ArrayList<String>(filters);
308 }
309
313 public void setPathToOutputFolder(String path) {
314 this.pathToOutputFolder = path;
315 }
316
317 /*
318 * public static Search fromJSON(String json) {
319 *
320 * Gson gson = new Gson(); if
321 * (json.contains("\"construction\":\"polynomial\"")){ return
322 * gson.fromJson(json, PolynomialLatticeSearch.class); } else if
323 * ((json.contains("\"construction\":"))){ return gson.fromJson(json,
324 * DigitalNetSearch.class); } else { return gson.fromJson(json,
325 * OrdinaryLatticeSearch.class); } }
326 */
327}
This abstract class represents a general point set.
Definition PointSet.java:99
void setPathToOutputFolder(String path)
Sets the path to the output folder.
Definition Search.java:313
void setWeights(List< String > weights)
Sets the weights to the figure of merit of the search.
Definition Search.java:288
void setDimension(String dimension)
Sets the dimension of the searched point set.
Definition Search.java:224
void setSizeParameter(String sizeParameter)
Sets the size parameter of the point set.
Definition Search.java:234
void setNormType(String normType)
Sets the norm-type for the figure of merit of the search.
Definition Search.java:279
String toString()
Formats the search parameters for printing.
Definition Search.java:84
double time()
Returns the elapsed CPU time taken for the search.
Definition Search.java:195
void setFigureOfMerit(String figure)
Sets the figure of merit of the search.
Definition Search.java:270
abstract PointSet search()
Executes the search and returns the corresponding point set.
void setFilters(List< String > filters)
Sets the filters of the search.
Definition Search.java:306
boolean successful()
Returns a boolean indicating if the search was successful.
Definition Search.java:202
abstract String interlacing()
Returns the interlacing factor of the search.
void setExplorationMethod(String explorationMethod)
Sets the exploration method of the search.
Definition Search.java:261
abstract String pointSetType()
Returns the type of the searched point set.
void setMultilevel(boolean multilevel)
Sets the search to the multilevel mode.
Definition Search.java:243
void setCombiner(String combiner)
Sets the combiner for the merit in the multilevel mode case.
Definition Search.java:252
ArrayList< String > executeCommandLine()
Executes the command-line and reads the content of the outputMachine.txt file.
Definition Search.java:139
void addWeight(String weight)
Add a weight to the figure of merit of the search.
Definition Search.java:297
void setPathToLatNetBuilder(String path)
Sets the path to the latnetbuilder executable.
Definition Search.java:215