// Class automatically generated by Dev-C++ New Class wizard #include "node.h" // class's header file // class constructor Node::Node() { state = false; inNode = 0; outNode = 0; inSum = 0.000000; maxNode = 15; addWeight = 0.20000; baseWeight = 0.5000; thresh = 1.00000; strcpy(rootName, "RoOt: "); } Node::Node(char naming[20]) { state = false; inNode = 0; outNode = 0; inSum = 0.000000; maxNode = 15; addWeight = 0.20000; baseWeight = 0.5000; thresh = 1.00000; strcpy (name, naming); strcpy(rootName, "RoOt: "); // cout << "Node created with name: " << name <name, name) != 0) // only if not adding self { if (inRef(adding) == 99) // if adding is NOT already an input node { inputs[inNode] = adding; // Add the Input Node if (strcmp(adding->name, rootName) == 0) //If I'm adding Root { weight[inNode] = thresh; // Gets full weight } else { weight[inNode] = baseWeight; } //Otherwise baseWeight inNode++; // Increase the Input Node Counter inputs[inRef(adding)]->addOutput(this); // give adding my Output inSort(); // Sort the new list of inputs //if (inNode>15) { branchIn(); } //If we now have more than 15 nodes:branch off input node } else // if adding returns a value (i.e. adding exists as a node) { weight[inRef(adding)] += addWeight; //Add some weight to it's connection } } // At this point, ELSE would signify I'm trying to add myself. } // End addInput void Node::addInput(Node *adding, float w) { if (strcmp(adding->name, name) != 0) // only if not adding self { if (inRef(adding) == 99) // if adding is NOT already an input node { inputs[inNode] = adding; // add the input node if (strcmp(adding->name, rootName) != 0) //If I'm adding Root { weight[inNode] = thresh; // Gets full weight } else { weight[inNode] = w; } //Otherwise specified weight inNode++; // Increase the Input Node Counter adding->addOutput(this); // give adding my Output inSort(); // Sort the new list of inputs //if (inNode>15) { branchIn(); } //If we now have more than 15 nodes:branch off input node } else // if inRef(adding) returns a value (i.e. adding exists as an InNode) { weight[inRef(adding)] += w; //Add specified weight to it's connection } } else // If Adding->name = this->name (trying to add myself) { } // Do nothing } // End addInput (with Weight) void Node::addOutput(Node *adding) { if (strcmp(adding->name, name) != 0) // only if not adding self { if (outRef(adding) == 99) //If adding is NOT already an output node { outputs[outNode] = adding; outNode++; //Increase the output node Counter outSort(); // Sort the new list of outputs // if (outNode>15) { branchOut(); } //If we now have more than 15 nodes:branch off outputs } // else if outRef(adding) returns a value (i.e. adding exists as an OutNode) do nothing } // else If adding->name = this->name (trying to add myself) do nothing } // End addOutput bool Node::live() { for (int i=0; istate) // if the input is ON (state=true) { inSum += weight[i]; // add it's weight to the inSum } } // inSum has been calculated if ((inSum > thresh) && (!state)) // If the inSum>thresh and this node is NOT already ON { return true; } // Then yes, this is live to fire; else { return false; } // Otherwise no, we are not live to fire } // End live void Node::fire() { cout << name << " "; // Print the word for I have fired; (and a space) if (outNode>0) // If I have outputs to even look at { if (strcmp(name, rootName) != 0) //if not root { if (live()) // If I'm live to fire (inSum>thresh & !state) { state=true; // This node becomes ON for (int i=0; ilive()) // if the output is live to fire { outputs[i]->fire(); //Fire the output break; // Fired, so good, break the for loop } } // endFOR-if no outputs fired, or we break: we are here state=false; //Reset the node (turn it OFF) - This happens on the way back from fire seq. } else // if I'm not live to fire { } // Do nothing; } // end if NotRoot seq. else // If this IS root { state=true; srand((unsigned)time(0)); int randNum = (rand()%3); //Generate a random 1 2 3 int highOut = 0; //initialize a few temporary ints int outRef = 0; int outRefA = 0; int outRefB = 0; for (int j=0; joutNode >= highOut) //if this out has more nodes than the current high number { highOut=outputs[j]->outNode; // New high number is that many nodes outRefB=outRefA; // second place becomes third place a->b outRefA=outRef; // first place becomes second place high->a outRef=j; // Our new highest place holder is formed } } // Determined 3 highest output nodes (i.e which of my outputs have the most output OPTIONS themselves) if (randNum == 0) { outputs[outRef]->fire(); } // Pick one of the top three randomly else if (randNum == 1) { outputs[outRefA]->fire(); } else if (randNum == 2) { outputs[outRefB]->fire(); } state=false; } // End ifRoot seq. } // End if I have outputs to look at else // outNode is 0 or less { } // Do nothing else, no nodes to fire anyways }// end Fire void Node::showOut() { for (int i=0; iname << endl; } } void Node::showIn() { for (int i=0; iname << " with weight " << weight[i] << endl; } } void Node::branchIn() { Node *inN; inN = new Node(); //Create a new blank node for (int i=0; i<6; i++) //For 6 iterations - Connecting new node to first 6 inputs { inN->addInput(inputs[i], weight[i]); //New node gets input #i, with it's corresponding weight } for (int i=0; i<(inNode-6); i++) //for 6 less than # of inputs { inputs[i]->delOut(this); //delete that inputs connection to this inputs[i]=inputs[(i+6)]; // input shuffled back 6 places weight[i]=weight[(i+6)]; // weights too } inNode -= 6; // # of inputs reduced by 6 addInput(inN, 1.0000); // Get a new input from the new node } // End branch IN void Node::branchOut() { Node *outN; outN = new Node; // create a new blank node for (int i=0; i<6; i++) //for 6 iterations { int temp = outputs[i]->inRef(this); //Find out what that output refers to this as outputs[i]->addInput(outN, outputs[i]->weight[temp]); //give that output an input to the new node, with the weight it had for this node } for (int i=0; i<(outNode-6); i++) // for 6 less than # of outputs { outputs[i]->delIn(this); // delete that inputs connection to this outputs[i] = outputs[(i+6)]; // output shuffled back 6 places } outNode -= 6; // # of outputs reduced by 6 outN->addInput(this, 1.00000); //give new Node an input of THIS with full weight } // End branchOut void Node::inSort() { for (int i=0; i weight[i]) // if there's a larger one after this(i) one { Node *temp = inputs[i]; float tempW = weight[i]; inputs[i] = inputs[j]; weight[i] = weight[j]; inputs[j] = temp; weight[j] = tempW; // Swap 'em } } //end for j } //end for i } // End inSort void Node::outSort() { for (int i=0; iinRef(this); //find out where i keeps me int second = outputs[j]->inRef(this); // find out where j keeps me if (outputs[j]->weight[second] > outputs[i]->weight[first]) // if theres a more important connection after this(i) one { Node *temp = outputs[i]; outputs[i] = outputs[j]; outputs[j] = temp; // swap 'em } } // end for j } // end for i } // End outSort int Node::inRef(Node *ref) { for (int i=0; iname, ref->name) == 0) // if it matches what we look for { return i; } } return 99; // Node Not Found!!! } // End inRef int Node::outRef(Node *ref) { for (int i=0; iname, ref->name) == 0) // if it matches what we look for { return i; } } return 99; // Node Not Found!!! } // End outRef void Node::delIn(Node *deling) { for (int i=0; iname, deling->name) == 0) // if it matches what we look for { int del = i; //set which node to delete for (int j=del; jname, deling->name) == 0) // if it matches what we look for { int del = i; //set which node to delete for (int j=del; j