Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<iomanip>
- #include<time.h>
- #include<stdlib.h>
- #define C 50
- #define NOB 10
- ///NOB numar obiecte
- using namespace std;
- int ob[]= {4,14,9,18,20,7,12,5,1,33};
- int eval(int v[]) {
- int prod=0;
- for(int i=0; i<NOB; i++)
- prod+=ob[i]*v[i]; ///calculam produsul scalar intre v si ob
- //cout<<"prod="<<prod<<endl;
- return abs(C-prod);///cat din rucsac mai ramane gol
- }
- int main() {
- int v[NOB];
- srand(time(NULL));
- int evaluareOptima=100;///trebuie sa tinda la 0
- while(evaluareOptima!=0) {
- for(int i=0; i<NOB; i++)
- v[i]=rand()%2;
- for(int k = 0; k < 100; k++) {
- int v2[NOB];
- for(int i=0; i<NOB; i++)
- v2[i]=v[i];
- int p=rand()% NOB;
- v2[p]=1-v2[p];
- //cout<<endl;
- //cout<<eval(v2)<<endl;
- if(eval(v2) < eval(v)) {
- for(int i=0; i<NOB; i++)
- v[i]=v2[i];
- }
- }
- evaluareOptima =eval(v);
- }
- for(int i=0; i<NOB; i++)
- cout<<setw(4)<<ob[i]<<" ";
- cout<<endl;
- for(int i=0; i<NOB; i++)
- cout<<setw(4)<<v[i]<<" ";
- cout<<endl;
- cout<<"Evaluare: "<<eval(v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement