com.jstarcraft.core.utility.RandomUtility Java Examples

The following examples show how to use com.jstarcraft.core.utility.RandomUtility. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RandomSeparator.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
public RandomSeparator(DataSpace space, DataModule dataModule, String matchField, float random) {
    this.dataModule = dataModule;
    ReferenceModule[] modules;
    if (matchField == null) {
        modules = new ReferenceModule[] { new ReferenceModule(dataModule) };
    } else {
        int matchDimension = dataModule.getQualityInner(matchField);
        DataSplitter splitter = new QualityFeatureDataSplitter(matchDimension);
        int size = space.getQualityAttribute(matchField).getSize();
        modules = splitter.split(dataModule, size);
    }
    this.trainReference = new IntegerArray();
    this.testReference = new IntegerArray();
    for (ReferenceModule module : modules) {
        IntegerArray reference = module.getReference();
        for (int cursor = 0, length = reference.getSize(); cursor < length; cursor++) {
            if (RandomUtility.randomFloat(1F) < random) {
                this.trainReference.associateData(reference.getData(cursor));
            } else {
                this.testReference.associateData(reference.getData(cursor));
            }
        }
    }
}
 
Example #2
Source File: RowGlobalMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected RowGlobalMatrix getRandomMatrix(int dimension) {
    MathMatrix from = DenseMatrix.valueOf(1, dimension);
    HashMatrix table = new HashMatrix(true, dimension, dimension, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < dimension - 1; rowIndex++) {
        for (int columnIndex = 0; columnIndex < dimension; columnIndex++) {
            if (RandomUtility.randomBoolean()) {
                table.setValue(rowIndex, columnIndex, 0F);
            }
        }
    }
    MathMatrix to = SparseMatrix.valueOf(dimension - 1, dimension, table);
    RowGlobalMatrix matrix = RowGlobalMatrix.attachOf(from, to);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #3
Source File: ColumnArrayMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected ColumnArrayMatrix getRandomMatrix(int dimension) {
    HashMatrix table = new HashMatrix(true, dimension, dimension, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < dimension; rowIndex++) {
        for (int columnIndex = 0; columnIndex < dimension; columnIndex++) {
            if (RandomUtility.randomBoolean()) {
                table.setValue(rowIndex, columnIndex, 0F);
            }
        }
    }
    SparseMatrix data = SparseMatrix.valueOf(dimension, dimension, table);
    ArrayVector[] vectors = new ArrayVector[dimension];
    for (int columnIndex = 0; columnIndex < dimension; columnIndex++) {
        vectors[columnIndex] = new ArrayVector(data.getColumnVector(columnIndex));
    }
    ColumnArrayMatrix matrix = ColumnArrayMatrix.valueOf(dimension, vectors);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #4
Source File: ScalarIteratorTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testVariance() {
    MathMatrix matrix = DenseMatrix.valueOf(5, 10);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(1F));
    });

    float mean = matrix.getSum(false) / matrix.getElementSize();
    VarianceMessage message = new VarianceMessage(mean);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        message.accumulateValue(scalar.getValue());
    });

    Float2FloatKeyValue keyValue = matrix.getVariance();

    Assert.assertEquals("平均值比较", message.getMean(), keyValue.getKey(), MathUtility.EPSILON);
    Assert.assertEquals("方差值比较", message.getValue(), keyValue.getValue(), MathUtility.EPSILON);
}
 
Example #5
Source File: ScalarIteratorTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testNorm() {
    MathMatrix matrix = DenseMatrix.valueOf(5, 10);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(1F));
    });

    for (int index = 0; index < 10; index++) {
        NormMessage message = new NormMessage(index);
        matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
            message.accumulateValue(scalar.getValue());
        });

        if (index == 0) {
            Assert.assertEquals("范数值比较", message.getValue(), matrix.getNorm(index, true), MathUtility.EPSILON);
        } else {
            Assert.assertEquals("范数值比较", Math.pow(message.getValue(), 1D / message.getPower()), matrix.getNorm(index, true), MathUtility.EPSILON);
        }
    }
}
 
