当前位置: 首页>>代码示例>>C++>>正文


C++ Constraint::addVariable方法代码示例

本文整理汇总了C++中Constraint::addVariable方法的典型用法代码示例。如果您正苦于以下问题:C++ Constraint::addVariable方法的具体用法?C++ Constraint::addVariable怎么用?C++ Constraint::addVariable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Constraint的用法示例。


在下文中一共展示了Constraint::addVariable方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: getLPP

LPP* CnLPP::getLPP()
{
    setComment("C(n) LPP " + gameId());
    
    // each line is a structural variable
    
    for (const Line& l: b.getLineList()) {        
        std::string v_name = to_string(l);

        setVariableBounds(v_name, 0.0, 1.0);
    }
    
    // each dot is a structural variable

    for (const Dot& d: b.getDotList()) {
        std::string v_name = "dot_" + to_string(d);
        setVariableBounds(v_name, 0.0, 1.0);

        getObjective().push_back(std::pair<std::string, double>(v_name, -1.0));

        // Constraint for exact problems.
        //
        //   variables are either 0 or 1 (i.e. the move is either played or not)

        if (getFlag("exact")) {
            setVariableBoolean(v_name, true);
        }
    }
    
    // constraints:
    //
    //   (1) for each segment, sum of weights of moves that use this segment is <= 1
    //   (2) for each segment, 
    //          dot_weight >= sum of weights of lines that use this segment
    //
    //   (3) sum of line weights = n
    //
    //   (4) dot_weight for dots from the cross is equal to 1

    //   (1) for each segment, sum of weights of moves that use this segment is <= 1
    //       (IT IS REDUNDANT TO (2) because dot_weight <= 1):
    //   (2) for each segment, 
    //          dot_weight >= sum of weights of moves that remove this segment

    for (const Segment& s: b.getSegmentList()) {
        Constraint c;
        c.setName("used_segment_" + to_string(s));
        
        for (const Line& l: b.getLinesUsingSegment(s)) {                        
            c.addVariable(to_string(l), -1.0);
        }
                
        c.addVariable("dot_" + to_string(s.first), 1.0);
        c.setBound(0.0);
        c.setType(Constraint::GT);            
        addConstraint(c);
    }
    
    // (3) sum of line weights = n
    {
        Constraint c;
        c.setName("startingdots");
        for (const Line& l: b.getLineList()) {
            c.addVariable(to_string(l), 1.0);
        }
        c.setBound(n);
        c.setType(Constraint::EQ);
        addConstraint(c);
    }
        
    //   (4) dot_weight for dots from the cross is equal to 1
    for (const Dot& d: b.getDotList()) {
        if (b.hasDot(d)) {
            Constraint c;
                        
            c.setName("cross_" + to_string(d));
            c.addVariable("dot_" + to_string(d), 1.0);
            c.setBound(1.0);
            c.setType(Constraint::EQ);            
            addConstraint(c);
        }
    }
    
    // symmetric solutions
    if (getFlag("symmetric")) {
        for (const Line&l: b.getLineList()) {
            Line sl = b.centerSymmetry(l);
            
            Constraint c;
            
            c.addVariable(to_string(l), 1.0);
            c.addVariable(to_string(sl), -1.0);
            c.setType(Constraint::EQ);
            c.setBound(0.0);
            c.setName("sym_" + to_string(l));
            
            addConstraint(c);
        }
    }

//.........这里部分代码省略.........
开发者ID:anagorko,项目名称:morpion-lpp,代码行数:101,代码来源:morpion_lpp.cpp

示例2: getLPP

LPP* PotentialLPP::getLPP()
{
    setComment("PotentialLPP " + gameId());
    
    // each move is a structural variable
    
    for (const Move& l: b.getMoveList()) {        
        std::string v_name = to_string(l);

        setVariableBounds(v_name, 0.0, 1.0);

        if (getFlag("exact") && getFlag("dot-acyclic")) {
            setVariableBoolean(v_name, true);
            setVariableOrd(v_name, 0);
        }
    }
    
    // each dot is a structural variable

    for (Dot d: b.getDotList()) {
        std::string v_name = "dot_" + to_string(d);

        if (!b.hasDot(d)) {
            getObjective().push_back(std::pair<std::string, double>(v_name, 1.0)); 
        }
            
        // dots are the only boolean variables        
        if (getFlag("exact")) {
            setVariableBoolean(v_name, true);
            if (b.infeasibleDot(d + Dot(1,0)) ||
                b.infeasibleDot(d + Dot(1,1)) ||
                b.infeasibleDot(d + Dot(0,1)) ||
                b.infeasibleDot(d + Dot(1,-1)) ||
                b.infeasibleDot(d + Dot(0,-1)) ||
                b.infeasibleDot(d + Dot(-1,-1)) ||
                b.infeasibleDot(d + Dot(-1,0)) ||
                b.infeasibleDot(d + Dot(-1,1))) 
            {
                Dot n = d - b.getCRef();
                setVariableOrd(v_name, 1 + abs(n.x) + abs(n.y));
            } else {            
                setVariableOrd(v_name, -1);
            }
        }

        // dots that are placed on board have dot variable equal to 1
        if (b.hasDot(d)) {
            setVariableBounds(v_name, 1.0, 1.0);
        } else {
            setVariableBounds(v_name, 0.0, 1.0);
        }
    }
                
    // constraints:
    //
    //   (1) dot at beginning of segment - moves removing segment starting from dot >= 0
    //
    //   (2) dot == sum of weights of moves that place dot
    //
    //   (3) sum of weights of dots - sum of weights of moves <= 36

    // (1)

    // L4
    
    for (const Segment& s: b.getSegmentList()) {
        Constraint c;
        c.setName("sgm_" + to_string(s));        

        c.addVariable("dot_" + to_string(s.first), 1.0);
        for (const Move& m: b.getMovesRemovingSegment(s)) {
            c.addVariable(to_string(m), -1.0);
        }
        c.setType(Constraint::GT);
        c.setBound(0.0);
        addConstraint(c);        
    }

    // L3
    
    for (const Dot& d: b.getDotList()) {
        if (b.hasDot(d)) continue;
        
        Constraint c;
        c.setName("L3_" + to_string(d));
        c.addVariable("dot_" + to_string(d), 1.0f);
        for (const Move& m: b.getMovesPlacingDot(d)) {
            c.addVariable(to_string(m), -1.0f);
        }
        c.setBound(0.0f);
        c.setType(Constraint::EQ);
        addConstraint(c);
    }
/*          
    // (2)
    
    for (const Dot &d: b.getDotList()) {
        if (b.hasDot(d)) continue;
        
        Constraint c;
//.........这里部分代码省略.........
开发者ID:anagorko,项目名称:morpion-lpp,代码行数:101,代码来源:potential_lpp.cpp


注:本文中的Constraint::addVariable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。