Example #1
0
// ItemWeight
float
BSplitLayout::ItemWeight(int32 index) const
{
	if (index < 0 || index >= CountItems())
		return 0;

	return ItemWeight(ItemAt(index));
}
void SocialRecommendSolution::getSolution()
{
	double accu = 0.0;
	cout << "Begin to get social recommendation." << endl;

	//make a item-user inverse table
	vector<list<int>> inverseTable(articleNum + 1);

	int n = userList.size();
	for (int i = 0; i < n; i++)
	{
		int m = userList[i]->pastArticleList.size();
		for (int j = 0; j < m; j++)
		{
			inverseTable[userList[i]->pastArticleList[j]].push_back(userList[i]->id);
		}
	}

	//utilize inversion to count cap between two users
	for (int i = 0; i < userNum; i++)
	{
		list<int>::iterator it1 = inverseTable[i].begin();
		while (it1 != inverseTable[i].end())
		{
			list<int>::iterator it2 = it1;
			it2++;
			while (it2 != inverseTable[i].end())
			{
				userSimMatrix[*it1][*it2]++;
				userSimMatrix[*it2][*it1]++;
				it2++;
			}
			it1++;
		}
	}
	
	/*
	//standardiztion
	for (int i = 0; i < userNum; i++)
	{
		int len1 = userList[i]->pastArticleList.size();
		for (int j = 0; j < i; j++)
		{
			int len2 = userList[j]->pastArticleList.size();
			userSimMatrix[userList[i]->id][userList[j]->id] /= sqrt(len1*len2);
			userSimMatrix[userList[j]->id][userList[i]->id] = userSimMatrix[userList[i]->id][userList[j]->id];
		}
	}
	*/

	//get recommendation weight
	for (int i = 0; i < userNum; i++)
	{
		vector<double> ItemWeight(articleNum+1, 0.0); //article'id begin with 1 not 0
		for (int j = 0; j < userNum; j++)
		{
			int n = userList[j]->pastArticleList.size();
			for (int k = 0; k < n; k++)
			{
				ItemWeight[userList[j]->pastArticleList[k]] += userSimMatrix[userList[i]->id][userList[j]->id];
			}
		}

		int n = userList[i]->alternativeList.size();
		for (int j = 0; j < n; j++)
		{
			userList[i]->alternativeList[j].weight[SOCIAL] = ItemWeight[userList[i]->alternativeList[j].id];
			if(userList[i]->alternativeList[j].weight[SOCIAL] > userList[i]->maxWeght[SOCIAL])
				userList[i]->maxWeght[SOCIAL] = userList[i]->alternativeList[j].weight[SOCIAL];
		}

		userList[i]->maxWeght[SOCIAL] = (userList[i]->maxWeght[SOCIAL] == 0.0 ? 1.0 : userList[i]->maxWeght[SOCIAL]);
		
		//figure accuracy
		vector<WeightArticle> v = getTopN_S(userList[i]->alternativeList, SOCIAL, TOP_N);

		int commom = 0;
		for(int k = 0; k < m_answer[i].size(); k++)
		{
			for(int j = 0; j < m_answer[i].size(); j++)
			{
				if(v[k].id == m_answer[i][j])
					commom++;
			}
		}

		accu += (double)commom / m_answer[i].size();
	}

	accu /= userList.size();
	cout << "Socail recommendation accuracy: " << accu << endl;

	cout << "Social recommendation completed." << endl;
}
Example #3
0
// _ValidateMinMax
void
BSplitLayout::_ValidateMinMax()
{
	if (fHorizontalLayouter != NULL)
		return;

	fLayoutValid = false;

	fVisibleItems.MakeEmpty();
	fHeightForWidthItems.MakeEmpty();

	_InvalidateCachedHeightForWidth();

	// filter the visible items
	int32 itemCount = CountItems();
	for (int32 i = 0; i < itemCount; i++) {
		BLayoutItem* item = ItemAt(i);
		if (item->IsVisible())
			fVisibleItems.AddItem(item);

		// Add "height for width" items even, if they aren't visible. Otherwise
		// we may get our parent into trouble, since we could change from
		// "height for width" to "not height for width".
		if (item->HasHeightForWidth())
			fHeightForWidthItems.AddItem(item);
	}
	itemCount = fVisibleItems.CountItems();

	// create the layouters
	Layouter* itemLayouter = new SimpleLayouter(itemCount, 0);

	if (fOrientation == B_HORIZONTAL) {
		fHorizontalLayouter = itemLayouter;
		fVerticalLayouter = new OneElementLayouter();
	} else {
		fHorizontalLayouter = new OneElementLayouter();
		fVerticalLayouter = itemLayouter;
	}

	// tell the layouters about our constraints
	if (itemCount > 0) {
		for (int32 i = 0; i < itemCount; i++) {
			BLayoutItem* item = (BLayoutItem*)fVisibleItems.ItemAt(i);
			BSize min = item->MinSize();
			BSize max = item->MaxSize();
			BSize preferred = item->PreferredSize();

			fHorizontalLayouter->AddConstraints(i, 1, min.width, max.width,
				preferred.width);
			fVerticalLayouter->AddConstraints(i, 1, min.height, max.height,
				preferred.height);

			float weight = ItemWeight(item);
			fHorizontalLayouter->SetWeight(i, weight);
			fVerticalLayouter->SetWeight(i, weight);
		}
	}

	fMin.width = fHorizontalLayouter->MinSize();
	fMin.height = fVerticalLayouter->MinSize();
	fMax.width = fHorizontalLayouter->MaxSize();
	fMax.height = fVerticalLayouter->MaxSize();
	fPreferred.width = fHorizontalLayouter->PreferredSize();
	fPreferred.height = fVerticalLayouter->PreferredSize();

	fHorizontalLayoutInfo = fHorizontalLayouter->CreateLayoutInfo();
	if (fHeightForWidthItems.IsEmpty())
		fVerticalLayoutInfo = fVerticalLayouter->CreateLayoutInfo();

	if (BView* view = View())
		view->ResetLayoutInvalidation();
}