Package net.sf.cpsolver.ifs.model

Examples of net.sf.cpsolver.ifs.model.Model


    /** Initialization */
    public void init(Solver solver) {}
   
    /** Variable selection */
    public Variable selectVariable(Solution solution) {
        Model model = solution.getModel();
        if (model.nrUnassignedVariables()==0) return null;
        Variable variable = null;
        for (Enumeration e=model.unassignedVariables().elements();e.hasMoreElements();) {
            Variable v = (Variable)e.nextElement();
            if (variable==null || v.compareTo(variable)<0) variable = v;
        }
        return variable;
    }
View Full Code Here


     * its value is below or equal to zero (continue with the next selection otherwise).
     * Return null when the given number of idle iterations is reached.
     */
    public Neighbour selectNeighbour(Solution solution) {
        if (iT0<0) iT0 = System.currentTimeMillis();
        Model model = (Model)solution.getModel();
        while (true) {
            iIter ++;
            if (iIter % 10000 == 0) {
                if (sInfo) {
                    sLog.info("Iter="+iIter/1000+"k, NonImpIter="+sDF2.format((iIter-iLastImprovingIter)/1000.0)+"k, Speed="+sDF2.format(1000.0*iIter/(System.currentTimeMillis()-iT0))+" it/s");
View Full Code Here

                iTabu.resize(Math.min(iTabuMaxSize,iTabu.size()+1));
            }
        }
       
        boolean acceptConflicts = solution.getModel().getBestUnassignedVariables()>0;
        Model model = solution.getModel();
        double bestEval = 0.0;
        Vector best = null;
        for (Enumeration e=model.variables().elements();e.hasMoreElements();) {
            Variable variable = (Variable)e.nextElement();
            Value assigned = variable.getAssignment();
            double assignedVal = (assigned==null?iConflictWeight:iValueWeight*assigned.toDouble());
            for (Enumeration f=variable.values().elements();f.hasMoreElements();) {
                Value value = (Value)f.nextElement();
                if (value.equals(assigned)) continue;
                double eval = iValueWeight*value.toDouble() - assignedVal;
                if (acceptConflicts) {
                    Set conflicts = model.conflictValues(value);
                    for (Iterator i=conflicts.iterator();i.hasNext();) {
                        Value conflict = (Value)i.next();
                        eval -= iValueWeight*conflict.toDouble();
                        eval += iConflictWeight * (1.0+(iStat==null?0.0:iStat.countRemovals(solution.getIteration(), conflict, value)));
                    }
                } else {
                    if (model.inConflict(value)) continue;
                }
                if (iTabu!=null && iTabu.contains(tabuElement(value))) {
                    int un = model.nrUnassignedVariables()-(assigned==null?0:1);
                    if (un>model.getBestUnassignedVariables()) continue;
                    if (un==model.getBestUnassignedVariables() && model.getTotalValue()+eval>=solution.getBestValue()) continue;
                }
                if (best==null || bestEval>eval) {
                    if (best==null)
                        best = new Vector();
                    else
                        best.clear();
                    best.add(value);
                    bestEval = eval;
                } else if (bestEval==eval) {
                    best.add(value);
                }
            }
        }
       
        if (best==null) {
            sLog.debug("  [tabu] --none--");
            iFirstIteration=-1;
            if (iTabu!=null) iTabu.clear();
            return null;
        }
        Value bestVal = (Value)ToolBox.random(best);
       
        if (sLog.isDebugEnabled()) {
            Set conflicts = model.conflictValues(bestVal);
            double wconf = (iStat==null?0.0:iStat.countRemovals(solution.getIteration(), conflicts, bestVal));
            sLog.debug("  [tabu] "+bestVal+" ("+(bestVal.variable().getAssignment()==null?"":"was="+bestVal.variable().getAssignment()+", ")+"val="+bestEval+(conflicts.isEmpty()?"":", conf="+(wconf+conflicts.size())+"/"+conflicts)+")");
        }
       
        if (iTabu!=null)
View Full Code Here

            } else if (idle%(iMaxIdleIterations/(iTabuMaxSize-iTabuMinSize))==0) {
                iTabu.resize(Math.min(iTabuMaxSize,iTabu.size()+1));
            }
        }

        Model model = solution.getModel();
        double bestEval = 0.0;
        Vector best = null;

        Value assigned = variable.getAssignment();
        //double assignedVal = (assigned==null?-iConflictWeight:iValueWeight*assigned.toDouble());
        double assignedVal = (assigned==null?iConflictWeight:iValueWeight*assigned.toDouble());
        for (Enumeration f=variable.values().elements();f.hasMoreElements();) {
            Value value = (Value)f.nextElement();
            if (value.equals(assigned)) continue;
            Set conflicts = model.conflictValues(value);
            double eval = iValueWeight*value.toDouble() - assignedVal;
            for (Iterator i=conflicts.iterator();i.hasNext();) {
                Value conflict = (Value)i.next();
                eval -= iValueWeight*conflict.toDouble();
                eval += iConflictWeight * (1.0+(iStat==null?0.0:iStat.countRemovals(solution.getIteration(), conflict, value)));
            }
            if (iTabu!=null && iTabu.contains(tabuElement(value))) {
                //if (model.getTotalValue()+eval>=solution.getBestValue()) continue;
                int un = model.nrUnassignedVariables()-(assigned==null?0:1);
                if (un>model.getBestUnassignedVariables()) continue;
                if (un==model.getBestUnassignedVariables() && model.getTotalValue()+eval>=solution.getBestValue()) continue;
        }
            if (best==null || bestEval>eval) {
                if (best==null)
                    best = new Vector();
                else
                    best.clear();
                best.add(value);
                bestEval = eval;
            } else if (bestEval==eval) {
                best.add(value);
            }
        }
       
        if (best==null) return null;
        Value bestVal = (Value)ToolBox.random(best);
       
        if (sLog.isDebugEnabled()) {
            Set conflicts = model.conflictValues(bestVal);
            double wconf = (iStat==null?0.0:iStat.countRemovals(solution.getIteration(), conflicts, bestVal));
            sLog.debug("  [tabu] "+bestVal+" ("+(bestVal.variable().getAssignment()==null?"":"was="+bestVal.variable().getAssignment()+", ")+"val="+bestEval+(conflicts.isEmpty()?"":", conf="+(wconf+conflicts.size())+"/"+conflicts)+")");
        }
       
        if (iTabu!=null) iTabu.add(tabuElement(bestVal));
