-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecommend_algorithm.cpp
More file actions
189 lines (183 loc) · 6.86 KB
/
Copy pathrecommend_algorithm.cpp
File metadata and controls
189 lines (183 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <QColor>
#include <QList>
#include <QSet>
#include <QPair>
#include <QtMath>
#include <QString>
#include <QSettings>
#include <algorithm>
#include "Cloth.h"
#include "recommend_algorithm.h"
//计算当前衣服颜色与用户偏好颜色的差异
float calculateColorHarmony(const QColor& color1,const QColor& color2){
float hueDiff = qAbs(color1.hueF() - color2.hueF());
if(hueDiff > 0.5f)
hueDiff = 1.0f - hueDiff;
float satDiff = qAbs(color1.saturationF() - color2.saturationF());//计算饱和度差异
float valueDiff = qAbs(color1.valueF() - color2.valueF());//计算明度差异
float harmonyScore = hueDiff * 0.6f + satDiff * 0.2f + valueDiff *0.2f;
return 1.0f - harmonyScore;
}
float calculateMatchScore(const Cloth& item,const QString& occasion,const QString& season,const QString& weather,const int& temperature){
float score = 0.0;
if(occasion == "正式"){
if(item.style() == "正式")//衣服风格的影响
score += 0.8f;
else if(item.style() == "商务")
score += 0.6f;
if(item.pattern() == "纯色" || item.pattern() == "条纹")//衣服图案
score += 0.1f;
else
score -= 0.1f;
if(item.getQColor().valueF() < 0.3f)
score += 0.3f;
else if(item.getQColor().saturationF() < 0.2f)
score += 0.1f;
}
else if(occasion == "商务"){
if(item.style() == "正式")
score += 0.6f;
else if(item.style() == "商务")
score += 0.8f;
if(item.pattern() == "纯色" || item.pattern() == "条纹")
score += 0.1f;
else
score -= 0.1f;
if(item.getQColor().valueF() < 0.3f)
score += 0.3f;
else if(item.getQColor().saturationF() < 0.2f)
score += 0.1f;
}
else if(occasion == "休闲"){
if(item.style() == "休闲" || item.style() == "运动")
score += 0.8f;
if(item.getQColor().saturationF() > 0.5f)
score += 0.3f;
}
else if(occasion == "运动"){
if(item.style() == "运动")
score += 0.8f;
if(item.getQColor().saturationF() > 0.7f)
score += 0.3f;
}
else{
if(item.style() == "美观")
score += 0.8f;
if(item.pattern() == "纯色" || item.pattern() == "印花")
score += 0.1f;
if(item.getQColor().saturationF() > 0.7f)
score += 0.3f;
}
if(item.season() == season)
score += 0.5f;
if(season == "春"){//春季适合柔和的色调
if(item.getQColor().hueF() >= 0.05f && item.getQColor().hueF() <= 0.2f && item.getQColor().saturationF() <= 0.7f)
score += 0.2f;
}
else if(season == "夏"){//夏季适合明亮清爽的色调
if((item.getQColor().hueF() <= 0.4f || item.getQColor().hueF() >= 0.55f) && item.getQColor().valueF() > 0.7f)
score += 0.2f;
}
else if(season == "秋"){//秋季适合暖色调
if(item.getQColor().hueF() >= 0.05f && item.getQColor().hueF() <= 0.15f)
score += 0.2f;
}
else{//冬季适合深色和冷色调
if((item.getQColor().hueF() <= 0.4f || item.getQColor().hueF() >= 0.55f) && item.getQColor().valueF() <= 0.7f)
score += 0.2f;
}
if(weather == "下雨"){
if(item.material() == "化纤" || item.material() == "羊毛")
score += 0.1f;
else if(item.material() == "麻")
score += 0.3f;
}
else if(weather == "下雪"){
if(item.thickness() == "厚")
score += 0.6f;
if(item.material() == "棉" || item.material() == "羊毛")
score += 0.2f;
}
else if(weather == "大风"){
if(item.thickness() == "中" || item.thickness() == "厚")
score += 0.1f;
}
if(temperature < 5){
if(item.thickness() == "厚")
score += 0.8f;
else
score -= 2.0f;
if(item.material() == "棉" ||item.material() == "麻" || item.material() == "羊毛")
score += 0.5f;
}
else if(temperature < 15){
if(item.thickness() == "厚")
score += 0.5f;
else
score -= 1.0f;
if(item.material() == "棉" ||item.material() == "麻" || item.material() == "羊毛")
score += 0.2f;
}
else if(temperature < 28){
if(item.thickness() == "中")
score += 0.5f;
else
score -= 0.5f;
}
else{
if(item.thickness() == "薄")
score += 0.6f;
else if(item.thickness() == "厚")
score -= 2.0f;
if(item.material() == "丝绸" || item.material() == "麻")
score += 0.3f;
}
if(item.type() == Cloth::Dress && temperature >= 22 && temperature <= 36)
score += 2.0f;
return score;
}
QList<Cloth> recommendOutfit(const int& userID,const QString& occasion,const QString& season,
const QString& weather,const int temperature,const QColor& userPreferredColor){
QList<Cloth> wardrobe = Cloth::getUserClothes(userID);
QList<QPair<Cloth,float>> scoredItems;
for(const auto& item : wardrobe){
float score = calculateMatchScore(item,occasion,season,weather,temperature);
scoredItems.append(qMakePair(item,score));
}
if(userPreferredColor.isValid()){
for(auto& pair : scoredItems){
float colorScore = calculateColorHarmony(pair.first.getQColor(),userPreferredColor);
pair.second += colorScore*0.3f;
}
}
std::sort(scoredItems.begin(),scoredItems.end(),
[](const QPair<Cloth,float>&a,const QPair<Cloth,float>& b){
return a.second > b.second;
});
QList<Cloth> recommendedOutfit;
QSet<Cloth::Type> typesAdded;
bool flag1 = true;
bool flag2 = true;
for(const auto& pair : scoredItems){
const Cloth& item = pair.first;
if(!typesAdded.contains(item.type())){
if(flag1 && item.type() == Cloth::Dress){
typesAdded.insert(item.type());
recommendedOutfit.append(item);
flag2 = false;
}
if(flag2 && (item.type() == Cloth::Top || item.type() == Cloth::Under)){
typesAdded.insert(item.type());
recommendedOutfit.append(item);
flag1 = false;
}
if(item.type() == Cloth::Shoes){
typesAdded.insert(item.type());
recommendedOutfit.append(item);
}
}
if(((typesAdded.contains(Cloth::Top) && typesAdded.contains(Cloth::Under))||typesAdded.contains(Cloth::Dress))&&typesAdded.contains(Cloth::Shoes))
break;
}
return recommendedOutfit;
}