void AdaLearningNode::learnFromInstance(const Instance* inst, HoeffdingAdaptiveTree* ht, SplitNode* parent, int parentBranch) { int trueClass = (int) inst->getLabel(); //New option vore int k = Utils::poisson(1.0); Instance* weightedInst = cloneInstance(inst); // TODO cancel random if (k > 0) { weightedInst->setWeight(inst->getWeight() * k); } //Compute ClassPrediction using filterInstanceToLeaf int ClassPrediction = Utils::maxIndex(*(this->getClassVotes(inst, ht))); bool blCorrect = (trueClass == ClassPrediction); if (this->estimationErrorWeight == nullptr) { this->estimationErrorWeight = new ADWIN(); } double oldError = this->getErrorEstimation(); this->ErrorChange = this->estimationErrorWeight->setInput( blCorrect == true ? 0.0 : 1.0); if (this->ErrorChange == true && oldError > this->getErrorEstimation()) { this->ErrorChange = false; } //Update statistics LearningNodeNBAdaptive::learnFromInstance(weightedInst, ht); //inst delete weightedInst; //Check for Split condition double weightSeen = this->getWeightSeen(); if (weightSeen - this->getWeightSeenAtLastSplitEvaluation() >= ht->params.gracePeriod) { ht->attemptToSplit(this, parent, parentBranch); this->setWeightSeenAtLastSplitEvaluation(weightSeen); } //learnFromInstance alternate Tree and Child nodes /*if (this->alternateTree != nullptr { this->alternateTree.learnFromInstance(inst,ht); } for (Node child : this->children) { if (child != nullptr { child.learnFromInstance(inst,ht); } }*/ }
// called only by factories, treat as private UObject* ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUServiceFactory* factory, UErrorCode& status) const { if (U_FAILURE(status)) { return NULL; } if (isDefault()) { return handleDefault(key, actualReturn, status); } ICUService* ncthis = (ICUService*)this; // cast away semantic const CacheEntry* result = NULL; { // The factory list can't be modified until we're done, // otherwise we might update the cache with an invalid result. // The cache has to stay in synch with the factory list. // ICU doesn't have monitors so we can't use rw locks, so // we single-thread everything using this service, for now. // if factory is not null, we're calling from within the mutex, // and since some unix machines don't have reentrant mutexes we // need to make sure not to try to lock it again. XMutex mutex(&lock, factory != NULL); if (serviceCache == NULL) { ncthis->serviceCache = new Hashtable(status); if (ncthis->serviceCache == NULL) { return NULL; } if (U_FAILURE(status)) { delete serviceCache; return NULL; } serviceCache->setValueDeleter(cacheDeleter); } UnicodeString currentDescriptor; UVectorDeleter cacheDescriptorList; UBool putInCache = FALSE; int32_t startIndex = 0; int32_t limit = factories->size(); UBool cacheResult = TRUE; if (factory != NULL) { for (int32_t i = 0; i < limit; ++i) { if (factory == (const ICUServiceFactory*)factories->elementAt(i)) { startIndex = i + 1; break; } } if (startIndex == 0) { // throw new InternalError("Factory " + factory + "not registered with service: " + this); status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } cacheResult = FALSE; } do { currentDescriptor.remove(); key.currentDescriptor(currentDescriptor); result = (CacheEntry*)serviceCache->get(currentDescriptor); if (result != NULL) { break; } // first test of cache failed, so we'll have to update // the cache if we eventually succeed-- that is, if we're // going to update the cache at all. putInCache = TRUE; int32_t index = startIndex; while (index < limit) { ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(index++); UObject* service = f->create(key, this, status); if (U_FAILURE(status)) { delete service; return NULL; } if (service != NULL) { result = new CacheEntry(currentDescriptor, service); if (result == NULL) { delete service; status = U_MEMORY_ALLOCATION_ERROR; return NULL; } goto outerEnd; } } // prepare to load the cache with all additional ids that // will resolve to result, assuming we'll succeed. We // don't want to keep querying on an id that's going to // fallback to the one that succeeded, we want to hit the // cache the first time next goaround. if (cacheDescriptorList._obj == NULL) { cacheDescriptorList._obj = new UVector(uprv_deleteUObject, NULL, 5, status); if (U_FAILURE(status)) { return NULL; } } UnicodeString* idToCache = new UnicodeString(currentDescriptor); if (idToCache == NULL || idToCache->isBogus()) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } cacheDescriptorList._obj->addElement(idToCache, status); if (U_FAILURE(status)) { return NULL; } } while (key.fallback()); outerEnd: if (result != NULL) { if (putInCache && cacheResult) { serviceCache->put(result->actualDescriptor, result, status); if (U_FAILURE(status)) { delete result; return NULL; } if (cacheDescriptorList._obj != NULL) { for (int32_t i = cacheDescriptorList._obj->size(); --i >= 0;) { UnicodeString* desc = (UnicodeString*)cacheDescriptorList._obj->elementAt(i); serviceCache->put(*desc, result, status); if (U_FAILURE(status)) { delete result; return NULL; } result->ref(); cacheDescriptorList._obj->removeElementAt(i); } } } if (actualReturn != NULL) { // strip null prefix if (result->actualDescriptor.indexOf((UChar)0x2f) == 0) { // U+002f=slash (/) actualReturn->remove(); actualReturn->append(result->actualDescriptor, 1, result->actualDescriptor.length() - 1); } else { *actualReturn = result->actualDescriptor; } if (actualReturn->isBogus()) { status = U_MEMORY_ALLOCATION_ERROR; delete result; return NULL; } } UObject* service = cloneInstance(result->service); if (putInCache && !cacheResult) { delete result; } return service; } } return handleDefault(key, actualReturn, status); }
/* * Function pseudocode * * bool flatten(const string& inModuleInstanceName, bool inKeepPlacement = false, * bool inKeepRouting = false) * { * locate module instance in design; * if(module instance not found) * { * warn user that module instance was not found; * return false; * } * locate module definition corresponding to module instance in design; * if(module definition not found) * { * warn user that module definition was not found; * return false; * } * //handle instances * iterate over module definition instances * { * clone every instance and set its name to moduleInstance/instanceName; * if(!inKeepPlacement) unplace instance; * add instance clone to design; * } * //handle internal nets * iterate over module definition internal nets * { * clone every net and set its name to moduleInstance/netName; * if(!inKeepRouting) unroute net; * add net clone to design; * } * //handle external nets * iterate over all design nets * { * if(!inKeepRouting) unroute net; * //handle net sources * iterate over all sources of a net * { * if(source is a port of the module) * { * update net outpin to reflect the flattened instance and the corresponding pin * that matches the port; * } * } * //handle net sinks * iterate over all sinks of a net * { * if(sink is a port of the module) * { * update net inpin to reflect the flattened instance and the corresponding pin * that matches the port; * } * } * } * remove module instance from design; * } */ bool flatten(const string& inModuleInstanceName, bool inKeepPlacement = false, bool inKeepRouting = false) { // Flattened module source pins InstancePinSharedPtrVector flattenedModuleSourcePinPtrVector; // Flattened module sink pins InstancePinSharedPtrVector flattenedModuleSinkPinPtrVector; // Look for the module instance in the design InstanceSharedPtrConstIterator designModInstPtrConstIter = mDesignPtr->findInstance(inModuleInstanceName); // Check for a non-existent module instance if(designModInstPtrConstIter == mDesignPtr->instancesEnd()) { // Warn the user that the module instance was not found in the design std::clog << "WARNING: Module instance of name " << inModuleInstanceName << " was not found in design " << mDesignPtr->getName() << "." << std::endl; // Operation did not complete successfully return false; } // Get a pointer to the instance InstanceSharedPtr instPtr = *designModInstPtrConstIter; // Look for the module in the design ModuleSharedPtrIterator designModPtrConstIter = mDesignPtr->findModule(instPtr->getType()); // Check for a non-existent module if(designModPtrConstIter == mDesignPtr->modulesEnd()) { // Warn the user that the module was not found in the design std::clog << "WARNING: Module of name " << instPtr->getType() << " was not found in design " << mDesignPtr->getName() << "." << std::endl; // Operation did not complete successfully return false; } // Current design module ModuleSharedPtr module = *designModPtrConstIter; // Check whether tile/site for module instance is same as tile/site for module def. // In case they don't match, the flattened module instance will be unplaced and any // external/internal nets to the module instance will be unrouted. InstanceSharedPtrIterator moduleAnchorInstIterator = module->findInstance(module->getAnchor()); InstanceSharedPtr moduleAnchorInst = *moduleAnchorInstIterator; // Begin handle instances inside module // Get a begin iterator to module instances InstanceSharedPtrIterator moduleInstancesIter = module->instancesBegin(); // Get an end iterator to module instances InstanceSharedPtrIterator moduleInstancesEnd = module->instancesEnd(); // Iterate over all instances hosted by the module while(moduleInstancesIter != moduleInstancesEnd) { // Get a pointer to the current instance hosted by the module InstanceSharedPtr modInstPtr = *moduleInstancesIter; // Clone the instance InstanceSharedPtr modInstPtrClone = cloneInstance(modInstPtr, inModuleInstanceName + sHierarchySeparator + modInstPtr->getName()); // Set clone instance reference modInstPtrClone->setInstanceReferencePtr(Factory::newInstanceReferencePtr( inModuleInstanceName, module, modInstPtr)); // Unplace the instance if so requested if(!inKeepPlacement) modInstPtrClone->unplace(); // Add clone of instance to design mDesignPtr->addInstance(modInstPtrClone); // Next instance moduleInstancesIter++; } // while(moduleInstancesIter != moduleInstancesEnd) // End handle instances inside module // Begin handle internal module nets // Get a begin iterator to module nets NetSharedPtrIterator moduleNetsIter = module->netsBegin(); // Get an end iterator to module nets NetSharedPtrIterator moduleNetsEnd = module->netsEnd(); // Iterate over all nets inside module while(moduleNetsIter != moduleNetsEnd) { // Get a pointer to the current net NetSharedPtr moduleNetPtr = *moduleNetsIter; // Clone the net NetSharedPtr moduleNetPtrClone = cloneNet(moduleNetPtr, inModuleInstanceName + sHierarchySeparator + moduleNetPtr->getName(), inModuleInstanceName); // Unroute the net if so requested if(!inKeepRouting) moduleNetPtrClone->unroute(); // Add net to design mDesignPtr->addNet(moduleNetPtrClone); // Next net moduleNetsIter++; } // End handle internal module nets // Begin handle external module nets // Get a begin iterator to design nets NetSharedPtrIterator designNetsIter = mDesignPtr->netsBegin(); // Get an end iterator to design nets NetSharedPtrIterator designNetsEnd = mDesignPtr->netsEnd(); // Iterate over all nets connected to module and other instances outside module while(designNetsIter != designNetsEnd) { // Get a pointer to the current net NetSharedPtr designNetPtr = *designNetsIter; // Get a begin iterator to net sources InstancePinSharedPtrIterator designNetSourcesIter = designNetPtr->sourcesBegin(); // Get an end iterator to net sources InstancePinSharedPtrIterator designNetSourcesEnd = designNetPtr->sourcesEnd(); // Iterate over all net sources while(designNetSourcesIter != designNetSourcesEnd) { // Net pin InstancePinSharedPtr instPinPtr = *designNetSourcesIter; // Pin instance InstanceSharedPtr pinInstPtr = instPinPtr->getInstancePtr().lock(); // Net connects to module instance if(pinInstPtr == instPtr) { // Unroute the net if so requested if(!inKeepRouting) designNetPtr->unroute(); // Look for the port in the module PortSharedPtrConstIterator modulePortConstIter = module->findPort(instPinPtr->getPinName()); // Check if port was found if(modulePortConstIter != module->portsEnd()) { // Get a pointer to the current port PortSharedPtr modPortPtr = *modulePortConstIter; // Get module port reference instance InstanceSharedPtr portInstPtr = modPortPtr->getInstancePtr().lock(); // Find the cloned instance corresponding to the pin reference instance InstanceSharedPtrIterator pinInstPtrCloneItr = mDesignPtr->findInstance(inModuleInstanceName + sHierarchySeparator + portInstPtr->getName()); // Create new pin referencing the port reference instance flattenedModuleSourcePinPtrVector.push_back( Factory::newInstancePinPtr(*pinInstPtrCloneItr, modPortPtr->getPinName())); // Remove pin reference module instance designNetPtr->removeSource(instPinPtr); // Update designNetSourcesEnd iterator designNetSourcesEnd = designNetPtr->sourcesEnd(); } } else { // Move to next source designNetSourcesIter++; } } // while(designNetSourcesIter != designNetSourcesEnd) // Get a begin iterator to flattened module source pins InstancePinSharedPtrIterator flattenedModuleSourcesIter = flattenedModuleSourcePinPtrVector.begin(); // Get an end iterator to flattened module source pins InstancePinSharedPtrIterator flattenedModuleSourcesEnd = flattenedModuleSourcePinPtrVector.end(); // Update net sources with new source pins while(flattenedModuleSourcesIter != flattenedModuleSourcesEnd) { // Add source pin to net designNetPtr->addSource(*flattenedModuleSourcesIter); // Move to next source flattenedModuleSourcesIter++; } // Reset vector flattenedModuleSourcePinPtrVector.clear(); // Get a begin iterator to net sinks InstancePinSharedPtrIterator designNetSinksIter = designNetPtr->sinksBegin(); // Get an end iterator to net sinks InstancePinSharedPtrIterator designNetSinksEnd = designNetPtr->sinksEnd(); // Iterate over all net sinks while(designNetSinksIter != designNetSinksEnd) { // Net pin InstancePinSharedPtr instPinPtr = *designNetSinksIter; // Pin instance InstanceSharedPtr pinInstPtr = instPinPtr->getInstancePtr().lock(); // Net connets to module instance if(pinInstPtr == instPtr) { // Unroute the net if so requested if(!inKeepRouting) designNetPtr->unroute(); // Look for the port in the module PortSharedPtrConstIterator modulePortConstIter = module->findPort(instPinPtr->getPinName()); // Check if port was found if(modulePortConstIter != module->portsEnd()) { // Get a pointer to the current port PortSharedPtr modPortPtr = *modulePortConstIter; // Get module port reference instance InstanceSharedPtr portInstPtr = modPortPtr->getInstancePtr().lock(); // Find the cloned instance corresponding to the pin reference instance InstanceSharedPtrIterator pinInstPtrCloneItr = mDesignPtr->findInstance(inModuleInstanceName + sHierarchySeparator + portInstPtr->getName()); // Create new pin referencing the port reference instance flattenedModuleSinkPinPtrVector.push_back( Factory::newInstancePinPtr(*pinInstPtrCloneItr, modPortPtr->getPinName())); // Remove pin reference module instance designNetPtr->removeSink(instPinPtr); // Update designNetSinksEnd iterator designNetSinksEnd = designNetPtr->sinksEnd(); } } else { // Move to next source designNetSinksIter++; } } // while(designNetSinksIter != designNetSinksEnd) // Get a begin iterator to flattened module sink pins InstancePinSharedPtrIterator flattenedModuleSinksBegin = flattenedModuleSinkPinPtrVector.begin(); // Get an end iterator to flattened module sink pins InstancePinSharedPtrIterator flattenedModuleSinksEnd = flattenedModuleSinkPinPtrVector.end(); // Update net sources with new source pins while(flattenedModuleSinksBegin != flattenedModuleSinksEnd) { // Add sink pin to net designNetPtr->addSink(*flattenedModuleSinksBegin); // Move to next sink flattenedModuleSinksBegin++; } // Reset vector flattenedModuleSinkPinPtrVector.clear(); // Move to next net designNetsIter++; } // while(designNetsIter != designNetsEnd) // End handle external module nets // Remove module instance from design mDesignPtr->removeInstance(instPtr); // Operation successfully completed return true; }