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

📄 constraintset.java

📁 使用java编写的关于通讯及串口编程所必需的类库
💻 JAVA
字号:
/* * Constraints.java * This is a required part of the com.adaptiveview.ospso package. * * Copyright (C) 2003 AdaptiveView.com *  * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. *  * This library 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 * General Public License for more details. *  * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *  * You may contact AdaptiveView.com via email at: comments.pso@adaptiveview.com  * */package com.adaptiveview.ospso;/**A ConstraintSet is a container for Constraint instances applicable to a particle. It * holds constraints for: the number of dimensions in the solution space, the * velocity (amount of change in position), and the sociality/individuality factors. * * @author  AdaptiveView.com */public class ConstraintSet implements com.adaptiveview.ospso.dmi.DMI_GPL_License {        private Constraint[] dimensionConstraints;    private Constraint velocityConstraint;    private StochasticFactor sociality;    private StochasticFactor individuality;    private int dimensionsCount = 0;    private boolean hasCommonConstraint = false;        /** Creates a new instance of Constraints with no dimension constraints     * @param dimensionsCount The number of dimensions in the solution space.     * @param velocityConstraint The limits (+/-) on how far a particle can move along a dimension at one time.     * @param sociality The sociality factor (a weight to be multiplied by a random number within a range).     * @param individuality The individuality factor (a weight to be multiplied by a random number within a range).     */    public ConstraintSet(int dimensionsCount,                          Constraint velocityConstraint,                          StochasticFactor sociality,                          StochasticFactor individuality) {        this.dimensionsCount = dimensionsCount;        this.velocityConstraint = velocityConstraint;        this.sociality = sociality;        this.individuality = individuality;        hasCommonConstraint = true;        dimensionConstraints = new Constraint[1];        dimensionConstraints[0] = new Constraint(0.0,0.0,Constraint.NONE);    }        /** Creates a new instance of Constraints from array of constraints     * @param dimensionConstraints An array of constraints limiting each dimemsion in the solution space.     * @param velocityConstraint The limits (+/-) on how far a particle can move along a dimension at one time.     * @param sociality The sociality factor (a weight to be multiplied by a random number within a range).     * @param individuality The individuality factor (a weight to be multiplied by a random number within a range).     * @throws IllegalArgumentException if the dimension constraints array is empty. */    public ConstraintSet(Constraint[] dimensionConstraints,                          Constraint velocityConstraint,                          StochasticFactor sociality,                          StochasticFactor individuality) throws IllegalArgumentException {        dimensionsCount = dimensionConstraints.length;        if (dimensionsCount == 0)             throw new IllegalArgumentException("Invalid constraints - array is empty.");        this.velocityConstraint = velocityConstraint;        this.sociality = sociality;        this.individuality = individuality;        hasCommonConstraint = (dimensionsCount < 2);        this.dimensionConstraints = new Constraint[dimensionsCount];        if (hasCommonConstraint)          this.dimensionConstraints[0] = dimensionConstraints[0];        else          System.arraycopy(dimensionConstraints,0,this.dimensionConstraints,0,dimensionsCount);      }        /** Creates a new instance of Constraints using a single constraint     * @param dimensionsCount The number of dimensions in the solution space.     * @param dimensionConstraint The minimum/maximum boudaries for all dimensions.     * @param velocityConstraint The limits (+/-) on how far a particle can move along a dimension at one time.     * @param sociality The sociality factor (a weight to be multiplied by a random number within a range).     * @param individuality The individuality factor (a weight to be multiplied by a random number within a range).     * @throws IllegalArgumentException if the dimensions count is < 1.      */    public ConstraintSet(int dimensionsCount,                          Constraint dimensionConstraint,                          Constraint velocityConstraint,                          StochasticFactor sociality,                          StochasticFactor individuality) throws IllegalArgumentException {        if (dimensionsCount < 1)             throw new IllegalArgumentException("Number of dimensions must be greater than 0.");        dimensionConstraints = new Constraint[1];        dimensionConstraints[0] = dimensionConstraint;        this.dimensionsCount = dimensionsCount;        this.velocityConstraint = velocityConstraint;        this.sociality = sociality;        this.individuality = individuality;        hasCommonConstraint = true;    }        /** Returns the number of dimensions in the solution space.     * @return  the number of dimensions */    public int getDimensionsCount() {        return this.dimensionsCount;    }        /** Returns a reference to the velocity constraint (for all dimensions).     * @return a reference to the velocity constraint (for all dimensions) */    protected Constraint getVelocityConstraint() {        return this.velocityConstraint;    }        /** Returns a reference to the individuality factor (for all dimensions).     * @return a reference to the individuality factor (for all dimensions) */    protected StochasticFactor getIndividuality() {        return this.individuality;    }        /** Returns a reference to the sociality factor (for all dimensions).     * @return a reference to the sociality factor (for all dimensions) */    protected StochasticFactor getSociality() {        return this.sociality;    }        /** Returns a reference to the constraint for a specified dimension.     * @param dimension the dimension to return the constraint for.     * @throws IllegalArgumentException if the dimension count is invalid     * @return a reference to the constraint for a specified dimension */    protected Constraint getConstraint(int dimension) throws IllegalArgumentException {        if (dimension < 1 || dimension > dimensionsCount)            throw new IllegalArgumentException("Dimension invalid - valid range: [1.." + (dimensionsCount) + "].");        if (hasCommonConstraint)          return dimensionConstraints[0];        return dimensionConstraints[dimension-1];    }   }

⌨️ 快捷键说明

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