use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class ColumnDecisionTreeTrainerBenchmark method tstF1.
/**
* Test decision tree regression.
* To run this test rename this method so it starts from 'test'.
*/
public void tstF1() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
int ptsCnt = 10000;
Map<Integer, double[]> ranges = new HashMap<>();
ranges.put(0, new double[] { -100.0, 100.0 });
ranges.put(1, new double[] { -100.0, 100.0 });
ranges.put(2, new double[] { -100.0, 100.0 });
int featCnt = 100;
double[] defRng = { -1.0, 1.0 };
Vector[] trainVectors = vecsFromRanges(ranges, featCnt, defRng, new Random(123L), ptsCnt, f1);
SparseDistributedMatrix m = new SparseDistributedMatrix(ptsCnt, featCnt + 1, StorageConstants.COLUMN_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
SparseDistributedMatrixStorage sto = (SparseDistributedMatrixStorage) m.getStorage();
loadVectorsIntoSparseDistributedMatrixCache(sto.cache().getName(), sto.getUUID(), Arrays.stream(trainVectors).iterator(), featCnt + 1);
IgniteFunction<DoubleStream, Double> regCalc = s -> s.average().orElse(0.0);
ColumnDecisionTreeTrainer<VarianceSplitCalculator.VarianceData> trainer = new ColumnDecisionTreeTrainer<>(10, ContinuousSplitCalculators.VARIANCE, RegionCalculators.VARIANCE, regCalc, ignite);
X.println("Training started.");
long before = System.currentTimeMillis();
DecisionTreeModel mdl = trainer.train(new MatrixColumnDecisionTreeTrainerInput(m, new HashMap<>()));
X.println("Training finished in: " + (System.currentTimeMillis() - before) + " ms.");
Vector[] testVectors = vecsFromRanges(ranges, featCnt, defRng, new Random(123L), 20, f1);
IgniteTriFunction<Model<Vector, Double>, Stream<IgniteBiTuple<Vector, Double>>, Function<Double, Double>, Double> mse = Estimators.MSE();
Double accuracy = mse.apply(mdl, Arrays.stream(testVectors).map(v -> new IgniteBiTuple<>(v.viewPart(0, featCnt), v.getX(featCnt))), Function.identity());
X.println("MSE: " + accuracy);
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class MultilayerPerceptron method readFromVector.
/**
* Read matrix with given dimensions from vector starting with offset and return new offset position
* which is last matrix entry position + 1.
*
* @param v Vector to read from.
* @param rows Count of rows of matrix to read.
* @param cols Count of columns of matrix to read.
* @param off Start read position.
* @return New offset position which is last matrix entry position + 1.
*/
private IgniteBiTuple<Integer, Matrix> readFromVector(Vector v, int rows, int cols, int off) {
Matrix mtx = new DenseLocalOnHeapMatrix(rows, cols);
int size = rows * cols;
for (int i = 0; i < size; i++) mtx.setX(i / cols, i % cols, v.getX(off + i));
return new IgniteBiTuple<>(off + size, mtx);
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class CategoricalFeatureProcessor method performSplitGeneric.
/**
* {@inheritDoc}
*/
@Override
public IgniteBiTuple<RegionProjection, RegionProjection> performSplitGeneric(SparseBitSet bs, double[] values, RegionProjection<CategoricalRegionInfo> reg, RegionInfo leftData, RegionInfo rightData) {
int depth = reg.depth();
int lSize = bs.cardinality();
int rSize = reg.sampleIndexes().length - lSize;
IgniteBiTuple<Integer[], Integer[]> lrSamples = splitByBitSet(lSize, rSize, reg.sampleIndexes(), bs);
BitSet leftCats = calculateCats(lrSamples.get1(), values);
CategoricalRegionInfo lInfo = new CategoricalRegionInfo(leftData.impurity(), leftCats);
// TODO: IGNITE-5892 Check how it will work with sparse data.
BitSet rightCats = calculateCats(lrSamples.get2(), values);
CategoricalRegionInfo rInfo = new CategoricalRegionInfo(rightData.impurity(), rightCats);
RegionProjection<CategoricalRegionInfo> rPrj = new RegionProjection<>(lrSamples.get2(), rInfo, depth + 1);
RegionProjection<CategoricalRegionInfo> lPrj = new RegionProjection<>(lrSamples.get1(), lInfo, depth + 1);
return new IgniteBiTuple<>(lPrj, rPrj);
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class IgniteH2Indexing method queryLocalSql.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(String schemaName, String cacheName, final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException {
String type = qry.getType();
String sqlQry = qry.getSql();
String alias = qry.getAlias();
Object[] params = qry.getArgs();
GridQueryCancel cancel = new GridQueryCancel();
final GridCloseableIterator<IgniteBiTuple<K, V>> i = queryLocalSql(schemaName, cacheName, sqlQry, alias, F.asList(params), type, filter, cancel);
return new QueryCursorImpl<>(new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return new ClIter<Cache.Entry<K, V>>() {
@Override
public void close() throws Exception {
i.close();
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public Cache.Entry<K, V> next() {
IgniteBiTuple<K, V> t = i.next();
K key = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get1(), keepBinary, false);
V val = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get2(), keepBinary, false);
return new CacheEntryImpl<>(key, val);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}, cancel);
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.
/**
* @throws Exception If failed.
*/
public void testMultithreadedWithNodeRestart() throws Exception {
final AtomicBoolean stop = new AtomicBoolean();
final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
final Collection<UUID> started = new GridConcurrentHashSet<>();
final Collection<UUID> stopped = new GridConcurrentHashSet<>();
final Random rnd = new Random();
IgniteInternalFuture<?> starterFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < CONSUME_CNT; i++) {
int idx = rnd.nextInt(GRID_CNT);
try {
IgniteEvents evts = grid(idx).events();
UUID consumeId = evts.remoteListenAsync(new P2<UUID, Event>() {
@Override
public boolean apply(UUID uuid, Event evt) {
return true;
}
}, null, EVT_JOB_STARTED).get(3000);
started.add(consumeId);
queue.add(F.t(idx, consumeId));
} catch (ClusterTopologyException ignored) {
// No-op.
}
U.sleep(10);
}
stop.set(true);
return null;
}
}, 8, "consume-starter");
IgniteInternalFuture<?> stopperFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
if (t == null)
continue;
int idx = t.get1();
UUID consumeId = t.get2();
try {
IgniteEvents evts = grid(idx).events();
evts.stopRemoteListenAsync(consumeId).get(3000);
stopped.add(consumeId);
} catch (ClusterTopologyException ignored) {
// No-op.
}
}
return null;
}
}, 4, "consume-stopper");
IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
startGrid("anotherGrid");
stopGrid("anotherGrid");
}
return null;
}
}, 1, "node-restarter");
IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
int idx = rnd.nextInt(GRID_CNT);
try {
grid(idx).compute().runAsync(F.noop()).get(3000);
} catch (IgniteException ignored) {
// Ignore all job execution related errors.
}
}
return null;
}
}, 1, "job-runner");
starterFut.get();
stopperFut.get();
nodeRestarterFut.get();
jobRunnerFut.get();
IgniteBiTuple<Integer, UUID> t;
while ((t = queue.poll()) != null) {
int idx = t.get1();
UUID consumeId = t.get2();
grid(idx).events().stopRemoteListenAsync(consumeId).get(3000);
stopped.add(consumeId);
}
Collection<UUID> notStopped = F.lose(started, true, stopped);
assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
}
Aggregations