Example #6
Source File: ScalarIteratorTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testMedian() {
    Stream.iterate(1, size -> size + 1).limit(100).forEach(size -> {
        MathVector vector = DenseVector.valueOf(size);
        ArrayList<Integer> data = new ArrayList<>(size);

        Stream.iterate(0, times -> times).limit(100).forEach(times -> {
            for (int index = 0; index < size; index++) {
                int value = RandomUtility.randomInteger(-10, 10);
                vector.setValue(index, value);
                data.add(value);
            }
            Assert.assertEquals("中位数值比较", getMedian(data), vector.getMedian(false), MathUtility.EPSILON);
            data.clear();
        });
    });
}
 
Example #7
Source File: SparseMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected SparseMatrix getRandomMatrix(int dimension) {
    HashMatrix table = new HashMatrix(true, dimension, dimension, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < dimension; rowIndex++) {
        for (int columnIndex = 0; columnIndex < dimension; columnIndex++) {
            if (RandomUtility.randomBoolean()) {
                table.setValue(rowIndex, columnIndex, 0F);
            }
        }
    }
    SparseMatrix matrix = SparseMatrix.valueOf(dimension, dimension, table);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #8
Source File: ColumnGlobalMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected ColumnGlobalMatrix getRandomMatrix(int dimension) {
    MathMatrix from = DenseMatrix.valueOf(dimension, 1);
    HashMatrix table = new HashMatrix(true, dimension, dimension, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < dimension; rowIndex++) {
        for (int columnIndex = 0; columnIndex < dimension - 1; columnIndex++) {
            if (RandomUtility.randomBoolean()) {
                table.setValue(rowIndex, columnIndex, 0F);
            }
        }
    }
    MathMatrix to = SparseMatrix.valueOf(dimension, dimension - 1, table);
    ColumnGlobalMatrix matrix = ColumnGlobalMatrix.attachOf(from, to);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #9
Source File: RandomDataSorter.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public ReferenceModule sort(DataModule module) {
    int size = module.getSize();
    IntegerArray reference = new IntegerArray(size, size);
    for (int index = 0; index < size; index++) {
        reference.associateData(index);
    }
    int from = 0;
    int to = size;
    for (int index = from; index < to; index++) {
        int random = RandomUtility.randomInteger(from, to);
        int data = reference.getData(index);
        reference.setData(index, reference.getData(random));
        reference.setData(random, data);
    }
    return new ReferenceModule(reference, module);
}
 
Example #10
Source File: AbstractEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    RandomUtility.setSeed(0L);
    int rowSize = 1000;
    int columnSize = 1000;
    HashMatrix featureTable = new HashMatrix(true, rowSize, columnSize, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {
        for (int columnIndex = 0; columnIndex < columnSize; columnIndex++) {
            if (RandomUtility.randomFloat(1F) < 0.5F) {
                featureTable.setValue(rowIndex, columnIndex, RandomUtility.randomFloat(1F));
            }
        }
    }
    SparseMatrix featureMatrix = SparseMatrix.valueOf(rowSize, columnSize, featureTable);
    Evaluator<L, R> evaluator = getEvaluator(featureMatrix);
    Integer2FloatKeyValue sum = evaluate(evaluator, featureMatrix);
    Assert.assertThat(sum.getValue() / sum.getKey(), CoreMatchers.equalTo(getMeasure()));
}
 
Example #11
Source File: QuantityAttributeTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void testConvertValue() {
    int size = 1000;
    float[] datas = new float[size];
    for (int index = 0; index < size; index++) {
        datas[index] = index;
    }
    RandomUtility.shuffle(datas);
    QuantityAttribute<Float> attribute = getQuantityAttribute();
    for (int index = 0; index < size; index++) {
        Assert.assertEquals(datas[index], attribute.convertData(datas[index]), 0F);
    }
    Assert.assertEquals(999F, attribute.getMaximum(), 0F);
    Assert.assertEquals(0F, attribute.getMinimum(), 0F);
    for (int index = 0; index < size; index++) {
        Assert.assertEquals(datas[index], attribute.convertData(datas[index]), 0F);
    }
    Assert.assertEquals(999F, attribute.getMaximum(), 0F);
    Assert.assertEquals(0F, attribute.getMinimum(), 0F);
}
 
Example #12
Source File: QualityAttributeTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void testConvertValue() {
    int size = 1000;
    float[] datas = new float[size];
    for (int index = 0; index < size; index++) {
        datas[index] = index;
    }
    RandomUtility.shuffle(datas);
    QualityAttribute<Float> attribute = getQualityAttribute();
    for (int index = 0; index < size; index++) {
        Assert.assertEquals(index, attribute.convertData(datas[index]));
    }
    Assert.assertEquals(size, attribute.getSize());
    for (int index = 0; index < size; index++) {
        Assert.assertEquals(index, attribute.convertData(datas[index]));
    }
    Assert.assertEquals(size, attribute.getSize());
}
 
Example #13
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * item(离散:1:稠密)-profile(连续:n:稀疏)
 * 
 * <pre>
 * 可以当作item(离散:1:稠密)-item(离散:1:稠密)-degree(连续:1:稠密)
 * </pre>
 */
@Test
public void mockItemProfile() throws Exception {
    File file = new File("data/mock/item-profile");
    FileUtils.deleteQuietly(file);
    file.getParentFile().mkdirs();
    file.createNewFile();
    StringBuilder buffer = new StringBuilder();
    try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
        for (int leftIndex = 0; leftIndex < itemSize; leftIndex++) {
            buffer.setLength(0);
            for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
                if (RandomUtility.randomFloat(1F) < ratio) {
                    float degree = RandomUtility.randomFloat(profileScope);
                    buffer.append(degree);
                }
                buffer.append(" ");
            }
            String profile = buffer.substring(0, buffer.length() - 1);
            out.write(StringUtility.format("{} {}", leftIndex, profile));
            out.newLine();
        }
    }
}
 
