Example #1
0
double solution::euclideanDistance(solution &s2) {
    double sum = 0;

    if(getNumObjectives() != s2.getNumObjectives())
        return nan("");

    int i;
    for(i=0;i<getNumObjectives();i++)
        sum += (getObjective(i) - s2.getObjective(i)) * (getObjective(i) - s2.getObjective(i));

    return sqrt(sum);
}
Example #2
0
bool solution::dominates(solution &s2) {
    if(getNumObjectives() != s2.getNumObjectives())
        return false;

    int i;
    for(i=0;i<getNumObjectives();i++)
        if(getObjective(i) > s2.getObjective(i))
            return false;

    return true;
}
 /* \brief Create the OptimizationProblem from the supplied components and
           return it */
 Teuchos::RCP<OPT> getOptimizationProblem() {
     Teuchos::RCP<V> x = getInitialGuess()->clone();
     x->set(*getInitialGuess());
     return Teuchos::rcp( new OPT ( getObjective(),
                                    x,
                                    getBoundConstraint(),
                                    getEqualityConstraint(),
                                    getEqualityMultiplier(),
                                    getInequalityConstraint(),
                                    getInequalityMultiplier() ) );
 }
Example #4
0
bool solution::operator <(solution &s2) {
    double sum1 = 0.0f, sum2 = 0.0f;

    if(getNumObjectives() != s2.getNumObjectives())
        return false;

    int i;
    for(i=0;i<getNumObjectives();i++) {
        sum1 += getObjective(i);
        sum2 += s2.getObjective(i);
    }

    return sum1 < sum2;
}
Example #5
0
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);
        }
    }

    return this;
}
Example #6
0
LPP* MorpionLPP::getLPP()
{
    setComment(gameId());

    // each possible move is a structural variable
    // our goal is to maximize sum over all variables

    for (const Move& m: b.getMoveList()) {        
        std::string v_name = to_string(m);

        setVariableBounds(v_name, 0.0, 1.0);
        getObjective().push_back(std::pair<std::string, double>(v_name, 1.0));
        
        // Constraint for exact problems.
        //
        //   mv_ variables are either 0 or 1 (i.e. the move is either played or not)

        if (getFlag("exact") || getFlag("binary_moves")) {
            setVariableBoolean(v_name, true);
        }
    }

    // each segment gives us constraints:
    // (1) if the segment is placed in the starting position
    //    (a) sum of weights of moves that place this segment <= 0
    //    (b) 1 - sum of weights of moves that remove this segment >= 0
    //        i.e. sum of weights of moves that remove this segment <= 1
    // (2) if the segment is not placed in the starting position
    //    (a) sum of weights of moves that place this segment <= 1
    //    (b) sum of weights of moves that place this segment
    //          - sum of weights of moves that remove this segment >= 0
            
    for (const Segment& s: b.getSegmentList()) {
        int x = s.first.x;
        int y = s.first.y;
        int d = s.second;

        std::string constr_name = "segment_" + to_string(x) + "_" + to_string(y) + "_" + to_string(d);

        if (b.hasDot(Dot(x,y))) {
            // (1a)
            {
                Constraint constr;
        
                for (Move& pl: b.getMovesPlacingSegment(s)) {
                    constr.addVariable(to_string(pl), 1.0);
                }
                constr.setName("r1a_" + constr_name);
                constr.setType(Constraint::LT);
                constr.setBound(0.0);
                addConstraint(constr);
            }
            // (1b)
            {
                Constraint constr;
        
                for (Move& rm: b.getMovesRemovingSegment(s)) {
                    constr.addVariable(to_string(rm), 1.0);
                }
                constr.setName("r1b_" + constr_name);
                constr.setType(Constraint::LT);
                constr.setBound(1.0);
                addConstraint(constr);
            }                        
        } else {
            // (2a)
            {
                Constraint constr;
        
                for (Move& pl: b.getMovesPlacingSegment(s)) {
                    constr.addVariable(to_string(pl), 1.0);
                }
                constr.setName("r2a_" + constr_name);
                constr.setType(Constraint::LT);
                constr.setBound(1.0);
                addConstraint(constr);
            }
            // (2b)
            {
                Constraint constr;

                for (Move& pl: b.getMovesPlacingSegment(s)) {
                    constr.addVariable(to_string(pl), 1.0);
                }
                for (Move& rm: b.getMovesRemovingSegment(s)) {
                    constr.addVariable(to_string(rm), -1.0);
                }
                constr.setName("r2b_" + constr_name);
                constr.setType(Constraint::GT);
                constr.setBound(0.0);      
                    
                addConstraint(constr);          
            }
        }        
    }

    // EXTRA CONSTRAINTS:
    //
    // for each move m:
    //   for each dot d required by m:
    //     for each move n placing d that is consistent with m:
    //       weight_m <= sum of weights of n's

    if (getFlag("extra")) {
        for (const Move& m: b.getMoveList()) {
            std::string constr_name = "_" + to_string(m);
        
            for (const Dot& d: m.requiredDots(b.getVariant())) {
                if (b.hasDot(d)) continue;
                
                Constraint constr;
                
                for (const Move& n: b.getMovesPlacingSegment(Segment(d,0))) {
                    if (m.consistentWith(n,b.getVariant())) {
                        constr.addVariable(to_string(n), -1.0);
                    }
                }
                constr.addVariable(to_string(m), 1.0);
                
                constr.setName("extra_" + to_string(d) + constr_name);
                constr.setType(Constraint::LT);
                constr.setBound(0.0);
                
                addConstraint(constr);
            }
        }
    }
    
    // Disallow cycles of length 3 from the solution.
    // Improves fuzzy solutions
    
    if (getFlag("short-cycles")) {         
        for (const Segment& s: b.getSegmentList()) {
        
            int d = s.second;
            
            if (d % 2 == 1) continue;
            
            addConstraints(createCycleConstraints(s, d + 2, d + 5));
            addConstraints(createCycleConstraints(s, d + 6, d + 1));                
        }            
    }
    
    // Constraints for acyclic problems (explanation makes sense for exact problems):
    //
    // OBSOLETE
    //
    // 1. Each move mv_* has accompanying order variable order_*
    // 2. Move that is not picked has always order 0, 
    //    move that is picked has order between 1 and 675
    // 
    // It is accomplished with the condition:
    //      mv_ <= order_ <= 675 * mv_
    //
    // 3. Assume that mv_1 removes segment s and mv_2 places segment s (mv_1 != mv_2). 
    //    Then
    //      order_1 + (1 - mv_1) * 1000 >= order_2 + 1
    //
    //    The (1-mv_1)*1000 term assures that the condition is not enforced
    //      for moves that are not picked (i.e. have mv_1 = 0)
            
    if (getFlag("acyclic")) {
        throw std::string("not implemented");
    }

    // Constraints for symmetric problems.
    //
    // Moves that are symmetric by central symmetry
    //   with center at ref + (1.5,1.5) have to have equal weights
    
    if (getFlag("symmetric")) {
        for (const Move&m: b.getMoveList()) {
            Move sm = b.centerSymmetry(m);
            
            Constraint c;
            
            c.addVariable(to_string(m), 1.0);
            c.addVariable(to_string(sm), -1.0);
            c.setType(Constraint::EQ);
            c.setBound(0.0);
            c.setName("sym_" + to_string(m));
            
            addConstraint(c);
        }
    }

    // dot-acyclic - better implementation of acyclic problems

    // dots that are not placed have dot_ variable equal to 0
    // this is important for --hull option
    // use this not only for dot-acyclic problems
    
    for (const Dot& d: b.getDotList())
    {
        if (b.hasDot(d)) continue;
        
        Constraint c;
        
        c.setName("dotmove_" + to_string(d));
        
        c.addVariable("dot_" + to_string(d), 1.0);
        
        for (const Move& m: b.getMovesPlacingDot(d)) {
            c.addVariable(to_string(m), -b.bound());
        }
        c.setType(Constraint::LT);
        c.setBound(0.0);
        addConstraint(c);                    
    }
    
    if (getFlag("dot-acyclic")) {
        for (int x = 0; x < b.getWidth(); x++) {
            for (int y= 0; y < b.getHeight(); y++) {
                if (b.infeasibleDot(Dot(x,y)) || b.hasDot(Dot(x,y))) continue;
                
                setVariableBounds("dot_" + to_string(Dot(x,y)), 0, b.bound());

                if (getFlag("symmetric")) {
                    Constraint c;
                    
                    if (b.infeasibleDot(b.centerSymmetry(Dot(x,y)))) continue;
                    
                    c.setName("dotsym_");
                    c.addVariable("dot_" + to_string(Dot(x,y)), 1.0);
                    c.addVariable("dot_" + to_string(b.centerSymmetry(Dot(x,y))), -1.0);
                    c.setType(Constraint::EQ);
                    c.setBound(0.0);
                    addConstraint(c);
                }                
            }
        }
        
        for (const Move& m: b.getMoveList()) {
            for (const Dot& rq: m.requiredDots(b.getVariant())) {
                if (b.hasDot(rq)) continue;
                
                Constraint c;
                
                // dot_placed >= dot_required + 1 - (1 - m) * bound
                // i.e. dot_placed - dot_required - bound * m >= 1 - bound
                
                c.setName("dot_");
                c.addVariable("dot_" + to_string(m.placedDot()), 1.0);
                c.addVariable("dot_" + to_string(rq), -1.0);
                c.addVariable(to_string(m), -b.bound());
                c.setBound(1 - b.bound());
                c.setType(Constraint::GT);
                
                addConstraint(c);
            }
        }      
    }

    // Constraints that enforce that the board (if it is rectangle) is 
    // the convex hull of the solution
    if (getFlag("rhull")) {
        // right side
        addConstraint(getSideConstraint(1,0,true));        
        // left side
        addConstraint(getSideConstraint(1,0,false));
        // top side
        addConstraint(getSideConstraint(0,1,true));
        // bottom side
        addConstraint(getSideConstraint(0,1,false));        
    }
    
    // Constraints that enforce that the board (if it is octagon) is 
    // the convex hull of the solution

    if (getFlag("hull")) {
        // right side
        addConstraint(getSideConstraint(1,0,true));        
        // left side
        addConstraint(getSideConstraint(1,0,false));
        // lower right side
        addConstraint(getSideConstraint(1,-1,false));
        // upper left side
        addConstraint(getSideConstraint(1,-1,true));
        // top side
        addConstraint(getSideConstraint(0,1,true));
        // bottom side
        addConstraint(getSideConstraint(0,1,false));
        // lower left side
        addConstraint(getSideConstraint(1,1,false));
        // upper right side
        addConstraint(getSideConstraint(1,1,true));
    }
            
    return this;
}
Example #7
0
LPP* PlusPlusLPP::getLPP()
{
    setComment("++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);
        getObjective().push_back(std::pair<std::string, double>(v_name, 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);
  
        // 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);
            
            setVariableOrd(v_name, 1000-abs(b.getCRef().x - d.x) - abs(b.getCRef().y - d.y));
        }
    }
    
    // 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 dot weights = 36 + sum of line weights
    //
    //   (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 dot weights <= 36 + sum of line weights
    {
        Constraint c;
        c.setName("startingdots");
        for (const Dot& d: b.getDotList()) {
            c.addVariable("dot_" + to_string(d), 1.0);
        }
        for (const Line& l: b.getLineList()) {
            c.addVariable(to_string(l), -1.0);
        }
        c.setBound(36.0);
        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);
        }

        for (const Dot&d: b.getDotList()) {
            Dot sd = b.centerSymmetry(d);
            
            Constraint c;
            
            c.addVariable("dot_"+to_string(d), 1.0);
            c.addVariable("dot_"+to_string(sd), -1.0);
            c.setType(Constraint::EQ);
            c.setBound(0.0);
            c.setName("dotsym_" + to_string(d));
            
            addConstraint(c);
        }
    }

    // exact hull
    if (getFlag("hull")) {
        // right side
        addConstraint(getSideConstraint(1,0,true));        
        // left side
        addConstraint(getSideConstraint(1,0,false));
        // lower right side
        addConstraint(getSideConstraint(1,-1,false));
        // upper left side
        addConstraint(getSideConstraint(1,-1,true));
        // top side
        addConstraint(getSideConstraint(0,1,true));
        // bottom side
        addConstraint(getSideConstraint(0,1,false));
        // lower left side
        addConstraint(getSideConstraint(1,1,false));
        // upper right side
        addConstraint(getSideConstraint(1,1,true));
    }

    // dots that are not placed have dot_ variable equal to 0
    // this is important for --hull option
    // and possibly for other reasons as well
    
    for (const Dot& d: b.getDotList())
    {
        Constraint c;
        
        c.setName("dotline_" + to_string(d));
        
        c.addVariable("dot_" + to_string(d), 1.0);
        
        for (const Line& l: b.getLinesUsingDot(d)) {
            c.addVariable(to_string(l), -1.0);
        }
        c.setType(Constraint::LT);
        c.setBound(0.0);
        addConstraint(c);                    
    }

    // extra constraints that remove small gaps from the solution
    // there is always an optimal solution without 1-segment gaps

    if (getFlag("extra")) {
        for (const Line& l: b.getLineList()) {
        {
            Constraint c;
            c.setName("smallgaps1_" + to_string(l));
            c.addVariable(to_string(l), 1.0);
            
            Line ol;
            
            if (b.getVariant() == T5) {
                ol = Line(l.st + direction[l.dir]*5, l.dir); if (b.validLine(ol)) { c.addVariable(to_string(ol), 1.0); }
            } else {
                ol = Line(l.st + direction[l.dir]*6, l.dir); if (b.validLine(ol)) { c.addVariable(to_string(ol), 1.0); }
            }
            
            c.setType(Constraint::LT);
            c.setBound(1.0);
            addConstraint(c);
        }
        {
            Constraint c;
            c.setName("smallgaps2_" + to_string(l));
            c.addVariable(to_string(l), 1.0);
            
            Line ol;
            
            if (b.getVariant() == T5) {
                ol = Line(l.st + direction[l.dir]*(-5), l.dir); if (b.validLine(ol)) { c.addVariable(to_string(ol), 1.0); }
            } else {
                ol = Line(l.st + direction[l.dir]*(-6), l.dir); if (b.validLine(ol)) { c.addVariable(to_string(ol), 1.0); }
            }
            
            c.setType(Constraint::LT);
            c.setBound(1.0);
            addConstraint(c);
        }


        }
    }
    
    return this;
}
Example #8
0
/** problem reading method of reader */
static
SCIP_DECL_READERREAD(readerReadCip)
{  /*lint --e{715}*/

   CIPINPUT cipinput;
   SCIP_Real objscale;
   SCIP_Real objoffset;
   SCIP_Bool initialconss;
   SCIP_Bool dynamicconss;
   SCIP_Bool dynamiccols;
   SCIP_Bool dynamicrows;
   SCIP_Bool initialvar;
   SCIP_Bool removablevar;
   SCIP_RETCODE retcode;

   if( NULL == (cipinput.file = SCIPfopen(filename, "r")) )
   {
      SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
      SCIPprintSysError(filename);
      return SCIP_NOFILE;
   }

   cipinput.len = 131071;
   SCIP_CALL( SCIPallocBufferArray(scip, &(cipinput.strbuf), cipinput.len) );

   cipinput.linenumber = 0;
   cipinput.section = CIP_START;
   cipinput.haserror = FALSE;
   cipinput.endfile = FALSE;
   cipinput.readingsize = 65535;

   SCIP_CALL( SCIPcreateProb(scip, filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );

   SCIP_CALL( SCIPgetBoolParam(scip, "reading/initialconss", &initialconss) );
   SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamiccols", &dynamiccols) );
   SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicconss", &dynamicconss) );
   SCIP_CALL( SCIPgetBoolParam(scip, "reading/dynamicrows", &dynamicrows) );

   initialvar = !dynamiccols;
   removablevar = dynamiccols;

   objscale = 1.0;
   objoffset = 0.0;

   while( cipinput.section != CIP_END && !cipinput.haserror )
   {
      /* get next input string */
      SCIP_CALL( getInputString(scip, &cipinput) );

      if( cipinput.endfile )
         break;

      switch( cipinput.section )
      {
      case CIP_START:
         getStart(scip, &cipinput);
         break;
      case CIP_STATISTIC:
         SCIP_CALL( getStatistics(scip, &cipinput) );
         break;
      case CIP_OBJECTIVE:
         SCIP_CALL( getObjective(scip, &cipinput, &objscale, &objoffset) );
         break;
      case CIP_VARS:
         retcode = getVariable(scip, &cipinput, initialvar, removablevar, objscale);

         if( retcode == SCIP_READERROR )
         {
            cipinput.haserror = TRUE;
            goto TERMINATE;
         }
         SCIP_CALL(retcode);

         break;
      case CIP_FIXEDVARS:
         retcode = getFixedVariable(scip, &cipinput);

         if( retcode == SCIP_READERROR )
         {
            cipinput.haserror = TRUE;
            goto TERMINATE;
         }
         SCIP_CALL(retcode);

         break;
      case CIP_CONSTRAINTS:
         retcode = getConstraint(scip, &cipinput, initialconss, dynamicconss, dynamicrows);

         if( retcode == SCIP_READERROR )
         {
            cipinput.haserror = TRUE;
            goto TERMINATE;
         }
         SCIP_CALL(retcode);

         break;
      default:
         SCIPerrorMessage("invalid CIP state\n");
         SCIPABORT();
         return SCIP_INVALIDDATA;  /*lint !e527*/
      } /*lint !e788*/ 
   }

   if( !SCIPisZero(scip, objoffset) && !cipinput.haserror )
   {
      SCIP_VAR* objoffsetvar;

      objoffset *= objscale;
      SCIP_CALL( SCIPcreateVar(scip, &objoffsetvar, "objoffset", objoffset, objoffset, 1.0, SCIP_VARTYPE_CONTINUOUS,
         TRUE, TRUE, NULL, NULL, NULL, NULL, NULL) );
      SCIP_CALL( SCIPaddVar(scip, objoffsetvar) );
      SCIP_CALL( SCIPreleaseVar(scip, &objoffsetvar) );
      SCIPdebugMessage("added variables <objoffset> for objective offset of <%g>\n", objoffset);
   }

   if( cipinput.section != CIP_END && !cipinput.haserror )
   {
      SCIPerrorMessage("unexpected EOF\n");
   }

 TERMINATE:
   /* close file stream */
   SCIPfclose(cipinput.file);

   SCIPfreeBufferArray(scip, &cipinput.strbuf);

   if( cipinput.haserror )
      return SCIP_READERROR;

   /* successfully parsed cip format */
   *result = SCIP_SUCCESS;
   return SCIP_OKAY;
}
void pp_get_corrected_vals(char *sarName, double *corrected_earth_radius,
                           double *corrected_azimuth_time_per_pixel)
{
    int status;
    int iter = 0, max_iter = 100;
    const gsl_root_fsolver_type *T;
    gsl_root_fsolver *s;
    gsl_function F;
    gsl_error_handler_t *prev;
    struct pp_erfin_params params;

    double nominal_pixsize_azimuth;
    double nadir_radius; /* earth radius at nadir */
    double pp_earth_radius; /* f'd up PP Earth radius from start of swath */
    double seconds_per_azimuth_line; /* PP's real azimuth resolution */
    double lo, hi;  /* starting points for the bisection algorithm */
    struct VFDRECV facdr;

    get_asf_facdr(sarName, &facdr);

    /* Find the PP's earth radius with an iterative search */
    F.function = &getObjective;
    F.params = &params;

    params.npixels=facdr.npixels;
    params.nlines=facdr.nlines;
    params.nominal_pixsize_range=facdr.rapixspc;
    nominal_pixsize_azimuth=facdr.azpixspc;
    nadir_radius=1.0e3*facdr.eradnadr;
    params.satellite_height=1.0e3*(facdr.scalt+facdr.eradnadr);
    params.slant_first=1.0e3*facdr.sltrngfp;
    params.slant_last=1.0e3*facdr.sltrnglp;

    prev = gsl_set_error_handler_off();

    lo = nadir_radius - 2000;
    hi = nadir_radius + 2000;

    T = gsl_root_fsolver_brent;
    s = gsl_root_fsolver_alloc (T);
    gsl_root_fsolver_set (s, &F, lo, hi);

    do {
        ++iter;
        status = gsl_root_fsolver_iterate(s);
        pp_earth_radius = gsl_root_fsolver_root(s);
        status = gsl_root_test_residual(
            getObjective(pp_earth_radius, (void*)&params), 1.0e-4);
    } while (status == GSL_CONTINUE && iter < max_iter);

    if (status == GSL_SUCCESS) {
        //printf("Converged after %d iterations.\n", iter);
        //printf("PP Earth Radius: %.3f m\n",pp_earth_radius);
        //printf("   (for comparison) Nadir Earth Radius: %.3f m\n",nadir_radius);
        *corrected_earth_radius = pp_earth_radius;
    } else {
        asfPrintWarning("Failed to determine PP earth radius!\n"
                        "iter: %d, pp_earth_radius=%.3f, res=%.5f\n"
                        "Proceeding using the nadir radius: %.3f m\n"
                        "Starting points were: lo: %.3f -> %.4f\n"
                        "                      hi: %.3f -> %.4f\n",
                        iter, pp_earth_radius,
                        getObjective(pp_earth_radius, (void*)&params),
                        nadir_radius,
                        lo, getObjective(lo, (void*)&params),
                        hi, getObjective(hi, (void*)&params));
        *corrected_earth_radius = nadir_radius;
    }

    gsl_set_error_handler(prev);

    // Find the PP's per-second azimuth pixel spacing
    seconds_per_azimuth_line=nominal_pixsize_azimuth/facdr.swathvel;
    //printf("PP seconds per azimuth line: %.9f s/line\n",seconds_per_azimuth_line);
    //printf("   (for comparison) PP interpolated lines per second: %.3f lines/s\n",1.0/seconds_per_azimuth_line);
    //printf("   (for comparison) FACDR swath velocity: %.3f m/s\n",facdr.swathvel);

    //double R=pp_earth_radius;
    //double H=params.satellite_height;
    //double HHRR=H*H + R*R;
    //double slant=0.5*(params.slant_first+params.slant_last);
    //double rg_center=R*acos( (HHRR - slant*slant) / (2*H*R));
    //double vs=sqrt(facdr.scxvel*facdr.scxvel + facdr.scyvel*facdr.scyvel + facdr.sczvel*facdr.sczvel);
    //double vsg = vs * pp_earth_radius/params.satellite_height*cos(rg_center/pp_earth_radius);

    //printf("   (for comparison) PP-style recalc velocity: %.3f m/s\n",vsg);

    *corrected_azimuth_time_per_pixel = seconds_per_azimuth_line;

    // Free the solver
    gsl_root_fsolver_free(s);
}
Example #10
0
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;
        c.setName("dotmv_" + to_string(d));
        
        for (const Move &m: b.getMovesPlacingDot(d)) {
            c.addVariable(to_string(m), -1.0);
        }
        c.addVariable("dot_" + to_string(d), 1.0);
        c.setBound(0.0);
        c.setType(Constraint::EQ);
        addConstraint(c);
    }
