Search in sources :

Example 86 with IgniteBiTuple

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);
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) Arrays(java.util.Arrays) FeaturesCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.FeaturesCache) IgniteTestResources(org.apache.ignite.testframework.junits.IgniteTestResources) Random(java.util.Random) BiIndex(org.apache.ignite.ml.trees.trainers.columnbased.BiIndex) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) VarianceSplitCalculator(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.VarianceSplitCalculator) Vector(org.apache.ignite.ml.math.Vector) Estimators(org.apache.ignite.ml.estimators.Estimators) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Level(org.apache.log4j.Level) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) MatrixColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.MatrixColumnDecisionTreeTrainerInput) LabeledVectorDouble(org.apache.ignite.ml.structures.LabeledVectorDouble) BaseDecisionTreeTest(org.apache.ignite.ml.trees.BaseDecisionTreeTest) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) ProjectionsCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.ProjectionsCache) UUID(java.util.UUID) StreamTransformer(org.apache.ignite.stream.StreamTransformer) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) ContextCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.ContextCache) DoubleStream(java.util.stream.DoubleStream) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) SparseMatrixKey(org.apache.ignite.ml.math.distributed.keys.impl.SparseMatrixKey) SplitCache(org.apache.ignite.ml.trees.trainers.columnbased.caches.SplitCache) RegionCalculators(org.apache.ignite.ml.trees.trainers.columnbased.regcalcs.RegionCalculators) IntStream(java.util.stream.IntStream) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Model(org.apache.ignite.ml.Model) HashMap(java.util.HashMap) Function(java.util.function.Function) GiniSplitCalculator(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.GiniSplitCalculator) BiIndexedCacheColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.BiIndexedCacheColumnDecisionTreeTrainerInput) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) MnistUtils(org.apache.ignite.ml.util.MnistUtils) LinkedList(java.util.LinkedList) Properties(java.util.Properties) Iterator(java.util.Iterator) ContinuousSplitCalculators(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.ContinuousSplitCalculators) IOException(java.io.IOException) SplitDataGenerator(org.apache.ignite.ml.trees.SplitDataGenerator) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) Tracer(org.apache.ignite.ml.math.Tracer) StorageConstants(org.apache.ignite.ml.math.StorageConstants) Assert(org.junit.Assert) Collections(java.util.Collections) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) GridCacheProcessor(org.apache.ignite.internal.processors.cache.GridCacheProcessor) InputStream(java.io.InputStream) CacheMode(org.apache.ignite.cache.CacheMode) HashMap(java.util.HashMap) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) MatrixColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.MatrixColumnDecisionTreeTrainerInput) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Function(java.util.function.Function) Random(java.util.Random) DoubleStream(java.util.stream.DoubleStream) Stream(java.util.stream.Stream) IntStream(java.util.stream.IntStream) InputStream(java.io.InputStream) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) LabeledVectorDouble(org.apache.ignite.ml.structures.LabeledVectorDouble) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) Model(org.apache.ignite.ml.Model) DoubleStream(java.util.stream.DoubleStream)

Example 87 with IgniteBiTuple

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);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 88 with IgniteBiTuple

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);
}
Also used : CategoricalRegionInfo(org.apache.ignite.ml.trees.CategoricalRegionInfo) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) RegionProjection(org.apache.ignite.ml.trees.trainers.columnbased.RegionProjection) FeatureVectorProcessorUtils.splitByBitSet(org.apache.ignite.ml.trees.trainers.columnbased.vectors.FeatureVectorProcessorUtils.splitByBitSet) BitSet(java.util.BitSet) SparseBitSet(com.zaxxer.sparsebits.SparseBitSet)

Example 89 with IgniteBiTuple

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);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) Cache(javax.cache.Cache)

Example 90 with IgniteBiTuple

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());
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) IgniteEvents(org.apache.ignite.IgniteEvents) IgniteException(org.apache.ignite.IgniteException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) UUID(java.util.UUID)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)93 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)42 ArrayList (java.util.ArrayList)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 HashMap (java.util.HashMap)20 List (java.util.List)20 IgniteException (org.apache.ignite.IgniteException)20 IOException (java.io.IOException)18 Map (java.util.Map)16 UUID (java.util.UUID)16 ClusterNode (org.apache.ignite.cluster.ClusterNode)13 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 Collection (java.util.Collection)10 Collections (java.util.Collections)10 Iterator (java.util.Iterator)10 Random (java.util.Random)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 Collectors (java.util.stream.Collectors)9