コード例 #1
0
ファイル: tb2arithmetic.cpp プロジェクト: eomahony/Numberjack
void Unary::propagate()
{
    if (ToulBar2::verbose >= 3) {
        print(cout);
        cout << " dxinf=" << deltaValueXinf << " dxsup=" << deltaValueXsup << endl;
    }
    wcsp->revise(this);
    set<Value>::iterator itinf = permitted.lower_bound(x->getInf());
    set<Value>::iterator itsup = permitted.upper_bound(x->getSup());
    --itsup;
    if (itinf == permitted.end() || itsup == permitted.end()) {
        // IC0 propagatation (increase global lower bound)
        deconnect();
        projectLB(penalty);
    } else {
        // propagate hard constraint
        if (CUT(wcsp->getLb()+penalty, wcsp->getUb())) {
            if (x->getInf() < *itinf) x->increase(*itinf);
            if (x->getSup() > *itsup) x->decrease(*itsup);
        }
        // BAC* propagation (increase unary costs of domain bounds)
        Value xinf = x->getInf();
        if (xinf != deltaValueXinf && xinf != deltaValueXsup && permitted.find(xinf) == permitted.end()) {
            deltaValueXinf = xinf;
            x->projectInfCost(penalty);
        }
        Value xsup = x->getSup();
        if (xsup != deltaValueXinf && xsup != deltaValueXsup && permitted.find(xsup) == permitted.end()) {
            deltaValueXsup = xsup;
            x->projectSupCost(penalty);
        }
    }
}
コード例 #2
0
 void assign(int varIndex)
 {
     assert(connected());
     deconnect(); // Warning! deconnection has to be done before the projection
     if (permitted.find(x->getValue()) == permitted.end()) {
         projectLB(penalty);
     }
 }
コード例 #3
0
void AllDiffConstraint::decompose() {
	deconnect();
	for (int i=0;i<arity_;i++) {
		for (int j=i+1;j<arity_;j++) {
			EnumeratedVariable* x = (EnumeratedVariable*)getVar(i);
			EnumeratedVariable* y = (EnumeratedVariable*)getVar(j);
			vector<Cost> costs;
			for (unsigned int a = 0; a < x->getDomainInitSize(); a++) {
				for (unsigned int b = 0; b < y->getDomainInitSize(); b++) {
					if (a == b) {
						costs.push_back(def);
					} else {
						costs.push_back(0);
					}
				}
			}
			if(ToulBar2::vac) {
				for (unsigned int a = 0; a < x->getDomainInitSize(); a++) {
					for (unsigned int b = 0; b < y->getDomainInitSize(); b++) {
						Cost c = costs[a * y->getDomainInitSize() + b];
						wcsp->histogram(c);
					}
				}               	
			}
			BinaryConstraint* ctr = x->getConstr(y);   		
			if(ctr)	{
				ctr->reconnect();
				ctr->addCosts(x,y,costs);
				ctr->propagate();
			}
			else {
				if (!ToulBar2::vac) {
					ctr = new BinaryConstraint(wcsp, x, y, costs, &wcsp->getStore()->storeCost);
				} else {
					ctr = new VACBinaryConstraint(wcsp, x, y, costs, &wcsp->getStore()->storeCost);
				}
			}
		}
	}
}
コード例 #4
0
 void deconnect(bool reuse = false) {
     if (connected()) {
         if (ToulBar2::verbose >= 3) cout << "deconnect " << this << endl;
         for(int i=0;i<arity_;i++) deconnect(i, reuse);
     }
 }
コード例 #5
0
 void assign(int varIndex)
 {
     if (x->assigned() && y->assigned())
         deconnect();
     propagate();
 }