View Full Code Here

    public void init(Solver solver) {
    }
   
    /** Neighbour selection */
    public Neighbour selectNeighbour(Solution solution) {
        Model model = solution.getModel();
        if (iVarEn==null) {
            iVarEn = new RandomEnumeration(model.variables(), iRandomOrder);
            iVariable = (Variable)iVarEn.nextElement();
            Vector v2 = new Vector(iVariable.values().size()+model.variables().size());
            if (iAllowSwaps && iVariable instanceof Swapable)
                v2.addAll(model.variables());
            v2.addAll(iVariable.values());
            iValEn = new RandomEnumeration(v2, iRandomOrder);
        }
        SimpleNeighbour n = null;
        if (!iValEn.hasMoreElements()) {
            if (!iVarEn.hasMoreElements()) {
                iVarEn = new RandomEnumeration(model.variables(), iRandomOrder);
            }
            iVariable = (Variable)iVarEn.nextElement();
            Vector v2 = new Vector(iVariable.values().size()+model.variables().size());
            if (iAllowSwaps && iVariable instanceof Swapable)
                v2.addAll(model.variables());
            v2.addAll(iVariable.values());
            iValEn = new RandomEnumeration(v2, iRandomOrder);
        }
        Object object = iValEn.nextElement();
        if (object instanceof Variable) {
            Variable anotherVariable = (Variable)ToolBox.random(model.variables());
            if (iVariable.equals(anotherVariable)) return null;
            return ((Swapable)iVariable).findSwap(anotherVariable);
        } else {
            Value value = (Value)object;
            if (value.equals(iVariable.getAssignment())) return null;
            Set conflicts = model.conflictValues(value);
            double eval = iValueWeight * value.toDouble();
            if (iVariable.getAssignment()!=null)
                eval -= iValueWeight * iVariable.getAssignment().toDouble();
            else
                eval -= iConflictWeight;
            eval += iConflictWeight * model.conflictValues(value).size();
            return new ItcSimpleNeighbour(iVariable,value,eval);
        }
    }
View Full Code Here

        iConflictWeight = properties.getPropertyDouble("Value.ConflictWeight", iConflictWeight);
        iValueWeight = properties.getPropertyDouble("Value.ValueWeight", iValueWeight);
    }
    public void init(Solver solver) {}
    public Neighbour selectNeighbour(Solution solution) {
        Model model = solution.getModel();
        boolean canConflict = model.nrAssignedVariables()!=0;
        Variable variable = (Variable)ToolBox.random(model.variables());
        Value value = (Value)ToolBox.random(variable.values());
        if (value.equals(variable.getAssignment())) return null;
        Set conflicts = model.conflictValues(value);
        double eval = iValueWeight * value.toDouble();
        if (variable.getAssignment()!=null)
            eval -= iValueWeight * variable.getAssignment().toDouble();
        else
            eval -= iConflictWeight;
        eval += iConflictWeight * model.conflictValues(value).size();
        return new ItcSimpleNeighbour(variable,value,eval);
    }
View Full Code Here

    public ItcNotConflictingMove(DataProperties properties) {}
    /** Initialization */
    public void init(Solver solver) {}
    /** Neighbour selection */
    public Neighbour selectNeighbour(Solution solution) {
        Model model = solution.getModel();
        Variable variable = (Variable)ToolBox.random(model.variables());
        Value value = (Value)ToolBox.random(variable.values());
        if (value.equals(variable.getAssignment())) return null;
        if (model.inConflict(value)) return null;
        return new ItcSimpleNeighbour(variable,value);
    }
View Full Code Here

    }
    /** Initialization */
    public void init(Solver solver) {}
    /** Neighbour selection */
    public Neighbour selectNeighbour(Solution solution) {
        Model model = solution.getModel();
        Variable variable = (Variable)ToolBox.random(model.variables());
        if (!(variable instanceof Swapable)) return null;
        Variable anotherVariable = (Variable)ToolBox.random(model.variables());
        if (variable.equals(anotherVariable)) return null;
        return ((Swapable)variable).findSwap(anotherVariable);
    }
View Full Code Here

TOP

Related Classes of net.sf.cpsolver.ifs.model.Model

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.