Example #14
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * user(离散:1:稠密)-profile(连续:n:稀疏)
 * 
 * <pre>
 * 可以当作user(离散:1:稠密)-user(离散:1:稠密)-degree(连续:1:稠密)
 * </pre>
 * 
 * >
 */
@Test
public void mockUserProfile() throws Exception {
    File file = new File("data/mock/user-profile");
    FileUtils.deleteQuietly(file);
    file.getParentFile().mkdirs();
    file.createNewFile();
    StringBuilder buffer = new StringBuilder();
    try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
        for (int leftIndex = 0; leftIndex < userSize; leftIndex++) {
            buffer.setLength(0);
            for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
                if (RandomUtility.randomFloat(1F) < ratio) {
                    float degree = RandomUtility.randomFloat(profileScope);
                    buffer.append(degree);
                }
                buffer.append(" ");
            }
            String profile = buffer.substring(0, buffer.length() - 1);
            out.write(StringUtility.format("{} {}", leftIndex, profile));
            out.newLine();
        }
    }
}
 
Example #15
Source File: IRRGModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);
    for (MatrixScalar term : scoreMatrix) {
        int userIndex = term.getRow();
        int itemIndex = term.getColumn();
        dataTable.put(userIndex, itemIndex, term.getValue());
    }

    correlationRegularization = configuration.getFloat("recommender.alpha");
    userFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(0.8F));
    });
    itemFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(0.8F));
    });

    computeAssociationRuleByItem();
    sortAssociationRuleByItem();
    computeAssociationRuleByGroup();
    sortAssociationRuleByGroup();
    complementAssociationRule();
    complementMatrix = SparseMatrix.valueOf(itemSize, itemSize, itemCorrsAR_added);
}
 
