//*************************************************************************** // // file: PreyPred.C // // // TITLE: Prey approach and Predator avoid. // GOAL: Schema Construction and Dynamics based on SBL. // // REFERENCES: // ------------ // Refer to Corbacho and Arbib (1995) for details on detour itself. // // ARCHITECTURE: // ------------ // Perceptual Schemas as functional units. // Fields corresponding to schema instances; // Integration of fields; WTA; Sensorimotor Xform; // Motor schemas as functional units. // // // SCHEMA DYNAMICS: // -------- // Relaxation Labeling (Hummel and Zucker, 1983). // Control of schemas based on distributed C&C dynamics. // Inclusion of NeuroSchemas. // // // SIMPLIFICATIONS: // --------------- // Perceptual Schemas (not Neural Implementation). // Motor Schemas as functional units. // // // SCHEMA CONSTRUCTION: // -------------------- // Autonomous Schema Construction in Run time. // Coherence and Performance Maximization. // // // AUTHOR: Gamaliel Rodrigo Sigala, based on the Fernando Jose Corbacho learning to detour work (Detour.C) // DATE (last update): Agost 20, 2001 // // // *************************************************************************** #include "nsl_include.h" #include "preypred.h" void gauss1D(nsl_vector& kernel,nsl_data& sigma) { int xn = kernel.get_imax(); float sig = sigma.get_data(); float k = xn/2; const float PI = 3.14159; float f= sig*sqrt(2.0*PI); for (int i=-k; i <= k - (1 - xn%2); i++) kernel.elem(i+k) = exp(-(i*i)/(2*sig*sig))/f; } void gauss2D(nsl_matrix& kernel,nsl_data& sigma) { int xn = kernel.get_imax(); int yn = kernel.get_jmax(); float sig = sigma.get_data(); int k1 = xn/2; int k2 = yn/2; const float PI = 3.14159; float f= sig*sqrt(2.0*PI); for (int i=-k1; i<=k1 - (1 - xn%2); i++) for (int j=-k2; j<=k2 - (1 - yn%2); j++) kernel.elem(i+k1,j+k2) = exp(-(i*i+j*j)/(2*sig*sig))/f; } nsl_vector maxim(nsl_vector& u) // returns the vector normalized (substraction) by its maximum. // only the maximum is above threshold (by 0.01). { int imax = u.get_imax(); nsl_vector c(imax); float max = 0; // Maximum value int max_i=0; // Coordinates of max value max = NSLmax(u); if (max == 0) for (int i=0; i < imax; i++) c.elem(i)= - 0.01; else for (int i=0; i < imax; i++) c.elem(i)= u.elem(i) - max + 0.01; c = NSLstep(c); return c; } nsl_matrix filter(nsl_matrix& view, int f) // filters out all the elements in the matrix that do not have a // value of "f". Simplified feature detector where "f" is the feature. { int i,j; int imax = view.get_imax(); nsl_matrix c(imax, imax); for (i=0; i yb.elem()) //si la rana pasó la barrera en y depthY = yf - yb; else depthY = 0; if (xf.elem() > xb_end.elem()) // si la rana está más a la der que la barrera depthX = xf - xb_end; else if (xf.elem() < xb_init.elem()) depthX = xb_init - xf; else depthX = 0; // this is not used if (depthY.elem() > safe_distance.elem() || depthX.elem() > safe_distance.elem()) // safe_distance.elem() ps= 2; // Go up fast else */ ps = 0.0; triggerSchema(); } void DepthField::triggerSchema() { if (ps.elem() > th.elem()) { printf("Depth \t"); } } Tactile::Tactile(char* str, NslModule* parent) : NslModule(str,parent), safe_distance("safe_distance",this), depthY("depthY",this), depthX("depthX",this), ps("ps",this), th("th",this) { } void Tactile::initSys() { ps = 0; } void Tactile::simRun() { if (depthY.elem() > 0 && depthY.elem() <= safe_distance.elem() && depthX.elem() <= safe_distance.elem()) ps = 2; // Go up fast else ps = 0.0; // Go down fast: -2.0 updateLabel(); triggerSchema(); } void Tactile::updateLabel() { // ps = ps + qs; ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void Tactile::triggerSchema() { if (ps.elem() > th.elem()) { printf("Tactile \t"); NSLoutput("Tactile \t"); } } FeedMotivation::FeedMotivation(char* str, NslModule* parent) : NslModule(str,parent), date("date", this), dayhour("dayhour",this), hunger("hunger",this), ps("ps",this), prey_ate("prey_ate", this), weigth_date("weigth_date", this), weigth_hour("weigth_hour", this), weigth_hunger("weigth_hunger", this) { } void FeedMotivation::initSys() { weigth_hunger = 0.4 ; weigth_date = 0.3; weigth_hour = 0.3; ps = 0; } void FeedMotivation::simRun() { month = date.elem() / 100; day = date.elem() - month*100; hour = dayhour.elem() /100; minute = dayhour.elem() - hour*100 ; NSLoutput("\nMes: ",month); NSLoutput("Día: ",day); NSLoutput("Hora: ",hour); NSLoutput("Minute: ",minute); updateLabel(); } void FeedMotivation::updateLabel() { static float mdate, mhour, alfadate, alfahour; static float max_privation, pi; static float hungerval = hunger.elem(); static int num_prey = 0; //presas capturadas mdate = 182; alfadate = 100; mhour=12; alfahour=4; max_privation = 48 ; //No. de horas máximo que soporta con provación de alimento pi=3.1416; static NslFloat0 hunger_effect, date_effect, date_effect_max, hour_effect, hour_effect_max, xdate, xhour; static NslFloat0 fraccion = minute; // hunger if (prey_ate==1) { num_prey++; } if ( (hunger<=0) || (hunger<1) ) hunger_effect = 0; else hunger_effect = log(hungerval) / log(max_privation) ; hunger_effect = hunger_effect - 0.2*num_prey; //Disminuye el hambre en cierto valor (0.2) if (hunger_effect<0) hunger_effect=0; if ( (hunger<=0) || (hunger<1) ) hunger_effect = 0; //date if (month==1) xdate = 328 + day; if (month==2) { if (day<=5) xdate = 359 +day; else xdate = day - 6; } if (month==3) xdate = 22+day; if (month==4) xdate = 53+day; if (month==5) xdate = 83+day; if (month==6) xdate = 114+day; if (month==7) xdate = 144+day; if (month==8) xdate = 175+day; if (month==9) xdate = 206+day; if (month==10) xdate = 236+day; if (month==11) xdate = 267+day; if (month==12) xdate = 297+day; if (alfadate!=0) { date_effect = exp(-1 * (xdate.elem()-mdate) * (xdate.elem()-mdate) / (2 * alfadate * alfadate) ) / ( sqrt(2*pi) * alfadate ); date_effect_max = 1 / ( sqrt(2*pi) * alfadate ); //valor en la media } else date_effect = 0; date_effect = date_effect / date_effect_max; //hour if ( (hour>=12) && (hour<=23) ) xhour = hour - 12 + fraccion/60; else if ( (hour>=0) && (hour<12) ) xhour = hour + 12 + fraccion/60; if (alfahour!=0) { hour_effect = exp(-1 * (xhour.elem()-mhour) * (xhour.elem()-mhour) / (2 * alfahour * alfahour) ) / ( sqrt(2*pi) * alfahour ); hour_effect_max = 1 / ( sqrt(2*pi) * alfahour ); //valor en la media } else hour_effect = 0; //Normalize hour_effect = hour_effect / hour_effect_max; NSLoutput("\nHunger_effect: ",hunger_effect.elem()); NSLoutput("Date_effect: ",date_effect.elem()); NSLoutput("Hour_effect: ",hour_effect.elem()); if ( hunger_effect == 0) ps = 0; else ps = hour_effect*weigth_hour + date_effect*weigth_date + hunger_effect*weigth_hunger ; NSLoutput("ps_motivation: ",ps.elem()); } PreyRec::PreyRec(char* str, NslModule* parent, int recsize,int tx,int ty) : NslModule(str,parent), visual_field("visual_field", this, recsize, recsize), t5_2f("t5_2f", this,tx,ty), // Output Data Port. t5_2("t5_2", this,tx,ty), ps("ps",this), ps_th3("ps_th3",this), rs_th3("rs_th3",this), psm("psm",this), rsm("rsm",this), th("th",this) { } void PreyRec::initSys() { ps = 0; //rsm = 0.7; rsm = 0.5; rs_th3 = 0.2; } void PreyRec::simRun() { t5_2 = filter(visual_field,2); // INSTANTIATED Schema. if (t5_2.sum() >= 1) // orig >1 ps=0.7; // Confidence else ps=0; updateLabel(); triggerSchema(); } void PreyRec::updateLabel() { ps = ps - ps_th3*rs_th3 + psm*rsm ; if (ps<0) ps=0; ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void PreyRec::triggerSchema() { t5_2f = NSLramp(t5_2); // Output to other Schemas } //CambiosPredador PredatorRec::PredatorRec(char* str, NslModule* parent, int recsize,int tx,int ty) : NslModule(str,parent), visual_field("visual_field", this, recsize, recsize), th3f("th3f", this,tx,ty), // Output Data Port. th3("th3", this,tx,ty), ps("ps",this), th("th",this) { } void PredatorRec::initSys() { ps = 0; } void PredatorRec::simRun() { th3 = filter(visual_field,3); // INSTANTIATED Schema. if (th3.sum() >= 1) // orig >1 ps=0.99; // Confidence else //ps=0; ps = ps / 3; updateLabel(); triggerSchema(); } void PredatorRec::updateLabel() { ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void PredatorRec::triggerSchema() { th3f = NSLramp(th3); // Output to other Schemas } SorRec::SorRec(char* str, NslModule* parent, int recsize,int tx,int ty) : NslModule(str,parent), visual_field("visual_field", this, recsize, recsize), th10f("th10f", this,tx,ty), th10("th10", this,tx,ty), depthY("depthY", this), depthX("depthXY", this), edge("edge",this), // the edge of barrier ps("ps",this), th("th",this) { } void SorRec::initSys() { ps = 0; } void SorRec::simRun() { th10 = filter(visual_field, 1); // view far from barrier. if (th10.sum() >= 1) // orig >1 ps=0.9; // Confidence else ps=0; updateLabel(); triggerSchema(); } void SorRec::updateLabel() { ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void SorRec::triggerSchema() { if (ps.elem() > th.elem()) { printf("SO Recog \t"); th10f = NSLramp(th10); // Output to other Schemas } else th10f = 0; } PreyApproach::PreyApproach(char* str, NslModule* parent,int tx,int ty) : NslModule(str,parent), t5_2f("t5_2f", this,tx,ty), // Output Data Port. prey_hor_f("prey_hor_f", this, tx), // OUTPUT Data Port. prey_hor("prey_hor", this, tx), // Parcellation (1D). prey_hor_default("prey_hor_default", this, tx), prey_f("prey_f", this, tx, ty), // PREY ATRACTANT FIELD (2D). prey_norm("prey_norm", this), // Normalization t5_2_rf("t5_2_rf", this, 80, 80), // KERNEL for Prey Attractant Field. t5_2_erf("t5_2_erf", this, 80, 80), t5_2_erf_sig("t5_2_erf_sig", this), t5_2_erf_wgt("t5_2_erf_wgt", this), psfo("psfo",this), rsfo("rsfo",this), ps("ps",this), th("th",this) { } void PreyApproach::initSys() { rsfo = 0.9; // Prey & Prey Approach. ps = 0; gauss2D(t5_2_erf,t5_2_erf_sig); t5_2_rf = t5_2_erf_wgt * t5_2_erf; } void PreyApproach::simRun() { prey_f = t5_2_rf * t5_2f; //** 2D: newconv(t5_2_rf, T5_2); prey_hor = prey_f.reduce_row(); // Parcellation: horizontal comp. updateLabel(); triggerSchema(); } void PreyApproach::updateLabel() { ps = rsfo*psfo; // ps + ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void PreyApproach::triggerSchema() { float mx = NSLmax(prey_hor); if (mx != 0.0) { prey_hor_f = prey_hor/mx; // Normalize. // ASSERTED: Affect other schemas. } /*if (ps.elem() > th.elem()) { } else {*/ prey_hor_f = prey_hor_f * psfo; //} } PredatorAvoid::PredatorAvoid(char* str, NslModule* parent,int tx,int ty) : NslModule(str,parent), th3f("th3f", this,tx,ty), // Output Data Port. predator_hor_f("predator_hor_f", this, tx), // OUTPUT Data Port. predator_hor("predator_hor", this, tx), // Parcellation (1D). predator_hor_default("predator_hor_default", this, tx), predator_f("predator_f", this, tx, ty), // PREDATOR RETRACTANT FIELD (2D). predator_norm("predator_norm", this), // Normalization th3_rf("th3_rf", this, 80, 80), // KERNEL for Predator Attractant Field. th3_erf("th3_erf", this, 80, 80), th3_erf_sig("th3_erf_sig", this), th3_erf_wgt("th3_erf_wgt", this), ps_th3("ps_th3",this), rs_th3("rs_th3",this), ps("ps",this), th("th",this) { } void PredatorAvoid::initSys() { rs_th3 = 0.9; // Predator & Predator Avoid. ps = 0; gauss2D(th3_erf,th3_erf_sig); th3_rf = th3_erf_wgt * th3_erf; } void PredatorAvoid::simRun() { predator_f = th3_rf * th3f; //** 2D: newconv(t5_2_rf, T5_2); predator_hor = predator_f.reduce_row(); // Parcellation: horizontal comp. updateLabel(); triggerSchema(); } void PredatorAvoid::updateLabel() { ps = rs_th3*ps_th3; ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void PredatorAvoid::triggerSchema() { // if (ps.elem() > th.elem()) { // printf("Predator Appro \t"); float mx = NSLmax(predator_hor); if (mx != 0.0) { predator_hor_f = predator_hor/mx; // Normalize. // ASSERTED: Affect other schemas. } // } // else predator_hor_f = predator_hor_f * ps_th3; } SorAvoid::SorAvoid(char* str, NslModule* parent,int tectsize,int tx,int ty,int rrf) : NslModule(str,parent), th10f("th10f", this,tx,ty), th10_hor("th10_hor", this, tx), // Parcellation tm_irf("tm_irf", this, rrf*2), tm_rf("tm_rf", this, rrf*2), tm_irf_sig("tm_irf_sig", this), tm_irf_wgt("tm_irf_wgt", this), gaps("gaps", this, tectsize), // openings within obstacles. gapsf("gapsf", this, tectsize), psso("psso",this), rsso("rsso",this), ps("ps",this), th("th",this) { } void SorAvoid::initSys() { rsso = 1.0; // Obstacle & Obstacle Avoid ps = 0; // OBSTACLE AVOIDANCE KERNEL // tm_irf = tm_irf_wgt * gauss1D(tm_irf_sig, tm_irf_dia.elem()); // tm_rf = - tm_irf; gauss1D(tm_irf,tm_irf_sig); tm_rf = - tm_irf_wgt * tm_irf; // tm_rf_modul = tm_rf; } void SorAvoid::simRun() { th10_hor = th10f.reduce_row(); // Parcellation (from 2D to 1D). gaps = tm_rf * th10_hor; // Convolve with kernel. updateLabel(); triggerSchema(); } void SorAvoid::updateLabel() { ps = rsso*psso; // ps + ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void SorAvoid::triggerSchema() { if (ps.elem() > th.elem()) { printf("SOR avoid \t"); float mx = NSLmax(gaps*-1); if (mx != 0.0) gapsf = gaps/mx; // ASSERTED: Affect other schema } else gapsf = 0; } BumpAvoid::BumpAvoid(char* str, NslModule* parent,int tectsize) : NslModule(str,parent), baf("baf", this, tectsize), gaps("gaps", this, tectsize), gapsf("gapsf", this, tectsize), field_a("field_a", this), field_center("field_center", this), // field_side("field_side", this, tectsize), // last_orientation("last_orientation", this,tectsize), tune_tm("tune_tm", this), // TUNE GAPS field (avoid bumping). tune_tm_base("tune_tm_base", this), tune_tm_layer("tune_tm_layer", this, tectsize), safe_distance("safe_distance",this), depthY("depthY",this), depthX("depthX",this), ps("ps",this), psta("psta",this), rsta("rsta",this), th("th",this) { } void BumpAvoid::initSys() { rsta = 1.0; // Tactile & Bumping Avoid ps = 0; } void BumpAvoid::simRun() { // both should be larger than zero for no bump //Comentario después de predador /* if (depthY.elem() <= safe_distance.elem() && depthX.elem() <= safe_distance.elem()) //Bumping. ps = 0; else ps = -1.0; // Go down fast: -2.0 */ tuningField(); // TUNING the KERNEL updateLabel(); triggerSchema(); } void BumpAvoid::updateLabel() { ps = ps + rsta*psta; ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void BumpAvoid::tuningField() { if (tune_tm.elem() < 1.5) // saturate tune_tm. tune_tm = tune_tm + tune_tm_base; tune_tm_layer = tune_tm; tune_tm_layer = tune_tm_layer ^ NSLstep(-gaps); // Modulate only already active neurons. gaps = gaps - tune_tm_layer; // GAPS fiels gets tuned. gapsf = gaps; } void BumpAvoid::triggerSchema() { if (ps.elem() > th.elem()) { printf("Bump Avoid \t"); NSLoutput("Bump Avoid \t"); // every time it bumps it increases tune_tm until it reaches a saturation point. field_center = field_center +.3; // Tunes the B_A field by increasing eccentricity. // Generate Bumping Avoidance FIELD. baf.elem(floor(field_center.elem())) = field_a.elem(); } else baf = 0; } MHM::MHM(char* str, NslModule* parent, int tectsize) : NslModule(str,parent), baf("baf", this, tectsize), gapsf("gapsf", this, tectsize), prey_hor_f("prey_hor_f", this, tectsize), ip("ip", this, tectsize), // PERCEPTUAL INTEGRATION. mhm("mhm", this, tectsize), in("in", this, tectsize), mhm_hat("mhm_hat", this, tectsize), // Predictive MHM. new_relation("new_relation", this), // temporary: new_relation learning("learning", this), // temporary: new_relation field_a("field_a", this), field_center("field_center", this), d_mhm("d_mhm", this), // Coherence over MHM. d_norm("d_norm", this), ps("ps",this), pspa("pspa",this), rspa("rspa",this), psoa("psoa",this), predator_hor_f("predator_hor_f", this, tectsize), psps("psps",this), rsps("rsps",this), rsoa("rsoa",this), psm("psm",this), rsm("rsm",this) { } void MHM::initSys() { rspa = 1.0; rsoa = 1.0; rsps = 1.0; rsm = 0.5; ps = 0; if (learning.get_data() == 1) new_relation = 1; // start learning immediately } void MHM::simRun() { // Dynamics of Motor Heading Map (MHM): integrating several fields. // New fields can also be added while learning. if (new_relation.elem() == 1) { baf.elem(floor(field_center.elem())) = field_a.elem(); in = baf; // New Field "inserted" } else in = 0; // reset input (cf. antidromic) mhm_hat = mhm; // Predictive MHM. //Escaling if ( pspa.elem() > psps.elem() ) prey_hor_f = prey_hor_f * 1.3; else predator_hor_f = predator_hor_f * 1.3; ip = gapsf + prey_hor_f + predator_hor_f; //+ baf; // (AW0501) // Fields over MHM. mhm = ip; coherence(); updateLabel(); } void MHM::updateLabel() { ps = rspa*pspa + rsps*psps + rsm*psm; //Prey-Predator ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void MHM::coherence() { // Learning Dynamics: it detects an incoherence and hence a trigger // for a new schema. d_mhm = 0.03 * dist(mhm, mhm_hat); // d_mhm.nslPrint(); if (new_relation.elem() == 0 && d_mhm.elem() > d_norm.elem()) // Incoherence. new_relation.elem() = 1; // AW2000 if (new_relation.elem() == 1 && d_mhm.elem() < d_norm.elem()) // Reset. new_relation.elem() = -1; // new_relation.nslPrint(); } WTA::WTA(char* str, NslModule* parent, int tectsize) : NslModule(str,parent), mhm("mhm", this, tectsize), wta_mhm("wta_mhm", this,tectsize) { } void WTA::simRun() { wta_mhm = maxim(mhm); } Xform::Xform(char* str, NslModule* parent, int tectsize) : NslModule(str,parent), wta_mhm("wta_mhm", this, tectsize), // Predictive MHM. angle_position("angle_position", this, tectsize), gradient("gradient", this, tectsize), // Gradient of weights. angle("angle", this), // Heading (target) angle. ps("ps",this), pspa("pspa",this), psps("psps",this), rsps("rsps",this), rspa("rspa",this), psoa("psoa",this), rsoa("rsoa",this), psm("psm",this), rsm("rsm",this), th("th",this) { } void Xform::initSys() { rspa = 1.0; rsoa = 1.0; rsm = 0.5; rsps = 1.0; ps = 0; } void Xform::simRun() { // Sensory motor Transformation from retinotopic coordinates to population coding. int i; wta_mhm.max(i); angle = 0; // position displacement in x if (i != 0) angle = i - wta_mhm.get_imax()/2; angle.nslPrint(); updateLabel(); } void Xform::updateLabel() { ps = rspa*pspa + rsps*psps + rsm*psm; //Prey-Pred ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } Jump::Jump(char* str, NslModule* parent) : NslModule(str,parent), depthY("depthY", this), // Heading (target) angle. step("step", this), out("out", this), ps("ps",this), psfo("psfo",this), rsfo("rsfo",this), ps_th3("ps_th3",this), rs_th3("rs_th3",this), psta("psta",this), rsta("rsta",this), psdp("psdp",this), rsdp("rsdp",this), psmhm("psmhm",this), rsmhm("rsmhm",this), psbk("psbk",this), rsbk("rsbk",this), th("th",this), psm("psm",this), rsm("rsm",this) { } void Jump::initSys() { rsfo = 0.4; rs_th3 = 1.0; rsmhm = 0.2; rsm = 0.6; rsta = -1.0; rsdp = 1.0; rsbk = -1.0; ps = 0; } void Jump::simRun() { updateLabel(); triggerSchema(); } void Jump::updateLabel() { ps = rsfo*psfo + rs_th3*ps_th3 + rsm*psm + rsmhm*psmhm; //prey-pred NSLoutput("\npsfo:",psfo.elem()); NSLoutput("ps_th3:",ps_th3.elem()); NSLoutput("psmhm:", psmhm.elem()); NSLoutput("psJump:", ps.elem()); ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void Jump::triggerSchema() { out = 0; if (ps.elem() > th.elem()) { out = step; NSLoutput("\n\t\t--Jump-- "); out.nslPrint(); } } Turn::Turn(char* str, NslModule* parent) : NslModule(str,parent), angle("angle", this), // Heading (target) angle. out("out", this), ps("ps",this), psfo("psfo",this), ps_th3("ps_th3",this), psm("psm",this), psso("psso",this), rsso("rsso",this), psta("psta",this), rsta("rsta",this), psba("psba",this), rsba("rsba",this), psmhm("psmhm",this), rsmhm("rsmhm",this), psx("psx",this), rsx("rsx",this), rsm("rsm",this), rsfo("rsfo",this), rs_th3("rs_th3",this), th("th",this) { } void Turn::initSys() { rsfo = 0.6; rs_th3 = 1.0; rsmhm = 0.2; rsm = 0.4; rsso = 0.5; rsta = 1.0; rsba = 0.5; rsx = 0.5; ps = 0; } void Turn::simRun() { updateLabel(); triggerSchema(); } void Turn::updateLabel() { static float pi = 3.1415926; ps = rsfo*psfo + rs_th3*ps_th3 + psmhm*rsmhm + psm*rsm; //prey-pred if (psfo > ps_th3) //Ataque de presa { //Calcula ángulo de giro /* if (angle.elem()<0) //Estímulo a la izquierda { //primera aprox: calcula el ángulo al que está el objetivo turn_angle = (angle.elem()+40) * (pi/2) / 40; //segunda aprox: el ángulo complementario es lo que tendrá que girar turn_angle = pi/2 - turn_angle; } else //Estímulo a la derecha { turn_angle = ( angle.elem() * pi / 80 ) * -1; } */ turn_angle = ( angle.elem() * pi / 80 ) * -1; // turn_angle = turn_angle / 2; //Estabiliza los giros } else if (psfo < ps_th3) //Huída de predador { /*if (angle.elem()<0) //Por la izquierda { turn_angle = (angle.elem()+40) * (pi/2) / 40; turn_angle = turn_angle - pi/2 ; } else { turn_angle = angle.elem() * pi / 80 ; turn_angle = pi/2 - turn_angle ; } */ if (angle.elem()>0) //Por la derecha { turn_angle = (40-angle.elem())*pi / 80 ; } else turn_angle = ( ( angle.elem() + 40 ) * pi / 80 ) * -1; } else //Si el estímulo de presa y pred es igual (muy motivado) permanece inomvil { ps = 0; } ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void Turn::triggerSchema() { out = 0; if (ps.elem() > th.elem()) { ps = 0; // -1.0; // Reset out = turn_angle; //out = angle; NSLoutput("\n\t\t--Turn: ", turn_angle*180/3.1416); NSLoutput("grados--"); } else ps=0; } Backward::Backward(char* str, NslModule* parent) : NslModule(str,parent), step("step", this), out("out", this), ps("ps",this), psta("psta",this), rsta("rsta",this), psba("psba",this), rsba("rsba",this), psfw("psfw",this), rsfw("rsfw",this), th("th",this) { } void Backward::initSys() { rsta = 1.0; rsba = 1.0; rsfw = -1.0; ps = 0; } void Backward::simRun() { updateLabel(); triggerSchema(); } void Backward::updateLabel() { ps = ps + rsta*psta + rsba*psba + rsfw*psfw; ps = NSLsigma(ps,(float)-1.0,(float)1.0,(float)-1.0,(float)1.0); } void Backward::triggerSchema() { out = 0; /* if (ps.elem() > th.elem()) { ps = -1.0; // Reset out = step; NSLoutput("backward: "); out.nslPrint(); } */} // Frog Frog::Frog(char* str, NslModule* parent,int wx,int wz,int recsize,int tectsize, int tx,int ty,int rrf) : NslModule(str,parent), visual("visual",this,wx,wz,recsize), depth("depth",this), tactile("tactile",this), feedMotivation("feedMotivation",this), preyRec("preyRec",this,recsize,tx,ty), sorRec("sorRec",this,recsize,tx,ty), predatorRec("predatorRec",this,recsize,tx,ty), //CambiosPred preyApproach("preyApproach",this,tx,ty), sorAvoid("sorAvoid",this,tectsize,tx,ty,rrf), predatorAvoid("predatorAvoid",this,tx,ty), //CambiosPred bumpAvoid("bumpAvoid",this,tectsize), mhm("mhm",this,tectsize), wta("wta",this,tectsize), xform("xform",this,tectsize), jump("jump",this), turn("turn",this), backward("backward",this), worldXZ("worldXZ",this, wx,wz), xf("xf", this), yf("yf", this), zf("zf", this), xw("xw", this), yw("yw", this), // zf("zf", this), //CambiosPred xp("xp", this), yp("yp", this), // zf("zf", this), //FinCambiosPred xb_end("xb_end", this), yb("yb", this), zb("zb", this), xb_init("xb_init", this), jump_step("jump_step", this), backward_step("backward_step", this), wangle("wangle", this), angle("angle", this), date ("date", this), dayhour ("dayhour", this), hunger("hunger",this), prey_ate("prey_ate",this), multiprey("multiprey",this) { makeConn(); } void Frog::makeConn() { nslRelabel(worldXZ,visual.worldXZ); nslRelabel(xb_init,depth.xb_init); nslRelabel(xb_end,depth.xb_end); nslRelabel(yb,depth.yb); nslRelabel(turn.out,angle); nslRelabel(jump.out,jump_step); nslRelabel(backward.out,backward_step); nslRelabel(xw,depth.xw); nslRelabel(yw,depth.yw); //FeedMotivation nslRelabel(date,feedMotivation.date); nslRelabel(dayhour,feedMotivation.dayhour); nslRelabel(hunger,feedMotivation.hunger); nslRelabel(prey_ate,feedMotivation.prey_ate); //EndFeedMotivation //CambiosPred nslRelabel(xp,depth.xp); nslRelabel(yp,depth.yp); nslConnect(xf,visual.xf); nslConnect(zf,visual.zf); nslConnect(xf,depth.xf); nslConnect(yf,depth.yf); //FeedMotivation /*nslConnect(hunger,feedMotivation.hunger); nslConnect(date,feedMotivation.date); nslConnect(dayhour,feedMotivation.dayhour); *///EndFeedMotivation nslConnect(visual.visual_field,preyRec.visual_field); nslConnect(visual.visual_field,sorRec.visual_field); nslConnect(depth.ps,jump.psdp); nslConnect(depth.depthY,tactile.depthY); nslConnect(depth.depthX,tactile.depthX); nslConnect(depth.depthY,sorRec.depthY); nslConnect(depth.depthX,sorRec.depthX); nslConnect(depth.depthY,bumpAvoid.depthY); nslConnect(depth.depthX,bumpAvoid.depthX); nslConnect(depth.depthY,jump.depthY); nslConnect(tactile.ps,bumpAvoid.psta); nslConnect(tactile.ps,jump.psta); nslConnect(tactile.ps,turn.psta); nslConnect(tactile.ps,backward.psta); //FeedMotivation nslConnect(feedMotivation.ps,preyRec.psm); nslConnect(feedMotivation.ps,mhm.psm); nslConnect(feedMotivation.ps,xform.psm); nslConnect(feedMotivation.ps,jump.psm); nslConnect(feedMotivation.ps,turn.psm); //EndFeedMotivation nslConnect(preyRec.t5_2f,preyApproach.t5_2f); nslConnect(preyRec.ps,preyApproach.psfo); nslConnect(preyRec.ps,jump.psfo); nslConnect(preyRec.ps,turn.psfo); nslConnect(sorRec.th10f,sorAvoid.th10f); nslConnect(sorRec.ps,sorAvoid.psso); nslConnect(sorRec.ps,turn.psso); nslConnect(preyApproach.prey_hor_f,mhm.prey_hor_f); nslConnect(preyApproach.ps,mhm.pspa); nslConnect(preyApproach.ps,xform.pspa); nslConnect(sorAvoid.gapsf,bumpAvoid.gaps); nslConnect(sorAvoid.ps,mhm.psoa); nslConnect(sorAvoid.ps,xform.psoa); nslConnect(bumpAvoid.baf,mhm.baf); nslConnect(bumpAvoid.ps,turn.psba); nslConnect(bumpAvoid.ps,backward.psba); nslConnect(bumpAvoid.gapsf,mhm.gapsf); nslConnect(mhm.ps,jump.psmhm); nslConnect(mhm.ps,turn.psmhm); nslConnect(mhm.mhm,wta.mhm); nslConnect(wta.wta_mhm,xform.wta_mhm); nslConnect(xform.angle,turn.angle); nslConnect(xform.ps,turn.psx); nslConnect(jump.ps,backward.psfw); nslConnect(backward.ps,jump.psbk); //CambiosPred nslConnect(visual.visual_field,predatorRec.visual_field); nslConnect(predatorRec.ps,preyRec.ps_th3); nslConnect(predatorRec.th3f,predatorAvoid.th3f); nslConnect(predatorRec.ps,predatorAvoid.ps_th3); nslConnect(predatorRec.ps,jump.ps_th3); nslConnect(predatorRec.ps,turn.ps_th3); nslConnect(predatorAvoid.predator_hor_f,mhm.predator_hor_f); nslConnect(predatorAvoid.ps,mhm.psps); nslConnect(predatorAvoid.ps,xform.psps); //FinCambiosPred } void Frog::initSys() { prey_ate = 0; //the actual prey catching } void Frog::endRun() { // int obj_type = 0; // 0 - prey only, 1 - predator double pi = 3.1415926535; double edge = 5; float yfant; static int sigue = 1; static float tangle = 0; //pi/2; double disp = jump_step.elem() - backward_step.elem(); // overall displacement NSLoutput("\ndisp: ", disp); NSLoutput("\n\t\t--turn.out.elem(): ", turn.out.elem()); //Calcula la nueva dirección tangle = turn.out.elem() + tangle; if (tangle < -pi/2) tangle = -pi/2; if (tangle > pi/2) tangle = pi/2; // se utiliza lo siguiente para el nuevo ciclo de computo wangle = tangle*180/pi; NSLoutput("\n\t\t--tangle: ", tangle); NSLoutput("\n\t\t--wangle: ", wangle.elem()); float t2 = tangle; //prueba //wangle = -30; double dispx2 = disp*sin(t2); double dispy2 = disp*cos(t2); if (dispy2<0) dispy2 = 0; //Verifica si la presa fue deborada if (prey_ate==1) { prey_ate = 0; if (multiprey==0) sigue = 0; } yfant = yf.elem(); // Valor anterior en y //Nueva posición xf = xf - dispx2; yf = yf - dispy2; //Verifica que la presa esté a una distancia (edge) asequible if ( ( ( (yf <= yw) && (yf + edge >= yw) ) || //Limite superior ( (yf >= yw) && (yf - edge <= yw) ) || //Limite inferior ( (yfant >= yw) && (yf <= yw ) ) //Se paso en el salto ) && ( (xf <= xw) && (xf.elem() + edge >= xw.elem() ) || //Limite por la izquierda (xf >= xw) && (xf.elem() - edge <= xw.elem() ) ) //Limite por la derecha ) { NSLoutput("\n\t\t--Ate Worm--"); prey_ate = 1; yf = yw - 1; xf = xw ; } /* if ( (obj_type == 0) && (sigue) ) //Persigue presa { NSLoutput("xw: ",xw.elem()); NSLoutput("yw: ",yw.elem()); if (prey_ate==1) { // yf = yw ; // xf = xw ; prey_ate = 0; if (multiprey==0) sigue = 0; //else // prey_ate = 0; // nslBreakEpoch(); // return; } if ( (xf.elem() <= xw.elem() ) && ( yf.elem() >= yw.elem() ) ) //Cuadrante 1 { // xf = xf + dispx; // yf = yf - dispy; xf = xf - dispx2; yf = yf - dispy2; if ( (xf>=xw) && (yf<=yw) ) //Verifica no pasarse en el salto { xf = xw; yf = yw; // NSLoutput("\n\t\t--Ataque--"); } } else if ( (xf.elem() >= xw.elem() ) && ( yf.elem() >= yw.elem() ) ) //Cuadrante 2 { //xf = xf - dispx; //yf = yf + dispy; //dispy es negativo xf = xf - dispx2; yf = yf - dispy2; if ( (xf<=xw) && (yf<=yw) ) //Verifica no pasarse en el salto { xf = xw; yf = yw; } } //Verifica si está en una distancia de captura if ((yf.elem() > yw.elem() - edge) && (yf.elem() < yw.elem() + edge) && (xf.elem() > xw.elem() - edge) && (xf.elem() < xw.elem() + edge) && (sigue) ) { NSLoutput("\n\t\t--Ate Worm--"); prey_ate = 1; } x = xf.elem() - xw.elem(); y = yf.elem() - yw.elem(); } else if (obj_type == 1) //Con predador { NSLoutput("xp: ",xp.elem()); NSLoutput("yp: ",yp.elem()); if (prey_ate==1) { // yf = yw ; // xf = xw ; prey_ate = 0; if (multiprey==0) sigue = 0; //else // prey_ate = 0; // nslBreakEpoch(); // return; } //Salta a 90o en sentido contrario de donde apareció el predador if (t<0) t=t*-1; if (t == 0) t = 0.25; else if (t>pi/2) t = t- pi/2; else t = pi/2 + t; double dispx = disp*sin(t); // fabs(disp)*sin(t); double dispy = disp*cos(t); // double dispx2 = disp*cos(t2); // double dispy2 = disp*sin(t2); xf = xf - dispx2; yf = yf - dispy2; if ((yf.elem() > yp.elem() - edge) && (yf.elem() < yp.elem() + edge) && (xf.elem() > xp.elem() - edge) && (xf.elem() < xp.elem() + edge)) { NSLoutput("\n\t\t--Ate Frog--"); yf = yp; xf = xp; nslBreakEpoch(); return; } x = xf.elem() - xp.elem(); y = yf.elem() - yp.elem(); } // tangle = turn.out.elem(); //tangle = tangle - 0.5; */ } Preypred::Preypred() : NslModel("Preypred"), world("world",this,100,100,100), // world("world",this,200,200,200), frog("frog",this,100,100,80,80,80,80,40), // frog("frog",this,200,200,80,80,80,80,40) prey("prey",this), //CambiosPred predator("predator",this) //FinCambiosPred { makeConn(); } void Preypred::makeConn() { nslConnect(world.worldXZ,frog.worldXZ); nslConnect(world.xb_init,frog.xb_init); nslConnect(world.xb_end,frog.xb_end); nslConnect(world.yb,frog.yb); nslConnect(world.zb,frog.zb); //FeedMotivation nslConnect(world.date,frog.date); nslConnect(world.dayhour,frog.dayhour); //EndFeedMotivation nslConnect(frog.xf,world.xf); nslConnect(frog.yf,world.yf); nslConnect(frog.zf,world.zf); nslConnect(frog.wangle,world.wangle); nslConnect(frog.prey_ate,world.prey_ate); nslConnect(world.prey_alive,prey.prey_alive); //AW99 nslConnect(prey.xw,frog.xw); nslConnect(prey.yw,frog.yw); nslConnect(prey.xw,world.xw); nslConnect(prey.yw,world.yw); nslConnect(prey.zw,world.zw); nslConnect(prey.multiprey,frog.multiprey); //CambiosPred nslConnect(predator.xp,frog.xp); nslConnect(predator.yp,frog.yp); nslConnect(predator.xp,world.xp); nslConnect(predator.yp,world.yp); nslConnect(predator.zp,world.zp); //FinCambiosPred } AslSchemaModel _Preypred("Preypred");