コード例 #6
0
 void assign(int varIndex)
 {
     assert(connected());
     wcsp->revise(this);
     if (x->assigned() && y->assigned()) {
         deconnect();
         if (x->getValue() >= xinfty && y->getValue() >= yinfty && costx + costy > deltaCost) {
             projectLB(costx + costy - deltaCost);
         } else if (x->getValue() >= xinfty && y->getValue() < yinfty && costx > deltaCost) {
             projectLB(costx - deltaCost);
         } else if (y->getValue() >= yinfty && x->getValue() < xinfty && costy > deltaCost) {
             projectLB(costy - deltaCost);
         } else if (x->getValue() < xinfty && y->getValue() < yinfty && x->getValue() < y->getValue() + csty && y->getValue() < x->getValue() + cstx) {
             THROWCONTRADICTION;
         }
     } else {
         if (varIndex == 0 && x->getValue() >= xinfty) {
             if (y->getInf() == deltaValueYinf) {
                 Cost cost = deltaCostYinf;
                 deltaCostYinf = MIN_COST;
                 y->projectInfCost(-cost);
             }
             if (y->getSup() == deltaValueYsup) {
                 Cost cost = deltaCostYsup;
                 deltaCostYsup = MIN_COST;
                 y->projectSupCost(-cost);
             }
             Cost cost = costx - deltaCost;
             if (cost > MIN_COST) {
                 deltaCost += cost;
                 projectLB(cost);
             }
             if (y->getSup() < yinfty) {
                 deconnect();
             } else {
                 assert(y->getSup() == yinfty);
                 deltaValueYsup = yinfty;
                 deltaCostYsup = costy;
                 y->projectSupCost(costy);
             }
         } else if (varIndex == 1 && y->getValue() >= yinfty) {
             if (x->getInf() == deltaValueXinf) {
                 Cost cost = deltaCostXinf;
                 deltaCostXinf = MIN_COST;
                 x->projectInfCost(-cost);
             }
             if (x->getSup() == deltaValueXsup) {
                 Cost cost = deltaCostXsup;
                 deltaCostXsup = MIN_COST;
                 x->projectSupCost(-cost);
             }
             Cost cost = costy - deltaCost;
             if (cost > MIN_COST) {
                 deltaCost += cost;
                 projectLB(cost);
             }
             if (x->getSup() < xinfty) {
                 deconnect();
             } else {
                 assert(x->getSup() == xinfty);
                 deltaValueXsup = xinfty;
                 deltaCostXsup = costx;
                 x->projectSupCost(costx);
             }
         } else {
             propagate();
         }
     }
 }