Example #16
Source File: KernelTrickTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testKernelTrick() {
    MathVector leftVector = DenseVector.valueOf(1000);
    MathVector rightVector = DenseVector.valueOf(1000);
    for (int index = 0; index < 1000; index++) {
        leftVector.setValue(index, RandomUtility.randomFloat(0.005F));
        rightVector.setValue(index, RandomUtility.randomFloat(0.005F));
    }

    KernelTrick kernel = getKernelTrick();
    KernelDistance distance = new KernelDistance(kernel);
    KernelSimilarity similarity = new KernelSimilarity(kernel);

    Assert.assertTrue(kernel.calculate(leftVector, leftVector) > kernel.calculate(leftVector, rightVector));
    Assert.assertTrue(kernel.calculate(rightVector, rightVector) > kernel.calculate(leftVector, rightVector));

    Assert.assertEquals(0F, distance.getCoefficient(leftVector, leftVector), MathUtility.EPSILON);
    Assert.assertEquals(0F, distance.getCoefficient(rightVector, rightVector), MathUtility.EPSILON);
    Assert.assertTrue(distance.getCoefficient(leftVector, rightVector) > 0F);

    Assert.assertEquals(1F, similarity.getCoefficient(leftVector, leftVector), MathUtility.EPSILON);
    Assert.assertEquals(1F, similarity.getCoefficient(rightVector, rightVector), MathUtility.EPSILON);
    Assert.assertTrue(similarity.getCoefficient(leftVector, rightVector) < 1F);
}
 
Example #17
Source File: MatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Test
public void testFourArithmeticOperation() throws Exception {
    EnvironmentContext context = EnvironmentFactory.getContext();
    Future<?> task = context.doTask(() -> {
        RandomUtility.setSeed(0L);
        int dimension = 10;
        MathMatrix dataMatrix = getZeroMatrix(dimension);
        dataMatrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
            scalar.setValue(RandomUtility.randomFloat(10F));
        });
        MathMatrix copyMatrix = getZeroMatrix(dimension);
        float sum = dataMatrix.getSum(false);

        copyMatrix.copyMatrix(dataMatrix, false);
        Assert.assertThat(copyMatrix.getSum(false), CoreMatchers.equalTo(sum));

        dataMatrix.subtractMatrix(copyMatrix, false);
        Assert.assertThat(dataMatrix.getSum(false), CoreMatchers.equalTo(0F));

        dataMatrix.addMatrix(copyMatrix, false);
        Assert.assertThat(dataMatrix.getSum(false), CoreMatchers.equalTo(sum));

        dataMatrix.divideMatrix(copyMatrix, false);
        Assert.assertThat(dataMatrix.getSum(false), CoreMatchers.equalTo(dataMatrix.getElementSize() + 0F));

        dataMatrix.multiplyMatrix(copyMatrix, false);
        Assert.assertThat(dataMatrix.getSum(false), CoreMatchers.equalTo(sum));
    });
    task.get();
}
 
Example #18
Source File: RowHashMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected HashMatrix getRandomMatrix(int dimension) {
    HashMatrix matrix = new HashMatrix(true, dimension, dimension, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < dimension; rowIndex++) {
        for (int columnIndex = 0; columnIndex < dimension; columnIndex++) {
            if (RandomUtility.randomBoolean()) {
                matrix.setValue(rowIndex, columnIndex, 0F);
            }
        }
    }
    return matrix;
}
 
