double RunElectionTrial(int voters, double spread, double votingError) {
    int invalid = 0;
    
    for (int i = 0; i < MaxTrials; i++) {
        int votesForA = voters/2  + (voters * spread)/2;
        int votesForB = voters/2  - (voters * spread)/2;
        
        //cout << "Trial " << i << ": Before: A = " << votesForA << " : B = " << votesForB << " : A+B= " << votesForA + votesForB << " . " << endl;
        int errorA = 0;
        int errorB = 0;
        
        for (int j = 0; j < votesForA; j++)
            if (RandomChance(votingError)) {
                errorA++;
                votesForA--;
                votesForB++;
            }
        for (int j = 0; j < votesForB; j++)
            if (RandomChance(votingError)) {
                errorB++;
                votesForA++;
                votesForB--;
            }
        //cout << "Error against A # " << errorA << endl;
        //cout << "Error against B # " << errorB << endl;
        
        if (votesForA <= votesForB)
            invalid++;
        
        //cout << "Trial " << i << ": After: A = " << votesForA << " : B = " << votesForB << " : A+B= " << votesForA + votesForB << " Invalid = " << invalid << endl;
    }
    
    return 100.0 * (double)invalid/(double)MaxTrials;
}
void castVotes(int &totalVotes, int voters, double percentError) {
    totalVotes = 0;
    for (int i = 0; i < voters; i++) {
        if (!RandomChance(percentError))
	    totalVotes++;
    }
}
Exemple #3
0
void RunSim( double err_pct, int votes_X_should_get, int *votes_for_X, int *votes_for_Y ) {
    for ( int i = 1; i <= votes_X_should_get; i++ ) {
        if ( RandomChance(1 - err_pct) ) {
            (*votes_for_X)++;
        }
        else {
            (*votes_for_Y)++;
        }
    }
}
Exemple #4
0
/*
 * Function: RunMemoryTrial
 * ------------------------
 * Fills a pqueue to specified size and reports memory usage.  Then does
 * a bunch of enqueue-dequeue operations to jumble things up and reports
 * on the memory usage again. Reports results to cout.
 */
