You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Artem Barger (JIRA)" <ji...@apache.org> on 2016/08/10 22:04:20 UTC

[jira] [Comment Edited] (RNG-5) Create API usage demo example application.

    [ https://issues.apache.org/jira/browse/RNG-5?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15416103#comment-15416103 ] 

Artem Barger edited comment on RNG-5 at 8/10/16 10:03 PM:
----------------------------------------------------------

Add this as an abstract base class

{code:java}
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.rng;

abstract public class AbstractMonteCarloComputation {

    /**
     * Desired approximation error bound
     */
    private double error;

    /**
     * Random source provider.
     */
    private UniformRandomProvider provider;

    /**
     * Number of generated points
     */
    private int generatedNums;

    /**
     * Actual approximation error received in computation.
     */
    private double actualError;

    /**
     * Monte Carlo simulation constructor.
     *
     * @param error  approximation error
     * @param source random source {@link RandomSource} to indicate the RNG algorithm
     */
    public AbstractMonteCarloComputation(double error, RandomSource source) {
        this.error = error;
        this.provider = RandomSource.create(source);
    }

    /**
     * Uses quasi Monte-Carlo simulation to approximate computation.
     *
     * @return approximated by simulation value, requires implementation of desired computation
     * inside {@link AbstractMonteCarloComputation#computeValue()} method
     * @see <a href="https://en.wikipedia.org/wiki/Quasi-Monte_Carlo_method">Wikipedia</a>
     */
    public double simulation() {
        double newValue = Double.MAX_VALUE;
        double oldValue = Double.MIN_VALUE;
        while ((actualError = Math.abs(oldValue - newValue)) > error) {
            oldValue = newValue;
            newValue = computeValue();
        }

        return newValue;
    }

    abstract public double computeValue();

    /**
     * Returns the amount of numbers required to generate to converge with desired error value.
     *
     * @return amount of numbers generated
     */
    public int generatedNums() {
        return generatedNums;
    }

    /**
     * Returns the absolute error of approximated PI value.
     *
     * @return the actual approximation error
     */
    public double getError() {
        return actualError;
    }

}
{code}

and make two demo classes to extend it.


was (Author: c0rwin):
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.rng;

abstract public class AbstractMonteCarloComputation {

    /**
     * Desired approximation error bound
     */
    private double error;

    /**
     * Random source provider.
     */
    private UniformRandomProvider provider;

    /**
     * Number of generated points
     */
    private int generatedNums;

    /**
     * Actual approximation error received in computation.
     */
    private double actualError;

    /**
     * Monte Carlo simulation constructor.
     *
     * @param error  approximation error
     * @param source random source {@link RandomSource} to indicate the RNG algorithm
     */
    public AbstractMonteCarloComputation(double error, RandomSource source) {
        this.error = error;
        this.provider = RandomSource.create(source);
    }

    /**
     * Uses quasi Monte-Carlo simulation to approximate computation.
     *
     * @return approximated by simulation value, requires implementation of desired computation
     * inside {@link AbstractMonteCarloComputation#computeValue()} method
     * @see <a href="https://en.wikipedia.org/wiki/Quasi-Monte_Carlo_method">Wikipedia</a>
     */
    public double simulation() {
        double newValue = Double.MAX_VALUE;
        double oldValue = Double.MIN_VALUE;
        while ((actualError = Math.abs(oldValue - newValue)) > error) {
            oldValue = newValue;
            newValue = computeValue();
        }

        return newValue;
    }

    abstract public double computeValue();

    /**
     * Returns the amount of numbers required to generate to converge with desired error value.
     *
     * @return amount of numbers generated
     */
    public int generatedNums() {
        return generatedNums;
    }

    /**
     * Returns the absolute error of approximated PI value.
     *
     * @return the actual approximation error
     */
    public double getError() {
        return actualError;
    }

}


> Create API usage demo example application.
> ------------------------------------------
>
>                 Key: RNG-5
>                 URL: https://issues.apache.org/jira/browse/RNG-5
>             Project: Commons RNG
>          Issue Type: Task
>            Reporter: Artem Barger
>         Attachments: ComputePiDemo.java
>
>
> Need to create a demo application for user guide to demonstrate how to use the API and show possible application.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)