Example #19
Source File: LocalMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected LocalMatrix getRandomMatrix(int dimension) {
    LocalMatrix matrix = new LocalMatrix(DenseMatrix.valueOf(dimension * 3, dimension * 3), dimension, dimension * 2, dimension, dimension * 2);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #20
Source File: QuantityAttributeTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void testConcurrent() throws Exception {
    int size = 1000;
    float[] datas = new float[size];
    for (int index = 0; index < size; index++) {
        datas[index] = index;
    }
    RandomUtility.shuffle(datas);

    final int numberOfThread = 10;
    ExecutorService executor = Executors.newFixedThreadPool(numberOfThread);
    final CyclicBarrier barrier = new CyclicBarrier(numberOfThread + 1);
    QuantityAttribute<Float> attribute = getQuantityAttribute();
    for (int thread = 0; thread < numberOfThread; thread++) {
        executor.submit(() -> {
            try {
                barrier.await();
                for (int index = 0; index < size; index++) {
                    Assert.assertEquals(datas[index], attribute.convertData(datas[index]), 0F);
                }
                Assert.assertEquals(999F, attribute.getMaximum(), 0F);
                Assert.assertEquals(0F, attribute.getMinimum(), 0F);
                barrier.await();
            } catch (Exception exception) {
                Assert.fail();
            }
        });
    }
    // 等待所有线程开始
    barrier.await();
    // 等待所有线程结束
    barrier.await();
}
 
Example #21
Source File: QualityAttributeTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void testConcurrent() throws Exception {
    int size = 1000;
    float[] datas = new float[size];
    for (int index = 0; index < size; index++) {
        datas[index] = index;
    }
    RandomUtility.shuffle(datas);

    final int numberOfThread = 10;
    ExecutorService executor = Executors.newFixedThreadPool(numberOfThread);
    final CyclicBarrier barrier = new CyclicBarrier(numberOfThread + 1);
    QualityAttribute<Float> attribute = getQualityAttribute();
    for (int thread = 0; thread < numberOfThread; thread++) {
        executor.submit(() -> {
            try {
                barrier.await();
                for (int index = 0; index < size; index++) {
                    Assert.assertEquals(index, attribute.convertData(datas[index]));
                }
                Assert.assertEquals(size, attribute.getSize());
                barrier.await();
            } catch (Exception exception) {
                exception.printStackTrace();
                Assert.fail();
            }
        });
    }
    // 等待所有线程开始
    barrier.await();
    // 等待所有线程结束
    barrier.await();
}
 
Example #22
Source File: DefaultMasker.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void mask(MathMatrix matrix, int iteration, int epoch) {
    float current = schedule.valueAt(iteration, epoch);

    matrix.iterateElement(MathCalculator.PARALLEL, (scalar) -> {
        float value = scalar.getValue();
        scalar.setValue(RandomUtility.randomFloat(1F) < current ? 0F : value);
    });
}
 
Example #23
Source File: AbstractRankingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected IntSet getLeft(MathVector vector) {
    IntSet itemSet = new IntOpenHashSet();
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            itemSet.add(scalar.getIndex());
        }
    }
    return itemSet;
}
 
Example #24
Source File: AbstractRankingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected IntList getRight(MathVector vector) {
    IntList recommendList = new IntArrayList(vector.getElementSize());
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            recommendList.add(scalar.getIndex());
        }
    }
    return recommendList;
}
 
Example #25
Source File: AbstractRatingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected FloatList getRight(MathVector vector) {
    FloatList recommendList = new FloatArrayList(vector.getElementSize());
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            recommendList.add(scalar.getValue());
        } else {
            recommendList.add(scalar.getValue() * 0.5F);
        }
    }
    return recommendList;
}
 
Example #26
Source File: DenseMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected DenseMatrix getRandomMatrix(int dimension) {
    DenseMatrix matrix = DenseMatrix.valueOf(dimension, dimension);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #27
Source File: KFoldCrossValidationSeparator.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
public KFoldCrossValidationSeparator(DataModule dataModule, int number) {
    this.dataModule = dataModule;
    this.number = number;
    this.folds = new Integer[this.dataModule.getSize()];
    for (int index = 0, size = this.folds.length; index < size; index++) {
        this.folds[index] = index % number;
    }
    // 通过随机与交换的方式实现打乱排序的目的.
    RandomUtility.shuffle(this.folds);
}
 
Example #28
Source File: SymmetryMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected SymmetryMatrix getRandomMatrix(int dimension) {
    SymmetryMatrix matrix = new SymmetryMatrix(dimension);
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomInteger(dimension));
    });
    return matrix;
}
 
