public CompressedFeatureVector compress(Counter<K> c) { List<Integer> keys = new ArrayList<>(c.size()); List<Double> values = new ArrayList<>(c.size()); for (Map.Entry<K, Double> e : c.entrySet()) { K key = e.getKey(); Integer id = index.get(key); if (id == null) { id = index.size(); inverse.put(id, key); index.put(key, id); } keys.add(id); values.add(e.getValue()); } return new CompressedFeatureVector(keys, values); }
public HashMap<String, Double> run() { HashBiMap<String, Integer> variable2id = HashBiMap.create(); for (Entry<String, Double> e : objective.entrySet()) { if (!variable2id.containsKey(e.getKey())) { variable2id.put(e.getKey(), variable2id.size()); } } for (Counter<String> counter : constraints) { for (Entry<String, Double> e : counter.entrySet()) { if (!variable2id.containsKey(e.getKey())) { variable2id.put(e.getKey(), variable2id.size()); } } } int NUMVAR = variable2id.size(); int NUMCON = constraints.size(); int NUMANZ = 0; // set up objective function double[] c = new double[NUMVAR]; for (Entry<String, Double> e : objective.entrySet()) { int vid = variable2id.get(e.getKey()); c[vid] = e.getValue(); } // set up constraints mosek.Env.boundkey[] bkc = new mosek.Env.boundkey[NUMCON]; double[] blc = new double[NUMCON]; double[] buc = new double[NUMCON]; int asub[][] = new int[NUMCON][]; double aval[][] = new double[NUMCON][]; for (int i = 0; i < NUMCON; i++) { bkc[i] = constraints_bound_type_list.get(i); blc[i] = constraints_lowerbound_list.get(i); buc[i] = constraints_upperbound_list.get(i); Counter<String> counter = constraints.get(i); asub[i] = new int[counter.keySet().size()]; aval[i] = new double[counter.keySet().size()]; int k = 0; for (Entry<String, Double> e : counter.entrySet()) { int vid = variable2id.get(e.getKey()); asub[i][k] = vid; aval[i][k] = e.getValue(); k++; NUMANZ++; } } // set up variable constraints mosek.Env.boundkey[] bkx = new mosek.Env.boundkey[NUMVAR]; double blx[] = new double[NUMVAR]; double bux[] = new double[NUMVAR]; { for (String v : variable2id.keySet()) { int vid = variable2id.get(v); double[] lowerupper = default_variable_lowerupper; if (variables_lowerupper.containsKey(v)) { lowerupper = variables_lowerupper.get(v); } bkx[vid] = mosek.Env.boundkey.ra; blx[vid] = lowerupper[0]; bux[vid] = lowerupper[1]; } } HashMap<String, Double> var2val = new HashMap<String, Double>(); double[] xx = callILP3(NUMVAR, NUMCON, NUMANZ, c, bkx, blx, bux, asub, aval, bkc, blc, buc, false); for (String v : variable2id.keySet()) { int vid = variable2id.get(v); var2val.put(v, xx[vid]); } return var2val; }