*/

/*    
    // (3)

    {
    Constraint c;
    c.setName("dots_moves");
    
    for (const Dot &d: b.getDotList()) {
        c.addVariable("dot_"+to_string(d), 1.0);
    }    
    for (const Move &m: b.getMoveList()) {
        c.addVariable(to_string(m), -1.0);
    }
    c.setBound(36.0);
    c.setType(Constraint::LT);
    addConstraint(c);
    
    }
*/

    // dots that are not placed have dot_ variable equal to 0
    // this is important for --hull option
    // use this not only for dot-acyclic problems
/*    
    for (const Dot& d: b.getDotList())
    {
        if (b.hasDot(d)) continue;
        
        Constraint c;
        
        c.setName("dotmove_" + to_string(d));
        
        c.addVariable("dot_" + to_string(d), 1.0);
        
        for (const Move& m: b.getMovesPlacingDot(d)) {
            c.addVariable(to_string(m), -b.bound());
        }
        c.setType(Constraint::LT);
        c.setBound(0.0);
        addConstraint(c);                    
    }
*/
    // Constraints that enforce that the board (if it is an octagon) is 
    // the convex hull of the solution

    if (getFlag("hull")) {
        // right side
        addConstraint(getSideConstraint(1,0,true));        
        // left side
        addConstraint(getSideConstraint(1,0,false));
        // lower right side
        addConstraint(getSideConstraint(1,-1,false));
        // upper left side
        addConstraint(getSideConstraint(1,-1,true));
        // top side
        addConstraint(getSideConstraint(0,1,true));
        // bottom side
        addConstraint(getSideConstraint(0,1,false));
        // lower left side
        addConstraint(getSideConstraint(1,1,false));
        // upper right side
        addConstraint(getSideConstraint(1,1,true));
    }

    if (getFlag("rhull")) {
        // right side
        addConstraint(getSideConstraint(1,0,true));        
        // left side
        addConstraint(getSideConstraint(1,0,false));
        // top side
        addConstraint(getSideConstraint(0,1,true));
        // bottom side
        addConstraint(getSideConstraint(0,1,false));
    }

    if (getFlag("rside")) {
        // right side
        addConstraint(getSideConstraint(1,0,true));        
    }
    
    if (getFlag("symmetric")) {
        for (const Move&m: b.getMoveList()) {
            Move sm = b.centerSymmetry(m);
            
            Constraint c;
            
            c.addVariable(to_string(m), 1.0);
            c.addVariable(to_string(sm), -1.0);
            c.setType(Constraint::EQ);
            c.setBound(0.0);
            c.setName("sym_" + to_string(m));
            
            addConstraint(c);
        }

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

            Dot sd = b.centerSymmetry(d);
            
            if (b.hasDot(d) || b.hasDot(sd)) {
                continue;
            }
            
            Constraint c;
            
            c.addVariable("dot_" + to_string(d), 1.0);
            c.addVariable("dot_" + to_string(sd), -1.0);
            c.setType(Constraint::EQ);
            c.setBound(0.0);
            c.setName("symdot_" + to_string(d));
            
            addConstraint(c);
        }
    }
    
    // create only acyclic solutions
    
    if (getFlag("dot-acyclic")) {
        for (int x = 0; x < b.getWidth(); x++) {
            for (int y= 0; y < b.getHeight(); y++) {
                if (b.infeasibleDot(Dot(x,y)) || b.hasDot(Dot(x,y))) continue;
                
                setVariableBounds("ord_" + to_string(Dot(x,y)), 0, b.bound());

                if (getFlag("symmetric")) {
                    Constraint c;
                    
                    if (b.infeasibleDot(b.centerSymmetry(Dot(x,y)))) continue;
                    
                    c.setName("ordsym_"+to_string(Dot(x,y)));
                    c.addVariable("ord_" + to_string(Dot(x,y)), 1.0);
                    c.addVariable("ord_" + to_string(b.centerSymmetry(Dot(x,y))), -1.0);
                    c.setType(Constraint::EQ);
                    c.setBound(0.0);
                    addConstraint(c);
                }                
            }
        }
        
        for (const Move& m: b.getMoveList()) {
            for (const Dot& rq: m.requiredDots(b.getVariant())) {
                if (b.hasDot(rq)) continue;
                
                Constraint c;

                // dot_placed >= dot_required + 1 - (1 - m) * bound
                // i.e. dot_placed - dot_required - bound * m >= 1 - bound
                
                c.setName("ord_" + to_string(m) + "_" + to_string(rq));
                c.addVariable("ord_" + to_string(m.placedDot()), 1.0);
                c.addVariable("ord_" + to_string(rq), -1.0);
                c.addVariable(to_string(m), -b.bound());
                c.setBound(1 - b.bound());
                c.setType(Constraint::GT);
             
                addConstraint(c);
            }
        }      
    }

/*
    enum { R = 0, D = 2, L = 4, U = 6 };
    
    std::vector<int> inside_path = { R, U, U, R, D, D, D, R, R, R, D, L, L, L,
                                     D, D, D, L, U, U, U, L, L, L, U, R, R, R };
                                
    Dot r = b.getReference();
    for (auto & d: inside_path) {
        r = r + direction[d];
        
        Constraint c;
        c.setName("inside_" + to_string(r));
        c.addVariable("dot_" + to_string(r), 1.0f);
        c.setBound(1.0f);
        c.setType(Constraint::EQ);
        addConstraint(c);
    }
*/
    
    return this;
}