//---------------------------------------------------------------------------------------------------------------------- // Positionable //---------------------------------------------------------------------------------------------------------------------- void Positionable::randomisePosition() { const float randXOffset = UniformRandom(-m_positionVar.x, m_positionVar.x); const float randYOffset = UniformRandom(-m_positionVar.y, m_positionVar.y); ci::Vec2f randOffset (randXOffset, randYOffset); setPosition(m_positionMean + randOffset); }
void SimulationCashierTakeCustomer(Simulation* self) { self->cashierMinutesLeft = CASHIER_MIN_TIME + UniformRandom(SimulationGetServiceStream(self)) * CASHIER_EXTRA_TIME; #if DEBUG printf("\tCashier took a customer (will take %d minutes).\n", self->cashierMinutesLeft); #endif }
void SimulationAddCustomer(Simulation* self) { int i; #if DEBUG printf("A new customer arrived on "); #endif SimulationPrintTime(self); if(UniformRandom(SimulationGetDecisionStream(self)) < 0.5) { for(i = 0; i < TELLER_COUNT; i++) { if(!self->tellerMinutesLeft[i]) { SimulationTellerTakeCustomer(self, i); return; } } #if DEBUG printf("\tThere were no tellers free, so the customer is waiting for one.\n"); #endif self->tellerQueue++; } else { #if DEBUG printf("\tThe customer is helping himself.\n"); #endif SimulationCustomerPurchasingDecision(self, CUSTOMER_SOLO_PURCHASE_RESISTANCE); } }
/* ShuffleDataFileList: Shuffle data file list */ static void ShuffleDataFileList() { int i, nIndex; g_nValidDfNum = g_nDataFileNum; /* shuffle data file list */ for (i = 0; i < g_nDataFileNum; i++) { nIndex = (int) (UniformRandom() * g_nDataFileNum); g_pShufDFList[i] = ExtractDataFile(nIndex); g_pShufDFList[i]->bValid = FALSE; g_nValidDfNum--; } /* copy shuffle list to orig list */ for (i = 0; i < g_nDataFileNum; i++) { g_pDataFileList[i] = g_pShufDFList[i]; g_pDataFileList[i]->bValid = TRUE; } }
void SwarmOptimizer::initialize(int ndims, int nelems) { this->ndims = ndims; this->nelems = nelems; swarm.resize(nelems); for(ParticleVector::iterator j = swarm.begin(); j != swarm.end(); ++j) { j->personalOptimum=0; j->friendOptimum=0; j->position.resize(ndims); j->velocity.resize(ndims,1.0); j->personalBest.resize(ndims); j->friendBest.resize(ndims); for (int i=0;i<ndims;i++) j->velocity[i]=UniformRandom(-10.0f, 10.0f); } setupGraph(); }
void SimulationCustomerPurchasingDecision(Simulation* self, double resistance) { if(UniformRandom(SimulationGetDecisionStream(self)) < resistance) { #if DEBUG printf("The customer successfully avoided being swindled into purchasing something.\n\n"); #endif } else { #if DEBUG printf("The customer wants to buy something!\n\n"); #endif if(!self->cashierMinutesLeft) { SimulationCashierTakeCustomer(self); return; } #if DEBUG printf("\tThe cashier isn't free, so the customer is waiting.\n"); #endif self->cashierQueue++; } }
gint main(gint argc, char** argv) { gtk_init(&argc, &argv); ball = g_slice_new(balls); paddle = g_slice_new(paddles); white = g_slice_new(GdkGC); black = g_slice_new(GdkGC); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request(window, DISPLAY_WIDTH, DISPLAY_HEIGHT); gtk_window_set_resizable(GTK_WINDOW(window), false); gtk_window_set_title(GTK_WINDOW(window), "GTK Pong"); draw = gtk_drawing_area_new(); gtk_container_add(GTK_CONTAINER(window), draw); InitCallback(); gtk_widget_show_all(window); pixmap = gdk_pixmap_new(draw->window, draw->allocation.width, draw->allocation.height, -1); black = draw->style->black_gc; white = draw->style->white_gc; GtkWidget *ballimagetmp = gtk_image_new_from_file("data/chromium.png"); ballimage = gtk_image_get_pixbuf(GTK_IMAGE(ballimagetmp)); BallInfo(); paddle->width = 100; paddle->height = 10; paddle->y = DISPLAY_HEIGHT - paddle->height; paddle->x = (DISPLAY_WIDTH / 2) - (paddle->width / 2); paddle->speed = paddle->width / 2; if (DISPLAY_WIDTH < paddle->width) { printf("Paddle too big"); return 2; } if (DISPLAY_WIDTH < ball->width) { printf("Ball too big"); return 2; } if (DISPLAY_HEIGHT < paddle->height + ball->height) { printf("Height of paddle and ball too big"); return 2; } ball->x = UniformRandom(0, DISPLAY_WIDTH - ball->width); //Initial position ball->y = UniformRandom(0, (DISPLAY_HEIGHT - ball->height - paddle->height) / 2); ball->dx = UniformRandom(3, 7); //Initial speed/direction of travel ball->dy = UniformRandom(3, 7); if (UniformRandom(1, 2) == 1)//Allow negative values initially ball->dx *= -1; if (UniformRandom(1, 2) == 1) ball->dy *= -1; Fill(white, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT); DrawPaddle(true); DrawBall(true); #ifdef DEBUG ball->dx = 0; ball->dy = 0; #else for (g_usleep(2 * G_USEC_PER_SEC); g_main_context_iteration(NULL, 0);) ; #endif while (true) { DrawBall(false); gint i; for (i = 0; i < ABS(ball->dx) || i < ABS(ball->dy); ++i) {//Move ball if (i < ABS(ball->dx)) { ball->x += ABS(ball->dx) / ball->dx; if (!checkPos()) { ball->dx *= -1; ball->x += 2 * ABS(ball->dx) / ball->dx; } } if (i < ABS(ball->dy)) { ball->y += ABS(ball->dy) / ball->dy; if (!checkPos()) { ball->dy *= -1; ball->y += 2 * ABS(ball->dy) / ball->dy; } } } DrawBall(true); for (g_usleep(20000); g_main_context_iteration(NULL, 0);) ; } return 0; }
//---------------------------------------------------------------------------------------------------------------------- void SMCAgent::update(float dt) { // Sense environment if(hasDistanceSensor()) { m_sensedValue = m_distanceSensor->getActivation(); m_sensedValueDerivative = m_distanceSensor->getDerivative(); switch(m_sensorMode) { case kSensorMode_Absolute: default: m_ctrnn->setExternalInput(0, m_sensedValue / dt); break; case kSensorMode_Derivative: m_ctrnn->setExternalInput(0, m_sensedValueDerivative); break; } } // Metabolise m_food = m_sensedValue * 10; float dA = - m_energy -(0.075*m_energy*m_energy*m_energy) + (0.5*m_food*m_energy*m_energy); m_energy += dA * dt; m_ctrnn->updateDynamic(dt); #if 0 // Update agent movement static int motTrans1, motTrans2; if(m_topology.getNumOutputs() >= 4) { motTrans1 = m_topology.getSize() - 4; motTrans2 = m_topology.getSize() - 1; static const int motRot1 = m_topology.getSize() - 3; static const int motRot2 = m_topology.getSize() - 2; m_angularVelocity = m_maxAngularSpeed * (m_ctrnn->getOutput(motRot1) - m_ctrnn->getOutput(motRot2)); m_angularVelocity += UniformRandom(-m_maxAngularSpeed / 100.0, m_maxAngularSpeed / 100.0); m_angle += m_angularVelocity * dt; if(m_angleWraps) m_angle = wrap(m_angle, -m_maxAngle, m_maxAngle); else m_angle = clamp(m_angle, -m_maxAngle, m_maxAngle); } #endif // Update positions if(m_topology.getNumOutputs() == 2) { float motTrans1 = m_topology.getSize() - 2; float motTrans2 = m_topology.getSize() - 1; m_velocity = m_maxSpeed * (m_ctrnn->getOutput(motTrans1) - m_ctrnn->getOutput(motTrans2)) * ci::Vec2f(0, 1); } else if(m_topology.getNumOutputs() == 1) { float motTrans1 = m_topology.getSize() - 1; m_velocity = m_maxSpeed * ((-1.0 + 2.0 * m_ctrnn->getOutput(motTrans1)) * ci::Vec2f(0, 1)); } m_position += m_velocity * dt; if(m_positionWraps) m_position.y = wrap(m_position.y, -m_maxPosition, m_maxPosition); /*else m_position.y = clamp(m_position.y, -m_maxPosition, m_maxPosition); */ m_time += dt; }
//---------------------------------------------------------------------------------------------------------------------- void BacteriumEvo::nextPhase() { m_phaseTime = 0; // Only store fitness on test phases if((m_phase >= 0))// && (m_phase % m_numTests != 0)) { m_phaseFits.push_back(m_phaseFit / m_phaseDuration); if(SETTINGS->getChild("Config/Globals/DebugLevel").getAttributeValue<int>("Value", 0) > 2) std::cout << "Phase " << m_phase << " fit: " << m_phaseFit/m_phaseDuration << std::endl; } m_phase++; m_phaseFit = 0; m_agent->reset(); // doesn't reset ctrnn // Setup next trial const std::vector<Positionable*>& objects = m_agent->getEnvironment().getObjects(); // Invisible food int test = m_phase % m_numTests; if(test == m_invTest-1) objects[1]->setVisibility(false); else objects[1]->setVisibility(true); if(m_phase % m_numTests == 0) { float foodR = isInnerPhase() ? 0.3 + m_randInitProp * UniformRandom(-0.1, 0.1) : 0.8 + m_randInitProp * UniformRandom(-0.1, 0.1); ((Torus*)objects[1])->setRadius(foodR); } // Every time we enter a new environment if((m_phase < m_numPhases) && (test == 0)) { // Change food position if(m_phase == 0) { ((Torus*)objects[1])->setColor(ci::ColorA(1,0,1,0.15)); } else { // alternate ((Torus*)objects[1])->setColor(ci::ColorA(1,0,0,0.15)); } } else{ ((Torus*)objects[1])->setColor(ci::ColorA(0,1,0,0.15)); } // Set agent position and orientation bool skipReset = !m_envReset && (test == 0) && (m_phase > 0); if(!skipReset) { float p = 0.55 + m_randInitProp * UniformRandom(-0.1, 0.1); float a = m_randInitProp * UniformRandom(-PI, PI); m_agent->setAngle(a); m_agent->setPosition(ci::Vec2f(0, p)); } m_phaseInitialDist = fabs(m_agent->getPosition().length() - ((Torus*)objects[1])->getRadius()); }
double ExponentialRandom(rand_stream* stream, int lambda) { return (-1.0 / (double) lambda) * log(1.0 - UniformRandom(stream)); }
void CTRNN::randomizeOutput(double lb, double ub) { for (int i = 0; i < size; i++) setOutput(i, UniformRandom(lb, ub)); }
void CTRNN::randomizeBiases(double lb, double ub) { for (int i = 0; i < size; i++) setBias(i, UniformRandom(lb, ub)); }
void CTRNN::RandomizeCircuitOutput(double lb, double ub) { for (int i = 1; i <= size; i++) SetNeuronOutput(i, UniformRandom(lb, ub)); }
// Randomize the states or outputs of a circuit. //---------------------------------------------------------------------------------------------------------------------- void CTRNN::randomizeState(double lb, double ub) { for (int i = 0; i < size; i++) setState(i, UniformRandom(lb, ub)); }
//---------------------------------------------------------------------------------------------------------------------- void Positionable::randomiseAngle() { const float randAngle = UniformRandom(-m_angleVar, m_angleVar); setAngle(m_angleMean + randAngle); }
void CTRNN::randomizeTimeConstants(double lb, double ub) { for (int i = 0; i < size; i++) setTimeConstant(i, UniformRandom(lb, ub)); }
void CTRNN::randomizeWeights(double lb, double ub) { for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) setWeight(i, j, UniformRandom(lb, ub)); }