⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 averageobserver.java

📁 peersim最新版1.0.4
💻 JAVA
字号:
/* * Copyright (c) 2003-2005 The BISON Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */package example.aggregation;import peersim.config.*;import peersim.core.*;import peersim.vector.*;import peersim.util.IncrementalStats;/** * Print statistics for an average aggregation computation. Statistics printed * are defined by {@link IncrementalStats#toString} *  * @author Alberto Montresor * @version $Revision: 1.17 $ */public class AverageObserver implements Control {    // /////////////////////////////////////////////////////////////////////    // Constants    // /////////////////////////////////////////////////////////////////////    /**     * Config parameter that determines the accuracy for standard deviation     * before stopping the simulation. If not defined, a negative value is used     * which makes sure the observer does not stop the simulation     *      * @config     */    private static final String PAR_ACCURACY = "accuracy";    /**     * The protocol to operate on.     *      * @config     */    private static final String PAR_PROT = "protocol";    // /////////////////////////////////////////////////////////////////////    // Fields    // /////////////////////////////////////////////////////////////////////    /**     * The name of this observer in the configuration. Initialized by the     * constructor parameter.     */    private final String name;    /**     * Accuracy for standard deviation used to stop the simulation; obtained     * from config property {@link #PAR_ACCURACY}.     */    private final double accuracy;    /** Protocol identifier; obtained from config property {@link #PAR_PROT}. */    private final int pid;    // /////////////////////////////////////////////////////////////////////    // Constructor    // /////////////////////////////////////////////////////////////////////    /**     * Creates a new observer reading configuration parameters.     */    public AverageObserver(String name) {        this.name = name;        accuracy = Configuration.getDouble(name + "." + PAR_ACCURACY, -1);        pid = Configuration.getPid(name + "." + PAR_PROT);    }    // /////////////////////////////////////////////////////////////////////    // Methods    // /////////////////////////////////////////////////////////////////////    /**     * Print statistics for an average aggregation computation. Statistics     * printed are defined by {@link IncrementalStats#toString}. The current     * timestamp is also printed as a first field.     *      * @return if the standard deviation is less than the given     *         {@value #PAR_ACCURACY}.     */    public boolean execute() {        long time = peersim.core.CommonState.getTime();        IncrementalStats is = new IncrementalStats();        for (int i = 0; i < Network.size(); i++) {            SingleValue protocol = (SingleValue) Network.get(i)                    .getProtocol(pid);            is.add(protocol.getValue());        }        /* Printing statistics */        System.out.println(name + ": " + time + " " + is);        /* Terminate if accuracy target is reached */        return (is.getStD() <= accuracy);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -