Example #1
0
bool PbSolver::convertPbs(bool first_call)
{
    vec<Formula>    converted_constrs;

    if (first_call){
        findIntervals();
        if (!rewriteAlmostClauses()){
            ok = false;
            return false; }
    }

    for (int i = 0; i < constrs.size(); i++){
        if (constrs[i] == NULL) continue;
        Linear& c   = *constrs[i]; assert(c.lo != Int_MIN || c.hi != Int_MAX);

        if (options->opt_verbosity >= 1)
            /**/reportf("---[%4d]---> ", constrs.size() - 1 - i);

        if (options->opt_convert == ct_Sorters) {
            if (options->opt_dump) { // no formulae built
                buildConstraint(c,primesLoader, options);
            } else {
                converted_constrs.push(buildConstraint(c,primesLoader, options));
            }
        }
        else if (options->opt_convert == ct_Adders)
            linearAddition(c, converted_constrs, options->opt_verbosity);
        else if (options->opt_convert == ct_BDDs)
            converted_constrs.push(convertToBdd(c, options->opt_verbosity));
        else if (options->opt_convert == ct_Mixed){
            int adder_cost = estimatedAdderCost(c);
            //**/printf("estimatedAdderCost: %d\n", estimatedAdderCost(c));
            Formula result = convertToBdd(c, options->opt_verbosity, (int)(adder_cost * options->opt_bdd_thres));
            if (result == _undef_)
                result = buildConstraint(c,primesLoader, options, (int)(adder_cost * options->opt_sort_thres));
            if (result == _undef_)
                linearAddition(c, converted_constrs, options->opt_verbosity);
            else
                converted_constrs.push(result);
        }else
            assert(false);

        if (!ok) return false;
    }

    if (!options->opt_validateResoult) cleanPBC();
	
	formulaSize = converted_constrs.size();
	
    clausify(*sat_solver, options, converted_constrs);
    
    if (options->opt_dump) {
        exit(0);
    }

    return ok;
}
Example #2
0
// Will return '_undef_' if 'cost_limit' is exceeded.
//
Formula buildConstraint(const Linear& c, int max_cost)
{
    vec<Formula>    ps;
    vec<Int>        Cs;

    for (int j = 0; j < c.size; j++)
        ps.push(lit2fml(c[j])),
        Cs.push(c(j));

    vec<Int> dummy;
    int      cost;
    vec<int> base;
    optimizeBase(Cs, dummy, cost, base);
    FEnv::push();

    Formula ret;
    try {
        ret = buildConstraint(ps, Cs, base, c.lo, c.hi, max_cost);
    }catch (Exception_TooBig){
        FEnv::pop();
        return _undef_;
    }

    if (opt_verbosity >= 1){
        reportf("Sorter-cost:%5d     ", FEnv::topSize());
        reportf("Base:"); for (int i = 0; i < base.size(); i++) reportf(" %d", base[i]); reportf("\n");
    }
    FEnv::keep();
    return ret;
}
Example #3
0
static
Formula buildConstraint(vec<Formula>& ps, vec<Int>& Cs, vec<int>& base, Int lo, Int hi, int max_cost)
{
    vec<Formula> carry;
    vec<vec<Formula> > digits;
    buildConstraint(ps, Cs, carry, base, 0, digits, max_cost);
    if (FEnv::topSize() > max_cost) throw Exception_TooBig();

    vec<int> lo_digs;
    vec<int> hi_digs;
    if (lo != Int_MIN)
        convert(lo, base, lo_digs);
    if (hi != Int_MAX)
        convert(hi+1, base, hi_digs);   // (+1 because we will change '<= x' to '!(... >= x+1)'


    /*DEBUG
    pf("Networks:");
    for (int i = 0; i < digits.size(); i++)
        pf(" %d", digits[i].size());
    pf("\n");

    if (lo != Int_MIN){
        pf("lo=%d :", lo); for (int i = 0; i < lo_digs.size(); i++) pf(" %d", lo_digs[i]); pf("\n"); }
    if (hi != Int_MAX){
        pf("hi+1=%d :", hi+1); for (int i = 0; i < hi_digs.size(); i++) pf(" %d", hi_digs[i]); pf("\n"); }
    END*/

/*
Base:  (1)    8    24   480
       aaa bbbbbb ccc ddddddd
Num:    2    0     5     6
*/

    Formula ret = ((lo == Int_MIN) ? _1_ :  lexComp(lo_digs, digits))
                & ((hi == Int_MAX) ? _1_ : ~lexComp(hi_digs, digits));
    if (FEnv::topSize() > max_cost) throw Exception_TooBig();
    return ret;
}
Example #4
0
Racer::Racer(IDirect3DDevice9* device, RacerType racerType)
{
	engineVoice = NULL;

	gunMountDraw = new Drawable(GUNMOUNTMESH, "textures/gun.dds", device);
	Renderer::renderer->addDrawable(gunMountDraw);

	gunDraw = new Drawable(GUNMESH, "textures/gun.dds", device);
	Renderer::renderer->addDrawable(gunDraw);

	engineVoice = Sound::sound->reserveSFXVoice();

	health = 100;
	kills = 0;
	suicides = 0;
	deaths = 0;
	givenDamage = 0;
	takenDamage = 0;
	laserReady = true;
	laserTime = 0.0f;

	respawnTimer = 0.0f;
	respawned = true;

	index = -1;

	currentSteering = 0.0f;
	currentAcceleration = 0.0f;

	lookDir.set(0, 0, 1);
	lookHeight = 0;


	switch (racerType)
	{
	case RACER1:
		drawable = new Drawable(RACER, "textures/racerred.dds", device);
		break;
	case RACER2:
		drawable = new Drawable(RACER, "textures/racerblue.dds", device);
		break;
	case RACER3:
		drawable = new Drawable(RACER, "textures/racerorange.dds", device);
		break;
	case RACER4:
		drawable = new Drawable(RACER, "textures/racergreen.dds", device);
		break;
	case RACER5:
		drawable = new Drawable(RACER, "textures/racerteal.dds", device);
		break;
	case RACER6:
		drawable = new Drawable(RACER, "textures/raceryellow.dds", device);
		break;
	case RACER7:
		drawable = new Drawable(RACER, "textures/racerpurple.dds", device);
		break;
	case RACER8:
		drawable = new Drawable(RACER, "textures/racerpink.dds", device);
		break;
	default:
		drawable = new Drawable(RACER, "textures/racerred.dds", device);
	}


	// Set up filter group (so the car doesn't collide with the wheels)
	int collisionGroupFilter = Physics::physics->getFilter();
	
	hkpRigidBodyCinfo info;
	hkVector4 halfExtent(0.9f, 0.6f, 2.3f);		//Half extent for racer rigid body box
	info.m_shape = new hkpBoxShape(halfExtent);
	info.m_qualityType = HK_COLLIDABLE_QUALITY_CRITICAL;
	info.m_centerOfMass.set(0.0f, 0.0f, 0.0f);
	info.m_restitution = 0.0f;
	info.m_maxAngularVelocity = 10.0f;
	info.m_maxLinearVelocity = 170.0f;
	info.m_angularDamping = 0.4f;
	hkpMassProperties massProperties;
	hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfExtent, chassisMass, massProperties);
	info.setMassProperties(massProperties);
	info.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(hkpGroupFilterSetup::LAYER_AI, collisionGroupFilter);
	body = new hkpRigidBody(info);		//Create rigid body
	body->setLinearVelocity(hkVector4(0, 0, 0));
	info.m_shape->removeReference();

	
	hkpPropertyValue val;
	val.setPtr(this);

	body->setProperty(0, val);

	index = Renderer::renderer->addDrawable(drawable);
	Physics::physics->addRigidBody(body);


	// Create tires
	wheelFL = new FrontWheel(device, collisionGroupFilter);
	Renderer::renderer->addDrawable(wheelFL->drawable);
	Physics::physics->addRigidBody(wheelFL->body);

	
	wheelFR = new FrontWheel(device, collisionGroupFilter);
	Renderer::renderer->addDrawable(wheelFR->drawable);
	Physics::physics->addRigidBody(wheelFR->body);


	wheelRL = new RearWheel(device, collisionGroupFilter);
	Renderer::renderer->addDrawable(wheelRL->drawable);
	Physics::physics->addRigidBody(wheelRL->body);


	wheelRR = new RearWheel(device, collisionGroupFilter);
	Renderer::renderer->addDrawable(wheelRR->drawable);
	Physics::physics->addRigidBody(wheelRR->body);

	// Now constrain the tires
	hkpGenericConstraintData* constraint;
	hkpConstraintInstance* constraintInst;

	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachFL, constraint, FRONT);
	constraintInst = new hkpConstraintInstance(wheelFL->body, body, constraint);
	Physics::world->addConstraint(constraintInst);
	constraint->removeReference();

	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachFR, constraint, FRONT);
	constraintInst = new hkpConstraintInstance(wheelFR->body, body, constraint);
	Physics::world->addConstraint(constraintInst);
	constraint->removeReference();
	
	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachRL, constraint, REAR);
	constraintInst = new hkpConstraintInstance(wheelRL->body, body, constraint);
	Physics::world->addConstraint(constraintInst);
	constraint->removeReference();

	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachRR, constraint, REAR);
	constraintInst = new hkpConstraintInstance(wheelRR->body, body, constraint);
	Physics::world->addConstraint(constraintInst);
	constraint->removeReference();

	
	hkpConstraintStabilizationUtil::stabilizeRigidBodyInertia(body);

	reset(&(hkVector4(0, 0, 0, 0)), 0);

	emitter = Sound::sound->getEmitter();

	braking = false;
}
Racer::Racer(IDirect3DDevice9* device, Renderer* r, Physics* p, RacerType racerType)
{
	index = -1;

	currentSteering = 0.0f;


	switch (racerType)
	{
	case PLAYER:
		drawable = new Drawable(RACER, "racer1.dds", device);
		break;
	case AI1:
		drawable = new Drawable(RACER, "racer2.dds", device);
		break;
	default:
		drawable = new Drawable(RACER, "racer2.dds", device);
	}


	// Set up filter group (so the car doesn't collide with the wheels)
	int collisionGroupFilter = p->getFilter();
	
	hkpRigidBodyCinfo info;
	hkVector4 halfExtent(0.9f, 0.7f, 2.3f);		//Half extent for racer rigid body box
	info.m_shape = new hkpBoxShape(halfExtent);
	info.m_qualityType = HK_COLLIDABLE_QUALITY_CRITICAL;
	info.m_centerOfMass = hkVector4(0.0f, -0.4f, -1.2f);	// move CM a bit
	info.m_restitution = 0.1f;
	hkpMassProperties massProperties;
	hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfExtent, chassisMass, massProperties);
	info.setMassProperties(massProperties);
	info.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(hkpGroupFilterSetup::LAYER_AI, collisionGroupFilter);
	body = new hkpRigidBody(info);		//Create rigid body
	body->setLinearVelocity(hkVector4(0, 0, 0));
	info.m_shape->removeReference();

	index = r->addDrawable(drawable);
	p->addRigidBody(body);


	// Create tires
	wheelFL = new FrontWheel(device, collisionGroupFilter);
	r->addDrawable(wheelFL->drawable);
	p->addRigidBody(wheelFL->body);
	
	WheelListener* listenFL = new WheelListener(&(wheelFL->touchingGround));
	wheelFL->body->addContactListener(listenFL);

	
	wheelFR = new FrontWheel(device, collisionGroupFilter);
	r->addDrawable(wheelFR->drawable);
	p->addRigidBody(wheelFR->body);

	WheelListener* listenFR = new WheelListener(&(wheelFR->touchingGround));
	wheelFL->body->addContactListener(listenFR);
	


	wheelRL = new RearWheel(device, collisionGroupFilter);
	r->addDrawable(wheelRL->drawable);
	p->addRigidBody(wheelRL->body);

	WheelListener* listenRL = new WheelListener(&(wheelRL->touchingGround));
	wheelFL->body->addContactListener(listenRL);


	wheelRR = new RearWheel(device, collisionGroupFilter);
	r->addDrawable(wheelRR->drawable);
	p->addRigidBody(wheelRR->body);
	
	WheelListener* listenRR = new WheelListener(&(wheelRR->touchingGround));
	wheelFL->body->addContactListener(listenRR);

	// Now constrain the tires
	hkpGenericConstraintData* constraint;
	hkpConstraintInstance* constraintInst;

	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachFL, constraint, FRONT);
	constraintInst = new hkpConstraintInstance(wheelFL->body, body, constraint);
	p->world->addConstraint(constraintInst);
	constraint->removeReference();

	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachFR, constraint, FRONT);
	constraintInst = new hkpConstraintInstance(wheelFR->body, body, constraint);
	p->world->addConstraint(constraintInst);
	constraint->removeReference();
	
	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachRL, constraint, REAR);
	constraintInst = new hkpConstraintInstance(wheelRL->body, body, constraint);
	p->world->addConstraint(constraintInst);
	constraint->removeReference();

	constraint = new hkpGenericConstraintData();
	buildConstraint(&attachRR, constraint, REAR);
	constraintInst = new hkpConstraintInstance(wheelRR->body, body, constraint);
	p->world->addConstraint(constraintInst);
	constraint->removeReference();

	
	hkpConstraintStabilizationUtil::stabilizeRigidBodyInertia(body);

	reset();
}
Example #6
0
static
void buildConstraint(vec<Formula>& ps, vec<Int>& Cs, vec<Formula>& carry, vec<int>& base, int digit_no, vec<vec<Formula> >& out_digits, int max_cost)
{
    assert(ps.size() == Cs.size());

    if (FEnv::topSize() > max_cost) throw Exception_TooBig();
    /**
    pf("buildConstraint(");
    for (int i = 0; i < ps.size(); i++)
        pf("%d*%s ", Cs[i], (*debug_names)[index(ps[i])]);
    pf("+ %d carry)\n", carry.size());
    **/

    if (digit_no == base.size()){
        // Final digit, build sorter for rest:
        // -- add carry bits:
        for (int i = 0; i < carry.size(); i++)
            ps.push(carry[i]),
            Cs.push(1);
        out_digits.push();
        buildSorter(ps, Cs, out_digits.last());

    }else{
        vec<Formula>    ps_rem;
        vec<int>        Cs_rem;
        vec<Formula>    ps_div;
        vec<Int>        Cs_div;

        // Split sum according to base:
        int B = base[digit_no];
        for (int i = 0; i < Cs.size(); i++){
            Int div = Cs[i] / Int(B);
            int rem = toint(Cs[i] % Int(B));
            if (div > 0){
                ps_div.push(ps[i]);
                Cs_div.push(div);
            }
            if (rem > 0){
                ps_rem.push(ps[i]);
                Cs_rem.push(rem);
            }
        }

        // Add carry bits:
        for (int i = 0; i < carry.size(); i++)
            ps_rem.push(carry[i]),
            Cs_rem.push(1);

        // Build sorting network:
        vec<Formula> result;
        buildSorter(ps_rem, Cs_rem, result);

        // Get carry bits:
        carry.clear();
        for (int i = B-1; i < result.size(); i += B)
            carry.push(result[i]);

        out_digits.push();
        for (int i = 0; i < B-1; i++){
            Formula out = _0_;
            for (int j = 0; j < result.size(); j += B){
                int n = j+B-1;
                if (j + i < result.size())
                    out |= result[j + i] & ((n >= result.size()) ? _1_ : ~result[n]);
            }
            out_digits.last().push(out);
        }

        buildConstraint(ps_div, Cs_div, carry, base, digit_no+1, out_digits, max_cost); // <<== change to normal loop
    }
}
// Will return '_undef_' if 'cost_limit' is exceeded.
//
Formula buildConstraint(const Linear& c,PrimesLoader& pl, PBOptions* options, int max_cost)
{
    vec<Formula>    ps;
    vec<Int>        Cs;

    for (int j = 0; j < c.size; j++)
        ps.push(lit2fml(c[j])),
        Cs.push(c(j));
	
    vec<int> base;
  
    SearchMetaData* data = searchForBase(Cs, base, pl, options);
    FEnv::push();
    if (options->opt_dump) { // don't spend time on building unneeded formulae
        return _undef_;
    }
    Int lo = c.lo;
    Int hi = c.hi;
	if (options->opt_tare & (lo == hi | lo == Int_MIN | hi == Int_MAX)) {
		Int toNormlize;
		Int toAdd;
		if (lo == Int_MIN) toNormlize = hi;
		else                 toNormlize = lo;
		Int temp = 1;
		int i;
		for(i=0;temp<toNormlize && i<base.size();i++) temp *= base[i];
		if (temp < toNormlize) {
			i=2;
			while (temp*i < toNormlize) i++;
			temp*=i;
		}
		else {
			i--;
			Int bmi;
			if (i==-1) bmi=1;
			else       bmi = temp / base[i];   
			while(temp-bmi>=toNormlize) temp-=bmi;
			assert(temp > toNormlize);
		}
		toAdd = temp-toNormlize;
		if (toAdd>0) {
			ps.push(_1_);
			Cs.push(toAdd);
			if (lo != Int_MIN) lo=lo+toAdd;
			if (hi != Int_MAX) hi=hi+toAdd;
		}
	}
    Formula ret;
    try {
    	if (options->opt_verbosity >= 1) {
	    	if (c.lo != Int_MIN) {
	    		printf("orignal   lo:%5d     \n", (int)toint(c.lo));
	    		printf("normlized lo:%5d     \n", (int)toint(lo));
	    	}
	    	if  (c.hi != Int_MAX) {
	    		printf("orignal   hi:%5d     \n", (int)toint(c.hi));
	    		printf("normlized hi:%5d     \n", (int)toint(hi));
	    	}
    	}
        ret = buildConstraint(ps, Cs, base, lo, hi, max_cost, options);
    }catch (Exception_TooBig){
        FEnv::pop();
        return _undef_;
    }
	
	if (data!=0) data->fEnvSize = FEnv::topSize();
	
    if (options->opt_verbosity >= 1){
        printf("FEnv.topSize:%5d     ", FEnv::topSize());
        printf("Base:"); for (int i = 0; i < base.size(); i++) printf(" %d", base[i]); printf("\n");
    }
    FEnv::keep();
    return ret;
}