Example #29
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
/**
 * user(离散:1:稠密)-item(离散:1:稠密)-score(连续:1:稠密)-instant(离散:1:稠密)-location(离散:2:稠密)-comment(连续:n:稀疏)
 */
@Test
public void mockUserItemScoreInstantLocationComment() throws Exception {
    File file = new File("data/mock/user-item-score-instant-location-comment");
    FileUtils.deleteQuietly(file);
    file.getParentFile().mkdirs();
    file.createNewFile();
    StringBuilder buffer = new StringBuilder();
    try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
        for (int leftIndex = 0; leftIndex < userSize; leftIndex++) {
            for (int rightIndex = 0; rightIndex < itemSize; rightIndex++) {
                // 此处故意选择特定的数据(TODO 考虑改为利用正态分布)
                if (rightIndex < 10 || RandomUtility.randomFloat(1F) < ratio) {
                    // 得分
                    float score = RandomUtility.randomFloat(scoreScope);
                    // 时间
                    int instant = RandomUtility.randomInteger(instantSize);
                    // 地点(经度)
                    int longitude = RandomUtility.randomInteger(locationSize);
                    // 地点(纬度)
                    int latitude = RandomUtility.randomInteger(locationSize);
                    buffer.setLength(0);
                    for (int commentIndex = 0; commentIndex < commentSize; commentIndex++) {
                        if (RandomUtility.randomFloat(1F) < ratio) {
                            float degree = RandomUtility.randomFloat(commentScope);
                            buffer.append(degree);
                        }
                        buffer.append(" ");
                    }
                    // 评论
                    String comment = buffer.substring(0, buffer.length() - 1);
                    out.write(StringUtility.format("{} {} {} {} {} {} {}", leftIndex, rightIndex, score, instant, longitude, latitude, comment));
                    out.newLine();
                }

            }
        }
    }
}
 
Example #30
Source File: LeastRecentlyUsedTransienceStrategyTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformance() throws Exception {
    Map<String, String> configuration = new HashMap<>();
    configuration.put(LeastRecentlyUsedTransienceStrategy.PARAMETER_MINIMUN_SIZE, String.valueOf(MINIMUN_SIZE));
    configuration.put(LeastRecentlyUsedTransienceStrategy.PARAMETER_MAXIMUN_SIZE, String.valueOf(MAXIMUN_SIZE));
    configuration.put(LeastRecentlyUsedTransienceStrategy.PARAMETER_CONCURRENCY_LEVEL, String.valueOf(THREAD_SIZE));
    LeastRecentlyUsedTransienceStrategy strategy = new LeastRecentlyUsedTransienceStrategy("stratiegy", configuration);
    strategy.start();
    TransienceManager manager = strategy.getTransienceManager(null);

    // 多线程并发读写操作
    int threadSize = 100;
    AtomicBoolean run = new AtomicBoolean(true);
    AtomicLong operationCount = new AtomicLong();
    for (int index = 0; index < threadSize; index++) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (run.get()) {
                    int readId = RandomUtility.randomInteger(0, MAXIMUN_SIZE + MINIMUN_SIZE);
                    int wirteId = RandomUtility.randomInteger(0, MAXIMUN_SIZE + MINIMUN_SIZE);
                    manager.createInstance(wirteId, MockEntityObject.instanceOf(wirteId, "birdy" + wirteId, "hong", 0, 0));
                    manager.retrieveInstance(readId);
                    operationCount.incrementAndGet();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }

    Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPIRE_SECONDS, TimeUnit.SECONDS));
    run.set(false);
    if (manager.getSize() == 0) {
        Assert.fail();
    }

    String message = StringUtility.format("{}策略{}条线程在{}秒内执行{}次读写操作", strategy.getName(), threadSize, EXPIRE_SECONDS, operationCount.get());
    logger.debug(message);
}