GAindividual mutateNetwork(GAindividual p) { ReactionNetwork * r = (ReactionNetwork*)(p); int n0, n1, i; double * iv; int * fixed; GAMutateFnc f; GAindividual net; if (!r || (r->type < 0) || (r->type > NUMBER_OF_NETWORK_TYPES)) return p; f = mutateFunctions[r->type]; n0 = getNumSpecies(r); if (f) { net = f(r->network); r->network = net; n1 = getNumSpecies(r); if (n0 != n1) { iv = r->initialValues; fixed = r->fixed; r->initialValues = (double*)malloc(n1 * sizeof(double)); r->fixed = (int*)malloc(n1 * sizeof(double)); for (i=0; i < n0 && i < n1; ++i) { r->initialValues[i] = iv[i]; r->fixed[i] = fixed[i]; } for (i=n0; i < n1; ++i) { r->initialValues[i] = AVG_INIT_VALUES * mtrand(); r->fixed[i] = 0; } free(iv); } if (mtrand() < MUTATE_INIT_VALUE_PROB) { r->initialValues[ (int)(mtrand() * getNumSpecies(r)) ] *= 2.0 * mtrand(); } return r; } return 0; }
double * simulateNetworkStochastically( GAindividual individual, double time, int* sz) { ReactionNetwork * r = (ReactionNetwork*)individual; StoichiometryFunction stoic; double * N; PropensityFunction rate; double * y = 0, * iv; int species = 0, reactions = 0; void * p = 0; if (!r || (r->type > NUMBER_OF_NETWORK_TYPES)) return 0; iv = r->initialValues; rate = rateFunctions[r->type]; p = r->network; species = getNumSpecies(r); reactions = getNumReactions(r); if (species == 0 || reactions == 0) return 0; N = getStoichiometryMatrix(individual); y = SSA(species, reactions, N, rate, iv, 0.0, time, 1000000, sz, p); free(N); return y; }
double * networkSteadyState( GAindividual individual ) { ReactionNetwork * r = (ReactionNetwork*)individual; StoichiometryFunction stoic; double * N; PropensityFunction rate; double * y = 0; void * p = 0; int species = 0, reactions = 0; double* iv; if (!r || (r->type > NUMBER_OF_NETWORK_TYPES)) return 0; iv = r->initialValues; rate = rateFunctions[r->type]; p = r->network; species = getNumSpecies(r); reactions = getNumReactions(r); if (species == 0 || reactions == 0) return 0; N = getStoichiometryMatrix(individual); y = steadyState2(species, reactions, N, rate, iv, p, SS_FUNC_ERROR_TOLERANCE,SS_FUNC_MAX_TIME,SS_FUNC_DELTA_TIME); free(N); return y; }
double * simulateNetworkODE( GAindividual individual, double time, double dt) { ReactionNetwork * r = (ReactionNetwork*)individual; StoichiometryFunction stoic; PropensityFunction rate; double * N, * y; int species = 0, reactions = 0; void * p = 0; double* iv; if (!r || (r->type > NUMBER_OF_NETWORK_TYPES)) return 0; iv = r->initialValues; rate = rateFunctions[r->type]; p = r->network; species = getNumSpecies(r); reactions = getNumReactions(r); if (species == 0 || reactions == 0) return 0; N = getStoichiometryMatrix(individual); y = ODEsim2(species, reactions, N, rate, iv, 0, time, dt, p); free(N); return y; }
void setFixed(GAindividual p,int i, int f) { ReactionNetwork * r = (ReactionNetwork*)(p); if (!p || getNumSpecies(r) <= i) return; r->fixed[i] = (f>0); }
SimpleStochasticSimulator::SimpleStochasticSimulator( std::string rulesfile, std::string modelfile ) : SimpleSimulator() { loadRules(rulesfile); loadModel(modelfile); std::cout << "Prior to initialization there are " << getNumSpecies() << " species and " << getNumRxns() << " reactions" << std::endl; recordNewReactions(); initialize(); std::cout << "After initialization there are " << getNumSpecies() << " species and " << getNumRxns() << " reactions" << std::endl; }
/* converting to l1 any hasOnlySubstanceUnits attributes should be removed */ void Model::removeHasOnlySubstanceUnits() { for (unsigned int i = 0; i < getNumSpecies(); i++) { getSpecies(i)->setHasOnlySubstanceUnits(false); } }
/* fitness that calculates the coefficient of variation (CV) */ double fitness(GAindividual p) { int i,r,n,sz; double f, sd, dt, time, * y, mXY = 0,mX = 0, mY = 0, mX2 = 0, mY2 = 0; n = getNumSpecies(p); r = getNumReactions(p); time = 500.0; y = simulateNetworkStochastically(p,time,&sz); //stochastic simulation f = 0; if (y != 0) //compute the variance { mXY = mX = mY = mX2 = mY2 = 0; for (i = 0; i < (sz-1); ++i) { dt = getValue(y,n+1,i+1,0) - getValue(y,n+1,i,0); mX += getValue(y,n+1,i,1) * dt; mX2 += getValue(y,n+1,i,1)*getValue(y,n+1,i,1)*dt; } mX /= time; mX2 /= time; sd = sqrt(mX2 - mX*mX); //standard deviation if (sd <= 0 || mX <= 0 || mX > 5.0) f = 0.0; else f = mX / sd; // CV = sdev/mean, but the fitness = 1/CV = mean/sdev free(y); } if(getNumSpecies(p) > 5) //disallow large networks f = 0.0; return (f); }
void setInitialValues( GAindividual x, double * values) { int i,n; ReactionNetwork * rnet = (ReactionNetwork*)x; if (!rnet || !values) return; n = getNumSpecies(rnet); for (i=0; i < n; ++i) rnet->initialValues[i] = values[i]; }
/* converting to l1 any metaid attributes should be removed */ void Model::removeMetaId() { unsigned int n, i; unsetMetaId(); for (n = 0; n < getNumUnitDefinitions(); n++) { getUnitDefinition(n)->unsetMetaId(); for (i = 0; i < getUnitDefinition(n)->getNumUnits(); i++) { getUnitDefinition(n)->getUnit(i)->unsetMetaId(); } } for (n = 0; n < getNumCompartments(); n++) { getCompartment(n)->unsetMetaId(); } for (n = 0; n < getNumSpecies(); n++) { getSpecies(n)->unsetMetaId(); } for (n = 0; n < getNumParameters(); n++) { getParameter(n)->unsetMetaId(); } for (n = 0; n < getNumRules(); n++) { getRule(n)->unsetMetaId(); } for (n = 0; n < getNumReactions(); n++) { getReaction(n)->unsetMetaId(); for (i = 0; i < getReaction(n)->getNumReactants(); i++) { getReaction(n)->getReactant(i)->unsetMetaId(); } for (i = 0; i < getReaction(n)->getNumProducts(); i++) { getReaction(n)->getProduct(i)->unsetMetaId(); } if (getReaction(n)->isSetKineticLaw()) { getReaction(n)->getKineticLaw()->unsetMetaId(); } } }
//----------------------------------------------------------------------------- // Function : Region::getNumIntVars // // Purpose : Return the number of internal variables (solution vars) // this region is adding to the system. // // Special Notes : The number of internal variables is comprised of the // number of species in the reaction network. // // Scope : public // // Creator : Tom Russo, SNL, Electrical and Microsystems Modeling // Creation Date : 10/24/06 //----------------------------------------------------------------------------- int Region::getNumIntVars() { int hInt=0; int numSpeciesVars=0; if (!(regData.doNothing)) { numSpeciesVars=getNumSpecies(); } return (numSpeciesVars+hInt); }
void printNetwork(FILE * stream, GAindividual individual) { ReactionNetwork * r = (ReactionNetwork*)individual; PrintNetworkFunction f; int n,i,fix; if (!r || (r->type < 0) || (r->type > NUMBER_OF_NETWORK_TYPES)) return; n = getNumSpecies(r); f = printNetworkFunctions[r->type]; if (r->fixed) { fix = 0; for (i=0; i < n; ++i) { if (r->fixed[i]) { fix = i+1; break; } } if (fix) { fprintf(stream, "const s%i",fix); for (i=0; i < n; ++i) { if (r->fixed[i]) fprintf(stream, ", s%i",i+1); } fprintf(stream, "\n"); } } f(stream,r->network); if (r->initialValues) { fprintf(stream,"\n"); for (i=0; i < n; ++i) { fprintf(stream,"s%i = %lf;\n",i+1,r->initialValues[i]); } } }
GAindividual cloneNetwork(GAindividual p) { ReactionNetwork * r = (ReactionNetwork*)(p); ReactionNetwork * r2; int i,j; if (!r || (r->type < 0) || (r->type > NUMBER_OF_NETWORK_TYPES)) return p; r2 = (ReactionNetwork*) malloc(sizeof(ReactionNetwork)); i = 0; /*if (r->parents) while (r->parents[i]) ++i; r2->parents = 0; if (TRACK_NETWORK_PARENTS && (i > 0)) { r2->parents = (int*) malloc((i+1)*sizeof(int)); for (j=0; j < i; ++j) r2->parents[j] = r->parents[j]; r2->parents[i] = 0; } r2->id = r->id; */ r2->type = r->type; r2->network = cloneFunctions[r->type](r->network); j = getNumSpecies(r2); r2->initialValues = (double*)malloc(j * sizeof(double)); r2->fixed = (int*)malloc(j * sizeof(double)); for (i=0; i < j; ++i) { r2->initialValues[i] = r->initialValues[i]; r2->fixed[i] = 0; } return r2; }
double* getStoichiometryMatrix(GAindividual individual) { ReactionNetwork * r = (ReactionNetwork*)individual; StoichiometryFunction stoic; double * N; int i=0, j=0, i2=0, numFixed = 0, n = getNumSpecies(individual), m = getNumReactions(individual); if (!r || (r->type < 0) || (r->type > NUMBER_OF_NETWORK_TYPES)) return 0; stoic = stoicFunctions[r->type]; N = stoic(r->network); for (i=0; i < n; ++i) { if (r->fixed[i] == 1) { for (j=0; j < m; ++j) getValue(N,m,i,j) = 0.0; } } return N; }
void Model::removeDuplicateTopLevelAnnotations() { unsigned int i, n; this->removeDuplicateAnnotations(); if (getNumFunctionDefinitions() > 0) { getListOfFunctionDefinitions()->removeDuplicateAnnotations(); for (i = 0; i < getNumFunctionDefinitions(); i++) { getFunctionDefinition(i)->removeDuplicateAnnotations(); } } if (getNumUnitDefinitions() > 0) { getListOfUnitDefinitions()->removeDuplicateAnnotations(); for (i = 0; i < getNumUnitDefinitions(); i++) { getUnitDefinition(i)->removeDuplicateAnnotations(); getUnitDefinition(i)->getListOfUnits()->removeDuplicateAnnotations(); for (n = 0; n < getUnitDefinition(i)->getNumUnits(); n++) { getUnitDefinition(i)->getUnit(n)->removeDuplicateAnnotations(); } } } if (getNumCompartmentTypes() > 0) { getListOfCompartmentTypes()->removeDuplicateAnnotations(); for (i = 0; i < getNumCompartmentTypes(); i++) { getCompartmentType(i)->removeDuplicateAnnotations(); } } if (getNumSpeciesTypes() > 0) { getListOfSpeciesTypes()->removeDuplicateAnnotations(); for (i = 0; i < getNumSpeciesTypes(); i++) { getSpeciesType(i)->removeDuplicateAnnotations(); } } if (getNumCompartments() > 0) { getListOfCompartments()->removeDuplicateAnnotations(); for (i = 0; i < getNumCompartments(); i++) { getCompartment(i)->removeDuplicateAnnotations(); } } if (getNumSpecies() > 0) { getListOfSpecies()->removeDuplicateAnnotations(); for (i = 0; i < getNumSpecies(); i++) { getSpecies(i)->removeDuplicateAnnotations(); } } if (getNumParameters() > 0) { getListOfParameters()->removeDuplicateAnnotations(); for (i = 0; i < getNumParameters(); i++) { getParameter(i)->removeDuplicateAnnotations(); } } if (getNumInitialAssignments() > 0) { getListOfInitialAssignments()->removeDuplicateAnnotations(); for (i = 0; i < getNumInitialAssignments(); i++) { getInitialAssignment(i)->removeDuplicateAnnotations(); } } if (getNumConstraints() > 0) { getListOfConstraints()->removeDuplicateAnnotations(); for (i = 0; i < getNumConstraints(); i++) { getConstraint(i)->removeDuplicateAnnotations(); } } if (getNumRules() > 0) { getListOfRules()->removeDuplicateAnnotations(); for (i = 0; i < getNumRules(); i++) { getRule(i)->removeDuplicateAnnotations(); } } if (getNumReactions() > 0) { getListOfReactions()->removeDuplicateAnnotations(); for (i = 0; i < getNumReactions(); i++) { Reaction * r = getReaction(i); r->removeDuplicateAnnotations(); if (r->getNumReactants() > 0) { r->getListOfReactants()->removeDuplicateAnnotations(); for (n = 0; n < r->getNumReactants(); n++) { r->getReactant(n)->removeDuplicateAnnotations(); } } if (r->getNumProducts() > 0) { r->getListOfProducts()->removeDuplicateAnnotations(); for (n = 0; n < r->getNumProducts(); n++) { r->getProduct(n)->removeDuplicateAnnotations(); } } if (r->getNumModifiers() > 0) { r->getListOfModifiers()->removeDuplicateAnnotations(); for (n = 0; n < r->getNumModifiers(); n++) { r->getModifier(n)->removeDuplicateAnnotations(); } } if (r->isSetKineticLaw()) { r->getKineticLaw()->removeDuplicateAnnotations(); if (r->getKineticLaw()->getNumParameters() > 0) { r->getKineticLaw()->getListOfParameters() ->removeDuplicateAnnotations(); for (n = 0; n < r->getKineticLaw()->getNumParameters(); n++) { r->getKineticLaw()->getParameter(n)->removeDuplicateAnnotations(); } } } } } if (getNumEvents() > 0) { getListOfEvents()->removeDuplicateAnnotations(); for (i = 0; i < getNumEvents(); i++) { getEvent(i)->removeDuplicateAnnotations(); if (getEvent(i)->getNumEventAssignments() > 0) { getEvent(i)->getListOfEventAssignments()->removeDuplicateAnnotations(); for (n = 0; n < getEvent(i)->getNumEventAssignments(); n++) { getEvent(i)->getEventAssignment(n)->removeDuplicateAnnotations(); } } } } }
void Model::removeSBOTermsNotInL2V2(bool strict) { unsigned int n, i; if (strict == true) { for (n = 0; n < getNumUnitDefinitions(); n++) { getUnitDefinition(n)->unsetSBOTerm(); for (i = 0; i < getUnitDefinition(n)->getNumUnits(); i++) { getUnitDefinition(n)->getUnit(i)->unsetSBOTerm(); } } for (n = 0; n < getNumCompartments(); n++) { getCompartment(n)->unsetSBOTerm(); } for (n = 0; n < getNumSpecies(); n++) { getSpecies(n)->unsetSBOTerm(); } for (n = 0; n < getNumCompartmentTypes(); n++) { getCompartmentType(n)->unsetSBOTerm(); } for (n = 0; n < getNumSpeciesTypes(); n++) { getSpeciesType(n)->unsetSBOTerm(); } for (n = 0; n < getNumReactions(); n++) { for (i = 0; i < getReaction(n)->getNumReactants(); i++) { if (getReaction(n)->getReactant(i)->isSetStoichiometryMath()) { getReaction(n)->getReactant(i)->getStoichiometryMath()->unsetSBOTerm(); } } for (i = 0; i < getReaction(n)->getNumProducts(); i++) { if (getReaction(n)->getProduct(i)->isSetStoichiometryMath()) { getReaction(n)->getProduct(i)->getStoichiometryMath()->unsetSBOTerm(); } } } for (n = 0; n < getNumEvents(); n++) { if (getEvent(n)->isSetTrigger()) { getEvent(n)->getTrigger()->unsetSBOTerm(); } if (getEvent(n)->isSetDelay()) { getEvent(n)->getDelay()->unsetSBOTerm(); } } } }
void Model::removeSBOTerms(bool strict) { unsigned int n, i; if (strict == true) { unsetSBOTerm(); for (n = 0; n < getNumUnitDefinitions(); n++) { getUnitDefinition(n)->unsetSBOTerm(); for (i = 0; i < getUnitDefinition(n)->getNumUnits(); i++) { getUnitDefinition(n)->getUnit(i)->unsetSBOTerm(); } } for (n = 0; n < getNumCompartments(); n++) { getCompartment(n)->unsetSBOTerm(); } for (n = 0; n < getNumSpecies(); n++) { getSpecies(n)->unsetSBOTerm(); } for (n = 0; n < getNumParameters(); n++) { getParameter(n)->unsetSBOTerm(); } for (n = 0; n < getNumRules(); n++) { getRule(n)->unsetSBOTerm(); } for (n = 0; n < getNumReactions(); n++) { getReaction(n)->unsetSBOTerm(); for (i = 0; i < getReaction(n)->getNumReactants(); i++) { getReaction(n)->getReactant(i)->unsetSBOTerm(); if (getReaction(n)->getReactant(i)->isSetStoichiometryMath()) { getReaction(n)->getReactant(i)->getStoichiometryMath()->unsetSBOTerm(); } } for (i = 0; i < getReaction(n)->getNumProducts(); i++) { getReaction(n)->getProduct(i)->unsetSBOTerm(); if (getReaction(n)->getProduct(i)->isSetStoichiometryMath()) { getReaction(n)->getProduct(i)->getStoichiometryMath()->unsetSBOTerm(); } } for (i = 0; i < getReaction(n)->getNumModifiers(); i++) { getReaction(n)->getModifier(i)->unsetSBOTerm(); } if (getReaction(n)->isSetKineticLaw()) { getReaction(n)->getKineticLaw()->unsetSBOTerm(); } } for (n = 0; n < getNumFunctionDefinitions(); n++) { getFunctionDefinition(n)->unsetSBOTerm(); } for (n = 0; n < getNumEvents(); n++) { getEvent(n)->unsetSBOTerm(); for (i = 0; i < getEvent(n)->getNumEventAssignments(); i++) { getEvent(n)->getEventAssignment(i)->unsetSBOTerm(); } if (getEvent(n)->isSetTrigger()) { getEvent(n)->getTrigger()->unsetSBOTerm(); } if (getEvent(n)->isSetDelay()) { getEvent(n)->getDelay()->unsetSBOTerm(); } } } }
/* in L1 and L2 there were built in values for key units * such as 'volume', 'length', 'area', 'substance' and 'time' * In L3 these have been removed - thus if a model uses one of these * it needs a unitDefinition to define it */ void Model::addDefinitionsForDefaultUnits() { /* create a list of unit values */ IdList unitsUsed; unsigned int n; bool implicitVolume = false; bool implicitArea = false; bool implicitLength = false; bool implicitSubstance = false; for (n = 0; n < getNumCompartments(); n++) { if (getCompartment(n)->isSetUnits()) { unitsUsed.append(getCompartment(n)->getUnits()); } else { if (getCompartment(n)->getSpatialDimensions() == 3) { implicitVolume = true; getCompartment(n)->setUnits("volume"); } else if (getCompartment(n)->getSpatialDimensions() == 2) { implicitArea = true; getCompartment(n)->setUnits("area"); } else if (getCompartment(n)->getSpatialDimensions() == 1) { implicitLength = true; getCompartment(n)->setUnits("length"); } } } for (n = 0; n < getNumSpecies(); n++) { if (getSpecies(n)->isSetSubstanceUnits()) { unitsUsed.append(getSpecies(n)->getSubstanceUnits()); } else { implicitSubstance = true; getSpecies(n)->setSubstanceUnits("substance"); } if (getSpecies(n)->isSetSpatialSizeUnits()) unitsUsed.append(getSpecies(n)->getSpatialSizeUnits()); } for (n = 0; n < getNumParameters(); n++) { if (getParameter(n)->isSetUnits()) unitsUsed.append(getParameter(n)->getUnits()); } if (getUnitDefinition("volume") == NULL) { if (unitsUsed.contains("volume") || implicitVolume) { UnitDefinition * ud = createUnitDefinition(); ud->setId("volume"); Unit * u = ud->createUnit(); u->setKind(UnitKind_forName("litre")); u->setScale(0); u->setExponent(1.0); u->setMultiplier(1.0); setVolumeUnits("volume"); } else { setVolumeUnits("litre"); } } else { setVolumeUnits("volume"); } if (getUnitDefinition("substance") == NULL) { if (unitsUsed.contains("substance") || implicitSubstance) { UnitDefinition * ud = createUnitDefinition(); ud->setId("substance"); Unit * u = ud->createUnit(); u->setKind(UnitKind_forName("mole")); u->setScale(0); u->setExponent(1.0); u->setMultiplier(1.0); setSubstanceUnits("substance"); setExtentUnits("substance"); } else { setSubstanceUnits("mole"); setExtentUnits("mole"); } } else { setSubstanceUnits("substance"); setExtentUnits("substance"); } if (getUnitDefinition("area") == NULL) { UnitDefinition * ud = createUnitDefinition(); ud->setId("area"); Unit * u = ud->createUnit(); u->setKind(UnitKind_forName("metre")); u->setScale(0); u->setExponent(2.0); u->setMultiplier(1.0); setAreaUnits("area"); } else { setAreaUnits("area"); } if (getUnitDefinition("length") == NULL) { if (unitsUsed.contains("length") || implicitLength) { UnitDefinition * ud = createUnitDefinition(); ud->setId("length"); Unit * u = ud->createUnit(); u->setKind(UnitKind_forName("metre")); u->setScale(0); u->setExponent(1.0); u->setMultiplier(1.0); setLengthUnits("length"); } else { setLengthUnits("metre"); } } else { setLengthUnits("length"); } if (getUnitDefinition("time") == NULL) { setTimeUnits("second"); } else { setTimeUnits("time"); } }
void Model::assignRequiredValues() { // when converting to L3 some attributes which have default values in L1/L2 // but are required in L3 are not present or set unsigned int i, n; if (getNumUnitDefinitions() > 0) { for (i = 0; i < getNumUnitDefinitions(); i++) { for (n = 0; n < getUnitDefinition(i)->getNumUnits(); n++) { Unit *u = getUnitDefinition(i)->getUnit(n); if (!u->isSetExponent()) u->setExponent(1.0); if (!u->isSetScale()) u->setScale(0); if (!u->isSetMultiplier()) u->setMultiplier(1.0); } } } if (getNumCompartments() > 0) { for (i = 0; i < getNumCompartments(); i++) { Compartment *c = getCompartment(i); c->setConstant(c->getConstant()); } } if (getNumSpecies() > 0) { for (i = 0; i < getNumSpecies(); i++) { Species * s = getSpecies(i); s->setBoundaryCondition(s->getBoundaryCondition()); s->setHasOnlySubstanceUnits(s->getHasOnlySubstanceUnits()); s->setConstant(s->getConstant()); } } if (getNumParameters() > 0) { for (i = 0; i < getNumParameters(); i++) { Parameter * p = getParameter(i); p->setConstant(p->getConstant()); } } if (getNumReactions() > 0) { for (i = 0; i < getNumReactions(); i++) { Reaction * r = getReaction(i); r->setFast(r->getFast()); r->setReversible(r->getReversible()); if (r->getNumReactants() > 0) { for (n = 0; n < r->getNumReactants(); n++) { SpeciesReference *sr = r->getReactant(n); if (sr->isSetStoichiometryMath()) { sr->setConstant(false); } else { sr->setConstant(true); } } } if (r->getNumProducts() > 0) { for (n = 0; n < r->getNumProducts(); n++) { SpeciesReference *sr = r->getProduct(n); if (sr->isSetStoichiometryMath()) { sr->setConstant(false); } else { sr->setConstant(true); } } } } } if (getNumEvents() > 0) { for (i = 0; i < getNumEvents(); i++) { Event * e = getEvent(i); e->setUseValuesFromTriggerTime(e->getUseValuesFromTriggerTime()); if (e->isSetTrigger()) { Trigger *t = e->getTrigger(); t->setPersistent(true); t->setInitialValue(true); } } } }
GApopulation randomNetworks(int sz0) { int i, j, r, k, n, total = 0; ReactionNetwork * rnet; GApopulation P0 = 0, P; P = (GAindividual*) malloc( sz0 * sizeof (GAindividual) ); r = (int)(networkProbs[MASS_ACTION_NETWORK] * sz0); if (r > 0 && r <= sz0) { P0 = randomMassActionNetworks(r); for (i=0; i < r; ++i) { rnet = (ReactionNetwork*) malloc(sizeof(ReactionNetwork)); rnet->type = MASS_ACTION_NETWORK; rnet->network = P0[i]; //rnet->id = i+total; //rnet->parents = 0; n = getNumSpecies(rnet); rnet->initialValues = (double*)malloc(n*sizeof(double)); rnet->fixed = (int*)malloc(n*sizeof(double)); for (j=0; j < n; ++j) { rnet->initialValues[j] = AVG_INIT_VALUES * mtrand(); rnet->fixed[j] = 0; } P[total+i] = rnet; } total += r; } r = (int)(networkProbs[ENZYME_NETWORK] * sz0); if (r > 0 && r <= sz0) { P0 = randomEnzymeNetworks(r); for (i=0; i < r; ++i) { rnet = (ReactionNetwork*) malloc(sizeof(ReactionNetwork)); rnet->type = ENZYME_NETWORK; rnet->network = P0[i]; //rnet->id = i+total; //rnet->parents = 0; n = getNumSpecies(rnet); rnet->initialValues = (double*)malloc(n*sizeof(double)); rnet->fixed = (int*)malloc(n*sizeof(double)); for (j=0; j < n; ++j) { rnet->initialValues[j] = AVG_INIT_VALUES * mtrand(); rnet->fixed[j] = 0; } P[total+i] = rnet; } total += r; } r = (int)(networkProbs[GENE_REGULATION_NETWORK] * sz0); if (r > 0 && r <= sz0) { P0 = randomGeneRegulationNetworks(r); for (i=0; i < r; ++i) { rnet = (ReactionNetwork*) malloc(sizeof(ReactionNetwork)); rnet->type = GENE_REGULATION_NETWORK; rnet->network = P0[i]; //rnet->parents = 0; n = getNumSpecies(rnet); rnet->initialValues = (double*)malloc(n*sizeof(double)); rnet->fixed = (int*)malloc(n*sizeof(double)); for (j=0; j < n; ++j) { rnet->initialValues[j] = AVG_INIT_VALUES * mtrand(); rnet->fixed[j] = 0; } //rnet->id = i+total; P[total+i] = rnet; } total += r; } if (total < sz0) { k = sz0 - total; P0 = randomProteinInteractionNetworks(k); for (i=0; i < k; ++i) { rnet = (ReactionNetwork*) malloc(sizeof(ReactionNetwork)); rnet->type = PROTEIN_INTERACTION_NETWORK; rnet->network = P0[i]; //rnet->parents = 0; n = getNumSpecies(rnet); rnet->initialValues = (double*)malloc(n*sizeof(double)); rnet->fixed = (int*)malloc(n*sizeof(double)); for (j=0; j < n; ++j) { rnet->initialValues[j] = AVG_INIT_VALUES * mtrand(); rnet->fixed[j] = 0; } //rnet->id = i+total; P[i+total] = rnet; } } return P; }
static int callBackWithLogKeeping(int iter, int popSz, GApopulation pop, double * fitnessArray, int *** parentsArray) { unsigned long long * seeds; double f; int i,j,k,*parents, num = 10*popSz, max = 0, stop = 0; int * temp = 0, * ids = 0; GAindividual * p; //save int each_fitness = PRINT_EACH_FITNESS, each_script = PRINT_EACH_SCRIPT, each_size = PRINT_EACH_SIZE, each_best_lineage = PRINT_EACH_BEST_LINEAGE, each_all_fitness = PRINT_EACH_ALL_FITNESS, each_all_lineage = PRINT_EACH_ALL_LINEAGE; if (USER_CALLBACK_FNC) stop = USER_CALLBACK_FNC(iter,popSz,pop,fitnessArray, parentsArray); if (!stop) stop = (iter == _MAX_ITER); if (stop) { //cheat PRINT_EACH_FITNESS = PRINT_FINAL_FITNESS; PRINT_EACH_SCRIPT = PRINT_FINAL_SCRIPT; PRINT_EACH_SIZE = PRINT_FINAL_SIZE; PRINT_EACH_BEST_LINEAGE = PRINT_FINAL_BEST_LINEAGE; PRINT_EACH_ALL_FITNESS = PRINT_FINAL_ALL_FITNESS; PRINT_EACH_ALL_LINEAGE = PRINT_FINAL_ALL_LINEAGE; //printf("\n========final results=======\n"); fprintf(LOGFILE,"\n========final results=======\n"); if (LOGFILE && PRINT_SEEDS) { seeds = getMTseeds(); fprintf(LOGFILE,"random number generator seeds: %llf,%llf,%llf,%llf\n",seeds[0],seeds[1],seeds[2],seeds[3]); } } if (iter == 0) //header { printf("gen"); fprintf(LOGFILE,"gen"); if (PRINT_EACH_FITNESS && !PRINT_EACH_ALL_FITNESS) { printf("\tfitness "); fprintf(LOGFILE,"\tfitness "); } if (PRINT_EACH_ALL_FITNESS) { for (i=0; i < popSz; ++i) { //printf("\tfitness_%i",i); fprintf(LOGFILE,"\tfitness_%i",i); } } if (PRINT_EACH_SIZE) { printf("\tspecies\treactions"); fprintf(LOGFILE,"\tspecies\treactions"); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { //printf("\tparents"); fprintf(LOGFILE,"\tparents"); } printf("\n"); fprintf(LOGFILE,"\n"); printf("---"); fprintf(LOGFILE,"---"); if (PRINT_EACH_FITNESS && !PRINT_EACH_ALL_FITNESS) { printf("\t------- "); fprintf(LOGFILE,"\t------- "); } if (PRINT_EACH_ALL_FITNESS) { for (i=0; i < popSz; ++i) { //printf("\t----------",i); fprintf(LOGFILE,"\t -------- ",i); } } if (PRINT_EACH_SIZE) { printf("\t-------\t---------"); fprintf(LOGFILE,"\t-------\t---------"); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { //printf("\t-------"); fprintf(LOGFILE,"\t-------"); } printf("\n"); fprintf(LOGFILE,"\n"); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { ids = (int*)malloc(num * sizeof(int)); for (i=0; i < num; ++i) ids[i] = 0; for (i=0; i < popSz; ++i) { p = pop[i]; if (!p) continue; parents = GAgetOriginalParents(i,iter,parentsArray); if (parents) { for (j=0; parents[j] > 0; ++j) { if (parents[j] >= max) max = parents[j]; if (parents[j] >= num) { temp = ids; ids = (int*)malloc( num * 2 * sizeof(int) ); for (k=0; k < num; ++k) ids[k] = temp[k]; num *= 2; for (; k < num; ++k) ids[k] = 0; free(temp); } ids[ parents[j] ] += 1; } } else { } if (PRINT_EACH_BEST_LINEAGE && !PRINT_EACH_ALL_LINEAGE) break; } } printf("%i",iter); fprintf(LOGFILE,"%i",iter); if (PRINT_EACH_FITNESS && !PRINT_EACH_ALL_FITNESS) { f = fitnessArray[0]; printf("\t%lf",f); fprintf(LOGFILE,"\t%lf",f); } if (PRINT_EACH_ALL_FITNESS) { for (i=0; i < popSz; ++i) { f = fitnessArray[i]; //printf("\t%lf",f); fprintf(LOGFILE,"\t%lf",f); } } if (PRINT_EACH_SIZE) { printf("\t%i\t%i",getNumSpecies(pop[0]),getNumReactions(pop[0])); fprintf(LOGFILE,"\t%i\t%i",getNumSpecies(pop[0]),getNumReactions(pop[0])); } if (TRACK_NETWORK_PARENTS && (PRINT_EACH_BEST_LINEAGE || PRINT_EACH_ALL_LINEAGE)) { for (i=0; i < max; ++i) { //printf("\t%i",ids[i]); fprintf(LOGFILE,"\t%i",ids[i]); } } if (PRINT_EACH_SCRIPT) { //printf("\n========script=======\n"); fprintf(LOGFILE,"\n========script=======\n"); //printNetwork(stdout,pop[0]); printNetwork(LOGFILE,pop[0]); //printf("\n=====================\n"); fprintf(LOGFILE,"\n=====================\n"); } printf("\n"); fprintf(LOGFILE,"\n"); if (ids && (num > 0)) free(ids); if (iter == _MAX_ITER) { //restore PRINT_EACH_FITNESS = each_fitness; PRINT_EACH_SCRIPT = each_script; PRINT_EACH_SIZE = each_size; PRINT_EACH_BEST_LINEAGE = each_best_lineage; PRINT_EACH_ALL_FITNESS = each_all_fitness; PRINT_EACH_ALL_LINEAGE = each_all_lineage; } return stop; }
double compareSteadyStates(GAindividual p, double ** table, int rows, int inputs, int outputs, int corr, double ** res) { int i, j, m, k, g, cols, n, *best; double * ss, * iv, closest, temp, sumOfSq, corrcoef, *mXY, *mX, *mY, *mX2, *mY2, oldMaxT = SS_FUNC_MAX_TIME; double * ss2; double a,b,c; ReactionNetwork * r = (ReactionNetwork*)(p); SS_FUNC_MAX_TIME = 100.0; cols = inputs + outputs; n = getNumSpecies(r); if (n < cols) return 0.0; // not enough species for (i=0; i < inputs; ++i) { setFixed(r,i,1); } sumOfSq = 0.0; corrcoef = 0.0; iv = (double*) malloc( n * sizeof(double) ); for (i=0; i < n; ++i) iv[i] = r->initialValues[i]; best = (int*) malloc ( outputs * sizeof(int) ); mX = (double*)malloc( outputs * sizeof(double) ); mY = (double*)malloc( outputs * sizeof(double) ); mXY = (double*)malloc( outputs * sizeof(double) ); mX2 = (double*)malloc( outputs * sizeof(double) ); mY2 = (double*)malloc( outputs * sizeof(double) ); for (i=0; i < outputs; ++i) { best[i] = -1; mXY[i] = mX[i] = mY[i] = mX2[i] = mY2[i] = 0; } ss2 = (double*)malloc(rows*sizeof(double)); for (m=0; m < rows; ++m) { for (i=0; i < inputs; ++i) r->initialValues[i] = table[m][i]; for (i=inputs; i < n; ++i) r->initialValues[i] = 0.0; ss = networkSteadyState(r); if (ss) //error in simulation? { if (res) { for (i=0; i < inputs; ++i) res[m][i] = ss[i]; } for (i=0; i < outputs; ++i) //for each target output { if (best[i] < 0) { closest = -1.0; for (j=inputs; j < n; ++j) //find best match { g = 0; for (k=0; k < outputs; ++k) if (best[k] == j) { g = 1; break; } if (g) continue; temp = (ss[j] - table[m][inputs+i]); if ((closest < 0.0) || ((temp*temp) < closest)) { closest = temp*temp; best[i] = j; } } } j = i+inputs; if (res) { res[m][inputs+i] = ss[j]; } ss2[m] = ss[j]; temp = (ss[j] - table[m][inputs+i]); closest = temp*temp; sumOfSq += closest; mX[i] += ss[j]; mY[i] += table[m][inputs+i]; mXY[i] += table[m][inputs+i] * ss[j]; mX2[i] += ss[j]*ss[j]; mY2[i] += table[m][inputs+i]*table[m][inputs+i]; if (closest < 0.0) { sumOfSq = -1.0; break; } } free(ss); } else { sumOfSq = -1.0; break; } } corrcoef = 0.0; for (i=0; i < outputs; ++i) { mX[i] /= rows; mY[i] /= rows; mXY[i] /= rows; mX2[i] /= rows; mY2[i] /= rows; if ( (mX2[i] - mX[i]*mX[i]) <= 0.0 || (mY2[i] - mY[i]*mY[i]) <= 0.0 ) { sumOfSq = -1.0; break; } a = mXY[i] - mX[i]*mY[i]; b = mX2[i] - mX[i]*mX[i]; c = mY2[i] - mY[i]*mY[i]; if (a > 1.0 && b > 1.0 && c > 1.0) { temp = a/( sqrt(b)*sqrt(c)); //correlation formula corrcoef = temp*temp; //between 0 and 1 } else { corrcoef = 0.0; sumOfSq = 0.0; } if (corrcoef > 0.9) { temp = temp + 0.0; } } free(ss2); for (i=0; i < n; ++i) r->initialValues[i] = iv[i]; free(iv); free(mX); free(mY); free(mXY); free(mX2); free(mY2); //restore the fixed for (i=0; i < inputs; ++i) { setFixed(r,i,0); } SS_FUNC_MAX_TIME = oldMaxT; if (sumOfSq <= 0.0) return 0.0; if (corr) return corrcoef; return (1.0 / (1.0 + sumOfSq)); }
GAindividual crossoverNetwork(GAindividual p1, GAindividual p2) { ReactionNetwork * r1 = (ReactionNetwork*)(p1); ReactionNetwork * r2 = (ReactionNetwork*)(p2); GACrossoverFnc f; GAindividual net; ReactionNetwork * r; int i,j,sz1,sz2; if (mtrand() > CROSSOVER_PROB || r1->type != r2->type || r2->type < 0 || r2->type >= NUMBER_OF_NETWORK_TYPES) return mutateNetwork(p1); f = crossoverFunctions[r2->type]; if (f) { net = f(r1->network,r2->network); r = (ReactionNetwork*)malloc(sizeof(ReactionNetwork)); r->type = r1->type; r->network = net; //r->id = r1->id; sz1 = getNumSpecies(r1); sz2 = getNumSpecies(r2); j = getNumSpecies(r); r->initialValues = (double*)malloc(j * sizeof(double)); r->fixed = (int*)malloc(j * sizeof(double)); for (i=0; i < sz1 && i < j; ++i) { r->initialValues[i] = r1->initialValues[i]; r->fixed[i] = r1->fixed[i]; } for (i=0; i < sz2 && (sz1+i) < j; ++i) { r->initialValues[sz1+i] = r2->initialValues[i]; r->fixed[sz1+i] = r2->fixed[i]; } for (i=sz1+sz2; i < j; ++i) { r->initialValues[i] = AVG_INIT_VALUES * mtrand(); r->fixed[i] = 0; } /* i = j = sz1 = sz2 = 0; if (r1->parents) { while (r1->parents[i]) { ++i; } } j = 0; if (r2->parents) { while (r2->parents[j]) { ++j; } } sz1 = i + j; r->parents = 0; if (TRACK_NETWORK_PARENTS) { if (sz1 > 0) { p = (int*)malloc(sz1 * sizeof(int)); for (k=0; k < i; ++k) p[k] = r1->parents[k]; for (k=0; k < j; ++k) p[k+i] = r2->parents[k]; sz2 = 0; for (i=0; i < sz1; ++i) { for (j=0; j < i; ++j) if (p[j] == p[i]) break; if (i == j) ++sz2; } r->parents = (int*) malloc((sz2+1)*sizeof(int)); r->parents[sz2] = 0; k = 0; for (i=0; i < sz1; ++i) { for (j=0; j < i; ++j) if (p[j] == p[i]) break; if (i == j) { r->parents[k] = p[i]; ++k; } } free(p); } else { r->parents = (int*) malloc((3)*sizeof(int)); r->parents[2] = 0; r->parents[0] = r1->id; r->parents[1] = r2->id; } } */ return r; } return mutateNetwork(p1); }