void KnapsackRandomize(void *a) {
	TRAIN_ANNEAL *anneal;
	int dimensions,i,  holdingEverythingAlready, pt;
	unsigned char *position;

	anneal = (TRAIN_ANNEAL*)a;
	position = (unsigned char*)anneal->train.trial_position;
	dimensions = anneal->train.position_size/sizeof(unsigned char);

	/* check for strange case where we have everything!
		This means that the max allowed knapsack weight is greater than the total of grabbing everything.
		This is kind of pointless, but don't go into an endless loop! */
	holdingEverythingAlready = 1;
	for (i=0;i<dimensions;i++) {
		if (!position[i]) {
			holdingEverythingAlready = 0;
			break;
		}
	}

	if (!holdingEverythingAlready) {
		/* try to add something */
		pt = RandNextIntRange(anneal->train.random, 0, dimensions); /* prime */
		while (position[pt]) {
			pt = RandNextIntRange(anneal->train.random, 0, dimensions);
		}

		/* add the item we found */
		position[pt] = 1;

		/* We probably need to drop something now. */
		Balance((KNAPSACK_PARAMS*)anneal->train.params, position, dimensions);
	}
}
static KNAPSACK_PARAMS *CreateKnapsack() {
	KNAPSACK_PARAMS *result;
	int n;

	result = (KNAPSACK_PARAMS *)calloc(1,sizeof(KNAPSACK_PARAMS));
	result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL));

	/* Generate a random set of items. */
	for (n = 0; n < NUM_ITEMS_TO_CHOOSE; n++) {
		result->profit[n] = RandNextIntRange(result->random,0, ITEM_MAX_VALUE);
		result->weight[n] = RandNextIntRange(result->random,0, ITEM_MAX_WEIGHT);
	}

	return result;
}
static void Balance(KNAPSACK_PARAMS *params, unsigned char *items, int item_count) {
	int remove;
	while (CalculateTotalWeight(params,items,item_count) > KNAPSACK_MAX_WEIGHT) {
		remove = RandNextIntRange(params->random, 0, item_count);
		items[remove] = 0;
	}
}
示例#4
0
/* Create an instance of the knapsack problem */
static KNAPSACK_PARAMS *CreateKnapsack() {
	KNAPSACK_PARAMS *result;
	int n;

	/* Create the KNAPSACK_PARAMS struct and a random number generator. */
	result = (KNAPSACK_PARAMS *)calloc(1,sizeof(KNAPSACK_PARAMS));
	result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL));

	/* Generate a random set of items. Random weights and profits. */
	result->max_profit = 0;
	for (n = 0; n < NUM_ITEMS_TO_CHOOSE; n++) {
		result->profit[n] = RandNextIntRange(result->random,0, ITEM_MAX_VALUE);
		result->weight[n] = RandNextIntRange(result->random,0, ITEM_MAX_WEIGHT);
		result->max_profit+=result->profit[n];
	}

	return result;
}
示例#5
0
文件: KMeans.c 项目: Adri96/aifh
void KMeansInitRandom(CLUSTER_ALOG *kmeans, CLUSTER_ITEM *items) {
	CLUSTER *currentCluster;
	CLUSTER *sourceCluster;
	unsigned int currentClusterIndex;
	CLUSTER_ITEM *currentItem;
	CLUSTER_ITEM *nextItem;
	CLUSTER_ITEM *sourceItem;
	unsigned int sourceClusterIndex;
	unsigned int sourceClusterCount;
	unsigned int sourceItemIndex;

	/* First, assign all items to a random cluster */
	currentItem = items;
	while(currentItem!=NULL) {
		nextItem = currentItem->next;
		currentClusterIndex = RandNextIntRange(kmeans->rnd, 0,kmeans->k);
		currentItem->next = kmeans->clusters[currentClusterIndex].firstItem;
		kmeans->clusters[currentClusterIndex].firstItem = currentItem;
		currentItem = nextItem;
	}

	/* Finally, make sure that all clusters have at least one item */
	for(currentClusterIndex=0;currentClusterIndex<kmeans->k;currentClusterIndex++) {
		currentCluster = &kmeans->clusters[currentClusterIndex];
		while(currentCluster->firstItem==NULL) {
			/* Choose a random cluster, might be the same cluster we are trying to fill.  */
			sourceClusterIndex = RandNextIntRange(kmeans->rnd, 0,kmeans->k);
			sourceCluster = &kmeans->clusters[sourceClusterIndex];
			sourceClusterCount = KMeansCountItems(sourceCluster->firstItem);
			/* Does J have at least two items? (this also means we did not pick the cluster we are filling)*/
			if( sourceClusterCount>=2 ) {
				sourceItemIndex = RandNextIntRange(kmeans->rnd, 0, sourceClusterCount);
				sourceItem = KMeansFindItem(sourceCluster->firstItem,sourceClusterIndex);
				KMeansRemoveItem(&sourceCluster->firstItem,sourceItem);
				sourceItem->next = NULL;
				currentCluster->firstItem = sourceItem;
			}
		}
	}

	/* Calculate the centroids */
	KMeansUpdateStep(kmeans);
}	
示例#6
0
文件: KMeans.c 项目: Adri96/aifh
void KMeansInitForgy(CLUSTER_ALOG *kmeans, CLUSTER_ITEM *items) {
	unsigned int currentClusterIndex;
	CLUSTER *currentCluster;
	unsigned int itemCount;
	unsigned int selectedItemIndex;
	CLUSTER_ITEM *selectedItem;
	CLUSTER_ITEM *currentItem;
	CLUSTER_ITEM *nextItem;
	CLUSTER *nearestCluster;

	/* Assign a random item to each cluster */
	itemCount = KMeansCountItems(items);
	for(currentClusterIndex=0;currentClusterIndex<kmeans->k;currentClusterIndex++) {
		currentCluster = &kmeans->clusters[currentClusterIndex];
		selectedItemIndex = RandNextIntRange(kmeans->rnd, 0, itemCount);
		selectedItem = KMeansFindItem(items,selectedItemIndex);
		KMeansRemoveItem(&items,selectedItem);
		selectedItem->next = NULL;
		itemCount--;
		currentCluster->firstItem=selectedItem;
		memcpy(currentCluster->centroid,selectedItem->features,sizeof(double)*kmeans->featureCount);
	}

	/* Assign remaining items to values */
	currentItem = items;
	while(currentItem!=NULL) {
		nextItem = currentItem->next;

		/* Find nearest cluster */
		nearestCluster = KMeansFindNearestCluster(kmeans,currentItem);

		/* Add item to nearest cluster */
		currentItem->next = nearestCluster->firstItem;
		nearestCluster->firstItem = currentItem;

		/* Move to next item */
		currentItem = nextItem;
	}

	/* Calculate the centroids */
	KMeansUpdateStep(kmeans);
}