コード例 #7
0
ファイル: SSH2Utils.cpp プロジェクト: tasosbull/yewtic
int SSH2Utils::connect(const char *hostname, const char *username,
		const char *password) {
    unsigned long hostaddr;
    int rc, sock, auth_pw = 0;
    struct sockaddr_in sin;
    const char *fingerprint;
    char *userauthlist;
    LIBSSH2_SESSION *session;

    _password = password;
    hostaddr = inet_addr(hostname);

    /* Ultra basic "connect to port 22 on localhost".  Your code is
     * responsible for creating the socket establishing the connection
     */
    sock = socket(AF_INET, SOCK_STREAM, 0);

    sin.sin_family = AF_INET;
    sin.sin_port = htons(22);
    sin.sin_addr.s_addr = hostaddr;
    if (::connect(sock, (struct sockaddr*)(&sin),
                sizeof(struct sockaddr_in)) != 0) {
        fprintf(stderr, "failed to connect!\n");
        m_errCode = 2;
        return -1;
    }

	/* Create a session instance */
    session = libssh2_session_init();
	if (!session) {
		m_errCode = 3;
		return -1;
	}

	/* tell libssh2 we want it all done non-blocking */
	//libssh2_session_set_blocking(session, 0);
	/* ... start it up. This will trade welcome banners, exchange keys,
	 * and setup crypto, compression, and MAC layers
	 */
	while ((rc = libssh2_session_startup(session, sock))
			== LIBSSH2_ERROR_EAGAIN)
		;
	if (rc) {
		fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
		m_errCode = 4;
		return -1;
	}

    /* At this point we havn't authenticated. The first thing to do is check
     * the hostkey's fingerprint against our known hosts Your app may have it
     * hard coded, may go to a file, may present it to the user, that's your
     * call
     */
    fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
    printf("Fingerprint: ");
    for(int i = 0; i < 20; i++) {
        printf("%02X ", (unsigned char)fingerprint[i]);
    }
    printf("\n");

    /* check what authentication methods are available */
    userauthlist = libssh2_userauth_list(session, username, strlen(username));
    printf("Authentication methods: %s\n", userauthlist);
    if (strstr(userauthlist, "password") != NULL) {
        auth_pw |= 1;
    }
    if (strstr(userauthlist, "keyboard-interactive") != NULL) {
        auth_pw |= 2;
    }
    if (strstr(userauthlist, "publickey") != NULL) {
        auth_pw |= 4;
    }

    if (auth_pw & 1) {
        /* We could authenticate via password */
        if (libssh2_userauth_password(session, username, password)) {
            printf("\tAuthentication by password failed!\n");
            m_errCode = 5;
            goto shutdown;
        } else {
            printf("\tAuthentication by password succeeded.\n");
        }
    } else if (auth_pw & 2) {
        /* Or via keyboard-interactive */
        if (libssh2_userauth_keyboard_interactive(session, username,
                                                  &SSH2Utils::kbd_callback) ) {
            printf("\tAuthentication by keyboard-interactive failed!\n");
            m_errCode = 6;
            goto shutdown;
        } else {
            printf("\tAuthentication by keyboard-interactive succeeded.\n");
        }
    } else if (auth_pw & 4) {
        /* Or by public key */
        if (libssh2_userauth_publickey_fromfile(session, username, keyfile1,
                                                keyfile2, password)) {
            printf("\tAuthentication by public key failed!\n");
            m_errCode = 7;
            goto shutdown;
        } else {
            printf("\tAuthentication by public key succeeded.\n");
        }
    } else {
        printf("No supported authentication methods found!\n");
        m_errCode = 8;
        goto shutdown;
    }

    m_sock = sock;
    m_session = session;

    return 0;

shutdown:
	return -1;
    deconnect();
}
コード例 #8
0
ファイル: SSH2Utils.cpp プロジェクト: tasosbull/yewtic
int SSH2Utils::reconnect(const char *hostname, const char *username, const char *password) {
	deconnect();
	return connect(hostname, username, password);
}
コード例 #9
0
ファイル: tb2arithmetic.cpp プロジェクト: eomahony/Numberjack
void SpecialDisjunction::propagate()
{
    if (ToulBar2::verbose >= 3) {
        print(cout);
        cout << "deltaCost= " << deltaCost << " dxinf=" << deltaValueXinf << " dxsup=" << deltaValueXsup << " dyinf=" << deltaValueYinf << " dysup=" << deltaValueYsup << endl;
    }
    wcsp->revise(this);
    if (x->getSup()>xinfty) x->decrease(xinfty);
    if (y->getSup()>yinfty) y->decrease(yinfty);
    if ((x->getSup() < xinfty && y->getSup() < yinfty &&
            (x->getInf() >= y->getSup() + csty || y->getInf() >= x->getSup() + cstx)) ||
            (x->getInf() >= xinfty && y->getSup() < yinfty) ||
            (y->getInf() >= yinfty && x->getSup() < xinfty)) {
        // deconnect the constraint if always satisfied
        assert(x->getInf() < xinfty || y->getSup() >= yinfty || deltaCost == costx);
        assert(y->getInf() < yinfty || x->getSup() >= xinfty || deltaCost == costy);
        deconnect();
    } else if (x->getSup() < xinfty && y->getSup() < yinfty &&
            x->getSup() < y->getInf() + csty && y->getSup() < x->getInf() + cstx) {
        // backtrack if the constraint if always unsatisfied
        THROWCONTRADICTION;
    } else {
        if (x->getInf() < xinfty && y->getInf() < yinfty) {
            // IC0 propagatation (increase global lower bound)
            if (min(x->getSup(),xinfty-1) < y->getInf() + csty &&
                    min(y->getSup(),yinfty-1) < x->getInf() + cstx) {
                Cost cost = min(costx,costy) - deltaCost;
                if (cost > MIN_COST) {
                    deltaCost += cost;
                    projectLB(cost);
                }
            }
            // propagate hard constraint
            if ((x->getSup() < xinfty || CUT(wcsp->getLb()+costx-deltaCost, wcsp->getUb())) &&
                    min(x->getSup(),xinfty-1) < y->getInf() + csty) {
                Value newInf = min(x->getInf() + cstx, yinfty);
                if (y->getInf() < newInf) y->increase(newInf);
                if (y->getSup() < yinfty) {
                    Value newSup = y->getSup() - cstx;
                    if (x->getSup() > newSup) x->decrease(newSup);
                }
            }
            if ((y->getSup() < yinfty || CUT(wcsp->getLb()+costy-deltaCost, wcsp->getUb())) &&
                    min(y->getSup(),yinfty-1) < x->getInf() + cstx) {
                Value newInf = min(y->getInf() + csty, xinfty);
                if (x->getInf() < newInf) x->increase(newInf);
                if (x->getSup() < xinfty) {
                    Value newSup = x->getSup() - csty;
                    if (y->getSup() > newSup) y->decrease(newSup);
                }
            }
            // BAC* propagation (increase unary costs of domain bounds)
            if (x->unassigned() && y->getInf() < yinfty) {
                Value xinf = x->getInf();
                Cost cost = -((Cost) deltaCost);
                if (xinf < y->getInf() + csty && xinf > min(y->getSup(),yinfty-1) - cstx) cost += costy;
                if (xinf == deltaValueXinf) {
                    Cost delta = cost - deltaCostXinf;
                    if (delta != MIN_COST) {
                        deltaCostXinf = cost;
                        x->projectInfCost(delta);
                    }
                } else {
                    deltaValueXinf = xinf;
                    deltaCostXinf = cost;
                    x->projectInfCost(cost);
                }
            }
            if (x->unassigned() && y->getInf() < yinfty) {
                Value xsup = x->getSup();
                Cost cost = -((Cost) deltaCost);
                if (xsup==xinfty) cost += costx;
                else if (xsup < y->getInf() + csty && xsup > min(y->getSup(),yinfty-1) - cstx) cost += costy;
                if (xsup == deltaValueXsup) {
                    Cost delta = cost - deltaCostXsup;
                    if (delta != MIN_COST) {
                        deltaCostXsup = cost;
                        x->projectSupCost(delta);
                    }
                } else {
                    deltaValueXsup = xsup;
                    deltaCostXsup = cost;
                    x->projectSupCost(cost);
                }
            }
            if (y->unassigned() && x->getInf() < xinfty) {
                Value yinf = y->getInf();
                Cost cost = -((Cost) deltaCost);
                if (yinf < x->getInf() + cstx && yinf > min(x->getSup(),xinfty-1) - csty) cost += costx;
                if (yinf == deltaValueYinf) {
                    Cost delta = cost - deltaCostYinf;
                    if (delta != MIN_COST) {
                        deltaCostYinf = cost;
                        y->projectInfCost(delta);
                    }
                } else {
                    deltaValueYinf = yinf;
                    deltaCostYinf = cost;
                    y->projectInfCost(cost);
                }
            }
            if (y->unassigned() && x->getInf() < xinfty) {
                Value ysup = y->getSup();
                Cost cost = -((Cost) deltaCost);
                if (ysup==yinfty) cost += costy;
                else if (ysup < x->getInf() + cstx && ysup > min(x->getSup(),xinfty-1) - csty) cost += costx;
                if (ysup == deltaValueYsup) {
                    Cost delta = cost - deltaCostYsup;
                    if (delta != MIN_COST) {
                        deltaCostYsup = cost;
                        y->projectSupCost(delta);
                    }
                } else {
                    deltaValueYsup = ysup;
                    deltaCostYsup = cost;
                    y->projectSupCost(cost);
                }
            }
        }
    }
}
コード例 #10
0
ファイル: tb2arithmetic.cpp プロジェクト: eomahony/Numberjack
void Disjunction::propagate()
{
    if (ToulBar2::verbose >= 3) {
        print(cout);
        cout << " dxinf=" << deltaValueXinf << " dxsup=" << deltaValueXsup << " dyinf=" << deltaValueYinf << " dysup=" << deltaValueYsup << endl;
    }
    wcsp->revise(this);
    // deconnect the constraint if always satisfied
    if (x->getInf() >= y->getSup() + csty || y->getInf() >= x->getSup() + cstx) {
        deconnect();
    } else if (x->getSup() < y->getInf() + csty && y->getSup() < x->getInf() + cstx) {
        // IC0 propagatation (increase global lower bound if always unsatisfied)
        deconnect();
        if (x->unassigned()) {
            if (x->getInf() == deltaValueXinf) x->projectInfCost(-penalty);
            if (x->getSup() == deltaValueXsup) x->projectSupCost(-penalty);
        }
        if (y->unassigned()) {
            if (y->getInf() == deltaValueYinf) y->projectInfCost(-penalty);
            if (y->getSup() == deltaValueYsup) y->projectSupCost(-penalty);
        }
        projectLB(penalty);
    } else {
        // propagate hard constraint
        if (CUT(wcsp->getLb()+penalty, wcsp->getUb())) {
            if (x->getSup() < y->getInf() + csty) {
                Value newInf = x->getInf() + cstx;
                if (y->getInf() < newInf) y->increase(newInf);
                Value newSup = y->getSup() - cstx;
                if (x->getSup() > newSup) x->decrease(newSup);
            } else if (y->getSup() < x->getInf() + cstx) {
                Value newInf = y->getInf() + csty;
                if (x->getInf() < newInf) x->increase(newInf);
                Value newSup = x->getSup() - csty;
                if (y->getSup() > newSup) y->decrease(newSup);
            }
        }

        // BAC* propagation (increase unary costs of domain bounds)
        if (x->unassigned()) {
            Value xinf = x->getInf();
            if (xinf != deltaValueXinf && xinf != deltaValueXsup && xinf < y->getInf() + csty && xinf > y->getSup() - cstx) {
                deltaValueXinf = xinf;
                x->projectInfCost(penalty);
            }
            Value xsup = x->getSup();
            if (xsup != deltaValueXsup && xsup != deltaValueXinf && xsup < y->getInf() + csty && xsup > y->getSup() - cstx) {
                deltaValueXsup = xsup;
                x->projectSupCost(penalty);
            }
        }
        if (y->unassigned()) {
            Value yinf = y->getInf();
            if (yinf != deltaValueYinf && yinf != deltaValueYsup && yinf < x->getInf() + cstx && yinf > x->getSup() - csty) {
                deltaValueYinf = yinf;
                y->projectInfCost(penalty);
            }
            Value ysup = y->getSup();
            if (ysup != deltaValueYsup && ysup != deltaValueYinf && ysup < x->getInf() + cstx && ysup > x->getSup() - csty) {
                deltaValueYsup = ysup;
                y->projectSupCost(penalty);
            }
        }
    }
}
コード例 #11
0
ファイル: tb2arithmetic.cpp プロジェクト: eomahony/Numberjack
void Supxyc::propagate()
{
    if (ToulBar2::verbose >= 3) {
        print(cout);
        cout << " delta=" << deltaCost << " dxinf=" << deltaValueXinf << " dxcost=" << deltaCostXinf << " dysup=" << deltaValueYsup << " dycost=" << deltaCostYsup << endl;
    }
    wcsp->revise(this);
    // deconnect the constraint if always satisfied
    if (x->getInf() >= y->getSup() + cst) {
        deconnect();
    } else {
        // propagate hard constraint
        Cost gap = wcsp->getUb() - wcsp->getLb() - 1 + deltaCost;
        Value newInf = ceil(y->getInf() + cst - ((gap < deltamax)?gap:deltamax));
        if (x->getInf() < newInf) x->increase(newInf);

        Value newSup = floor(x->getSup() - cst + ((gap < deltamax)?gap:deltamax));
        if (y->getSup() > newSup) y->decrease(newSup);

        // IC0 propagatation (increase global lower bound)
        Cost cost = y->getInf() + cst - x->getSup() - deltaCost;
        if (cost > MIN_COST) {
            deltaCost += cost;
            projectLB(cost);
        }

        // BAC* propagation (increase unary costs of domain bounds for unassigned variables)
        if (x->unassigned()) {
            Value xinf = x->getInf();
            Value yinf = y->getInf();
            cost = max(yinf + cst - xinf - deltaCost, MIN_COST);
            if (xinf == deltaValueXinf) {
                Cost delta = cost - deltaCostXinf;
                if (delta != MIN_COST) {
                    deltaCostXinf = cost;
                    x->projectInfCost(delta);
                }
            } else {
                deltaValueXinf = xinf;
                deltaCostXinf = cost;
                x->projectInfCost(cost);
            }
        }

        if (y->unassigned()) {
            Value xsup = x->getSup();
            Value ysup = y->getSup();
            cost = max(ysup + cst - xsup - deltaCost, MIN_COST);
            if (ysup == deltaValueYsup) {
                Cost delta = cost - deltaCostYsup;
                if (delta != MIN_COST) {
                    deltaCostYsup = cost;
                    y->projectSupCost(delta);
                }
            } else {
                deltaValueYsup = ysup;
                deltaCostYsup = cost;
                y->projectSupCost(cost);
            }
        }
    }
}