void RunMemoryTrial(int size)
{
    PQueue pq;
    cout << endl << "Running memory trial on " << size << "-element pqueue" << endl;
    for (int i = 0; i < size; i++)
		pq.enqueue(RandomInteger(1, size));
    cout << "After consecutive enqueues, " << size << "-element pqueue is using "
         << pq.bytesUsed()/1000 << " KB of memory" << endl;
    int num = size;
    for (int j = 0; j < NumRepetitions; j++) { /* do a bunch of enqueue/dequeue ops */
	    if (RandomChance(.5)) { 
	        pq.enqueue(RandomInteger(0, size));
	        num++;
        } else {
	        pq.dequeueMax();
	        num--;
	    }
    }
    cout << "After more enqueue/dequeue, " << num << "-element pqueue is using "
         << pq.bytesUsed()/1000 << " KB of memory" << endl;
}
Exemple #5
0
double CalculateError(int v, double s, double e) {
    double total = 0; // number of invalid elections
    int spread = v * s;
    for (int j = 0; j < NUM_TRIALS; j++) {
        int countA = 0; // candidate A
        int countB = 0; // candidate B
        for (int i = 0; i < v; i++) {
            bool vote = RandomChance(1.0 - e); // cast vote
            if (i < (v/2) + spread) { // apply votes according to spread
                if (vote) countA++;
                else countB++;
            } else {
                if (vote) countB++;
                else countA++;
            }
        }
        if (countB > countA)
            total++;
    }
    return double(total/NUM_TRIALS * 100);
}
Exemple #6
0
void
Weapon::Aim()
{
    locked   = false;
    centered = false;

    FindObjective();

    if (target) {
        double az = 0;
        double el = 0;

        locked = CanLockPoint(obj_w, az, el, &objective);

        double spread_az = design->spread_az;
        double spread_el = design->spread_el;

        // beam sweep target:
        if (design->beam) {
            double factor   = 0;
            double az_phase = 0;
            double el_phase = 0;

            if (target->Type() == SimObject::SIM_SHIP) {
                Ship* s = (Ship*) target;

                if (s->IsStarship()) {
                    switch (sweep) {
                    default:
                    case SWEEP_NONE:  factor = 0; break;
                    case SWEEP_TIGHT: factor = 1; break;
                    case SWEEP_WIDE:  factor = 2; break;
                    }
                }
            }

            if (factor > 0) {
                factor  *= atan2(target->Radius(), (double) objective.z);

                for (int i = 0; i < nbarrels; i++) {
                    if (beams && beams[i]) {
                        az_phase = sin(beams[i]->Life() * 0.4 * PI);
                        el_phase = sin(beams[i]->Life() * 1.0 * PI);
                        break;
                    }
                }

                az += factor * spread_az * az_phase;
                el += factor * spread_el * el_phase * 0.25;
            }
        }

        else if (!design->beam) {
            if (spread_az > 0)
            az += Random(-spread_az, spread_az);

            if (spread_el > 0)
            el += Random(-spread_el, spread_el);
        }

        AimTurret(az, -el);

        // check range for guided weapons:
        if (locked && guided) {
            double range = objective.length();

            if (range > design->max_track)
            locked = false;

            else if (range > design->max_range) {
                if (firing) {
                    if (RandomChance(1,4))   // 1 in 4 chance of locking anyway
                    locked = false;
                }
                else {
                    locked = false;
                }
            }
        }

        if (locked) {
            Point tloc = target->Location();
            tloc = Transform(tloc);

            if (tloc.z > 1) {
                az = atan2(fabs(tloc.x), tloc.z);
                el = atan2(fabs(tloc.y), tloc.z);

                double firing_cone = 10*DEGREES;

                if (orders == MANUAL)
                firing_cone = 30*DEGREES;

                if (az < firing_cone && el < firing_cone)
                centered = true;
            }
        }
    }
    else {
        AimTurret(aim_az_rest, -aim_el_rest);
    }
}
CampaignMissionRequest*
CampaignPlanMission::PlanRandomFighterMission()
{
    CampaignMissionRequest* request  = 0;
    int                     type     = fighter_mission_types[fighter_mission_index++];
    int                     ownside  = player_group->GetIFF();
    CombatGroup*            primary  = player_group;
    CombatGroup*            obj      = 0;

    if (fighter_mission_index > 15)
    fighter_mission_index = 0;

    if (type == Mission::ESCORT_FREIGHT) {
        CombatGroup*  freight  = campaign->FindGroup(ownside, CombatGroup::FREIGHT);
        if (!freight || freight->CalcValue() < 1)
        type = Mission::PATROL;
        else
        obj  = freight;
    }

    else if (type == Mission::ESCORT_SHUTTLE) {
        CombatGroup*  shuttle = campaign->FindGroup(ownside, CombatGroup::LCA_SQUADRON);
        if (!shuttle || shuttle->CalcValue() < 1)
        type = Mission::PATROL;
        else
        obj  = shuttle;
    }

    else if (primary->Type() == CombatGroup::WING) {
        if (RandomChance())
        primary = primary->FindGroup(CombatGroup::INTERCEPT_SQUADRON);
        else
        primary = primary->FindGroup(CombatGroup::FIGHTER_SQUADRON);
    }

    if (type >= Mission::AIR_PATROL && type <= Mission::AIR_INTERCEPT) {
        CombatZone* zone     = 0;
        bool        airborne = false;

        if (primary)
        zone = primary->GetAssignedZone();

        if (zone && zone->GetRegions().size() > 1) {
            Text        air_region = *zone->GetRegions().at(1);
            StarSystem* system     = campaign->GetSystem(zone->System());

            if (system) {
                OrbitalRegion* rgn = system->FindRegion(air_region);

                if (rgn && rgn->Type() == Orbital::TERRAIN)
                airborne = true;
            }
        }

        if (!airborne) {
            if (type == Mission::AIR_INTERCEPT)
            type = Mission::INTERCEPT;

            else if (type == Mission::AIR_SWEEP)
            type = Mission::SWEEP;

            else
            type = Mission::PATROL;
        }
    }

    request = new(__FILE__,__LINE__)
    CampaignMissionRequest(campaign, type, start, primary);

    if (request)
    request->SetObjective(obj);

    return request;
}