Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

import java.util.

Arrays;
import java.util.Random;
public class BuaAn {
public String [] FoodName={"Tao","Cherry","Cam","Quyt","Duahau","Chanh",
"Kiwi","Nho",
"DuaLuoi","DaoTien","Chuoi","DauTay","Thom","Le","SuaBo"
,"CaChua",
"CaRot","CaTim","HanhTay","Toi","KhoaiTay","Nam","BanhMy
","PhoMai",
"Bia","Ngheu","Cua","Ca","Tom","Ga","Bo","Trung"};
public int[][] Food={{0,0,1,1,0,0,0,0,0,0},{0,0,1,0,0,0,1,1,0,0},{0,0,1,
0,0,0,0,0,1,1},{0,0,1,1,1,0,0,0,0,1},
{0,0,1,0,0,0,0,1,0,0},{0,0,0,1,0,0,1,0,0,1},{0,0,1,1,1,0,0,1,0,1
},{0,0,1,0,0,1,0,0,0,0},
{0,0,0,0,0,1,1,0,1,0},{0,0,1,1,0,1,0,0,0,0},{0,0,1,0,0,1,0,0,0,1
},{0,0,1,1,0,0,1,0,0,1},
{0,0,1,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0},{1,1,1,0,1,1,0,1,0,0
},{0,0,1,0,0,1,0,1,0,1},
{0,0,1,1,0,0,1,1,0,0},{0,0,0,0,0,0,0,1,0,0},{0,0,1,0,1,1,0,0,0,0
},{1,0,0,1,0,0,1,0,1,1},
{0,0,1,0,0,0,0,0,0,1},{0,0,1,1,0,0,0,0,0,0},{1,1,1,0,0,0,0,0,0,0
},{1,1,0,0,1,1,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,1,0},{1,0,0,0,1,1,0,0,0,0
},{1,1,0,0,0,0,0,0,1,0},
{1,0,0,0,1,0,1,0,0,0},{1,0,0,0,0,1,0,1,0,0},{1,1,0,0,0,1,0,0,0,0
},{1,1,0,0,1,1,0,1,0,0}};
public int[] FoodValue={31,66,55,70,34,50,106,38,
62,49,55,82,33,13,118,54,
83,15,56,123,32,40,52,63,
18,74,49,62,64,55,70,100};
Random rand = new Random();
int N=100;
int [][] nghiem = new int[N][];
int [] dokhongthichnghi = new int[N];
public static void main(String [] args){
new BuaAn();
}
public BuaAn(){
KhoiTao();
for (int i=0;i<1000;i++){
DanhGia();
System.out.print(i+",");
Print();
LuaChon();
LaiGhep();
DoiBien();
}
}
private void Print() {
int [] temp=this.dokhongthichnghi.clone();
Arrays.sort(temp);
int nguong=temp[0];
System.out.print(nguong+",");
for (int i=0;i<N;i++){

if (this.dokhongthichnghi[i]==nguong){
for (int j=0;j<nghiem[i].length;j++)
if (this.nghiem[i][j]==1)
System.out.print(FoodName[j]+","
);
System.out.println();
break;
}
}
}
private void DoiBien() {
int index=rand.nextInt(N);
int bit=rand.nextInt(FoodName.length);
this.nghiem[index][bit]=1-this.nghiem[index][bit];
}
private void LaiGhep() {
for (int i=0;i<N/4;i++){
int vo=rand.nextInt(N);
int chong=rand.nextInt(N);
for (int j=0;j<this.nghiem[vo].length;j++){
if (rand.nextInt(2)==1){
int temp = nghiem[vo][j];
nghiem[vo][j] = nghiem[chong][j];
nghiem[chong][j]= temp;
}
}
}
}
private void LuaChon() {
int [] temp=this.dokhongthichnghi.clone();
Arrays.sort(temp);
int nguong=temp[N*80/100];
for (int i=0;i<N;i++){
if (this.dokhongthichnghi[i]>nguong){
this.nghiem[i]=this.nghiem[rand.nextInt(N)].clon
e();
}
}
}
private void DanhGia() {
for (int i=0;i<N;i++){
//Gia tien
this.dokhongthichnghi[i]=0;
for (int j=0;j<this.nghiem[j].length;j++){
this.dokhongthichnghi[i]+=
this.nghiem[i][j]*FoodValue[j];
}
//Phat vi thieu chat
for (int k=0;k<10;k++){
boolean cochat=false;
for (int j=0;j<this.nghiem[i].length;j++){

if (this.nghiem[i][j]==1 &&
Food[j][k]==1){
cochat=true;
break;
}
}
if (!cochat) this.dokhongthichnghi[i]+=30;
}
}

}
private void KhoiTao() {
for (int i=0;i<N;i++){
this.nghiem[i]=new int[FoodName.length];
for (int j=0;j<this.nghiem[i].length;j++){
this.nghiem[i][j]=rand.nextInt(2);
}
}
}
}

You might also like