Need some advice

This is a discussion on Need some advice within the C++ Programming forums, part of the General Programming Boards category; I've been racking my brain and changing/editing this one section of codes for hours now and i can't figure out ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    Need some advice

    I've been racking my brain and changing/editing this one section of codes for hours now and i can't figure out why it doesn't work.

    Basically it seems to clear my array for no apparent reason.

    output is:

    1
    10
    10
    10
    0
    Mlist // function

    Action Selection // another function

    Action List // yet another function somehow my array is cleared.

    0
    0
    0
    0
    0

    Here is the code, yes it's not great and yes Strings good Arrays bad .
    Anyway good luck and thanks for any help

    Code:
    // Learning Classifier Systems YCS on Protein secondary structure
    #define POPSize 27000
    #define AAlgh 186
    #define PWild 50 // 90% to high 80-85 works
    #include <time.h>
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int Pay = 10, Err = 10, Niche = 10;
    
    
    
    
    
    int Initialize(int POPinit[POPSize][AAlgh])
    {
    // create a Population of classifiers representing 9 amino acid units
    // each unit is 20 int long to represent the 20 different amino acids
    // eg: [00001000000000000000] = AA 5
    // each classifier contains a Payoff prediction, Error prediction, Niche size
    // estimate and fitness tagged on to the end.
    
    // Fill Pop
    int i; int j; int AminoAcid;
    char check = 'Y'; 
    
    
    for (i = 0; i < POPSize; i++)
    {
        
        for ( j = 0; j < AAlgh-6; j++)
        {
            if (check != 'N')
            {
                AminoAcid = (j + rand()%20); // random number 0-19, pick amino acid
                check = 'N';
                for (j; j < AminoAcid; j++) // fill up to AA
                {
                    POPinit[i][j] = 7;
              
                }
                POPinit[i][j] = 1;
             
            }
            else
            {
            
              POPinit[i][j] = 0;
             
            }  
           if (j == 19 || j == 39 || j == 59 || j == 79 || j == 99 || j == 119
                  || j == 139 || j == 159 || j == 179)
                  // Let rule fill up with 0's until it reachs end
                  {
                      check = 'Y';
                  }
                else
                {
                    //do nothing
                }            
                  
        }      
      
        check = 'Y';
       
    }             
    // edit each rule with generality '#' also add Parameter initialisation
    for ( i = 0; i < POPSize; i++)
    {
        for (j = 0; j < AAlgh-6; j++)
        {
            if ( POPinit[i][j] != 1)
            {
                if (rand()%100 > PWild)
                {
                    POPinit[i][j] = 4;
                }
            }
            cout << POPinit[i][j];
        }
        cout << endl;
        // AAlgh -6 is nothing so far
       POPinit[i][AAlgh-5] = rand()%3+1; // Action
       POPinit[i][AAlgh-4] = 10; // Payoff
       POPinit[i][AAlgh-3] = 10; // Error
       POPinit[i][AAlgh-2] = 10; // Niche
       POPinit[i][AAlgh-1] = 0; // Fitness
    }                
    
    
    
    cin.get();
    
    }
    
    // PERFORMANCE
    
    int MatchList(int POPinit[POPSize][AAlgh], int Mlist[100][AAlgh], int &M)
    {
    // Get input from environment then scan population and form matchlist
    int i, j, l, c = 0;
    int matched;
    char covercheck = 'Y';
    
    
    int Env[180] = {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
            0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
            0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0};
    
    
    for (i = 0; i < POPSize; i++)
    {
        matched = 0;
        for (j = 0; j < AAlgh-6; j++) // only need to scan Amino Acids
        { 
             
            if (Env[j] == 1 && (POPinit[i][j] == 1 || POPinit[i][j] == 4))
            {
                 
               matched++;
                if (matched == 9) // make sure all nine AA in condition match
                {
                    
                    
                    for (l = 0; l < AAlgh; l++) // need to copy whole rule
                    {
                        Mlist[M][c] = POPinit[i][l];
                        c++;
                        covercheck = 'N';
                    }   
                    
                  M++;
                  c = 0;
                  matched = 0; // clear match for next run
                }    
                
            }  
            else 
            {
             // do nothing
            }            
        }    
     
    }
    // Covering operator if no match to input, new classifier for each Output ie 3
    if (covercheck != 'N')
    {
        for (i = 0; i < 3; i++)
        {
        for ( j = 0; j < AAlgh-6; j++) // only need to fill amino acid
        {
            if(rand()%100 > PWild)
            {
                if (Env[j] != 1)
                {
                    Mlist[0][j] = 4; // Add # to string
                }
                else
                {
                    Mlist[0][j] = 1;
                }        
            }
            else
            {
                Mlist[0][j] = Env[j];    
            }    
            
        }
        // add action + other parameters
       Mlist[0][AAlgh-5] = 1; // add action
       Mlist[1][AAlgh-5] = 2; // add action                
       Mlist[2][AAlgh-5] = 3; // add action
       
       Mlist[i][AAlgh-4] = 10; // Payoff
       Mlist[i][AAlgh-3] = 10; // Error
       Mlist[i][AAlgh-2] = 10; // Niche
       Mlist[i][AAlgh-1] = 0; // Fitness
        
        M++;
        }    
      
    }        
    
    
    cout << "filled"<< endl;
    cout << M;
    cin.get();
    
    
    for (i = 0; i < M; i++)
    {
        for ( j = 0; j < AAlgh; j++)
        {
            cout << Mlist[i][j];
        }
        cout << endl;    
    
    
    }    
          cout << Mlist[0][AAlgh-5] << endl;
          cout << Mlist[0][AAlgh-4] << endl;
          cout << Mlist[0][AAlgh-3] << endl;
          cout << Mlist[0][AAlgh-2] << endl;
          cout << Mlist[0][AAlgh-1] << endl;
    cout << "Mlist" << endl;
    cin.get();
    
    
    }
    
    int ActionSelection(int &ActSel) // explore/exploit scheme
    {
        cout << "Action Selection" << endl;
        cin.get();
        
        //Explore
        if (ActSel == 1)
        {
            
            ActSel = 0;
        }
        else // Exploit
        {
            ActSel = 1;
        }
        
    }        
            
    int ActionList(int &M, int &A, int Alist[100][AAlgh], int Mlist[100][AAlgh], int &ActSel, int &ActChoice)
    {
        int index, i, j;
        int PopSort[AAlgh]; 
        A = 0;
        cout << "Action List" << endl;
        cin.get();
        cout << Mlist[0][AAlgh-5] << endl;
        cout << Mlist[0][AAlgh-4] << endl;
          cout << Mlist[0][AAlgh-3] << endl;
          cout << Mlist[0][AAlgh-2] << endl;
          cout << Mlist[0][AAlgh-1] << endl;
          
          cin.get();
        
        
        if (ActSel == 1)
        {
            ActChoice = rand()%3+1; // ie 1-3
        }
        else // choose highest payoff using Bubble sort
        {
           
          for (i = 0; i < M; i++)
          {
           for (index = 0; index < M-1; index++)
           {
               if (Mlist[index][AAlgh-4] > Mlist[index+1][AAlgh-4]) // payoff
               {
                   for ( j = 0; j < AAlgh; j++)
                   {
                       PopSort[j] = Mlist[index+1][j];
                       Mlist[index+1][j] = Mlist[index][j];
                       Mlist[index][j] = PopSort[j];
                   }                   
                                                                                            
               }   
             }
           }
           ActChoice = Mlist[M][AAlgh-5]; // pick classifier with highest payoff
         }        
        cout << "Choice made: " << ActChoice; 
        for (index = 0; index < M; index++) // check through whole matchlist
        {
          cout << "for loop" << endl;
          cout << Mlist[index][AAlgh-5] << endl;
          cout << Mlist[index][AAlgh-4] << endl;
          cout << Mlist[index][AAlgh-3] << endl;
          cout << Mlist[index][AAlgh-2] << endl;
          cout << Mlist[index][AAlgh-1] << endl;
          
          cin.get();
                switch (Mlist[index][AAlgh-5])
                {
                    case 1: // Scan Mlist and copy all classifiers with action 1
                        {
                            if ( ActChoice == 1)
                            {
                            
                                for (j = 0; j < AAlgh; j++)
                                {
                                    Alist[A][j] = Mlist[index][j];
                                    cout << Alist[A][j];
                                   
                                }
                                 A++; // so we know size of Action List
                                cout << endl;
                            }
                               
                            else
                            {
                                // do nothing
                            }
                            break;
                        }  
                    case 2: // Scan Mlist and copy all classifiers with action 2
                        {
                            if ( ActChoice == 2)
                            {
                            
                                for (j = 0; j < AAlgh; j++)
                                {
                                    Alist[A][j] = Mlist[index][j];
                                    cout << Alist[A][j];
                                    
                                }
                                A++;
                                cout << endl;
                            }
                                    
                            else
                            {
                                // do nothing
                            }
                            break;
                        } 
                    case 3: // Scan Mlist and copy all classifiers with action 3
                        {
                            if ( ActChoice == 3)
                            {
                            
                                for (j = 0; j < AAlgh; j++)
                                {
                                    Alist[A][j] = Mlist[index][j];
                                    cout << Alist[A][j];
                                   
                                }
                                 A++;
                                cout << endl;
                            }
                                 
                            else
                            {
                                // do nothing
                            }
                            break;
                    default:
                        break;
                        }       
                    }
          }    
                                                             
                                          
               
                 
        
          
             
              
    
    }
    
    int main()
    {
    srand ( time(NULL) );
    static int POPinit[POPSize][AAlgh]; // 180 for AA, 1 for action,  5 for Parameters etc
    int Env[AAlgh];
    int Mlist[100][AAlgh];
    int Alist[100][AAlgh];
    int ActSel = 0;
    int ActChoice;
    int M = 0, A = 0;
    
    
    
    
    Initialize(POPinit);
    MatchList(POPinit, Mlist, M);
    ActionSelection(ActSel);
    ActionList(M, A, Mlist, Alist, ActSel, ActChoice);
    cin.get();
     
    }

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Use <cstdlib> and <ctime>. Use void if your functions don't return a value. The Env is not used.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thanks, any idea about the original question?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Don't show us over 100 lines of code and some cryptic text describing your problem. The secret for a good answer is learning to ask. We don't want to see all your program and we don't want to search over 100 lines of code. We also don't want to guess what do you mean with "Action Selection // another function" and other crazy things you wrote.

    Show us the bit of code where you think the problem occurs.

    Oh, and btw... I really have to ask. Did you at least tried debugging your code to see why your array is being "deleted"?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You ever think about stepping through the program with a debugger to see where it begins to do what you do not expect it to do?

    And what did you really mean for this line to do?:

    Code:
     for (j; j < AminoAcid; j++)
    Also, if you are not intending on returning a value from a function, declare its return type as void, eg.,

    Code:
    void Initialize(int POPinit[POPSize][AAlgh]) // etc

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    And what did you really mean for this line to do?:
    Sorry it prb should have been j = j; essentially fills POPinit with 7's until it reaches aminoacid length.

    Also, if you are not intending on returning a value from a function, declare its return type as void, eg.,
    Yes, i thought my error might have something to do with having void, plus it's a bad habit i've picked up aswell.

    Don't show us over 100 lines of code and some cryptic text describing your problem. The secret for a good answer is learning to ask. We don't want to see all your program and we don't want to search over 100 lines of code. We also don't want to guess what do you mean with "Action Selection // another function" and other crazy things you wrote.
    Cheerful as always Mario

    But your right i should have been more clearer.

    There are 3 functions at work here. first Matchlist, then ActionSelection, then ActionList.

    The functions are quite long so i won't print them out again in full but.

    Here is what i don't quite understand.
    Code:
    for (i = 0; i < M; i++)
    {
        for ( j = 0; j < AAlgh; j++)
        {
            cout << Mlist[i][j];
        }
        cout << endl;    
    
    
    }    
          cout << Mlist[0][AAlgh-5] << endl;
          cout << Mlist[0][AAlgh-4] << endl;
          cout << Mlist[0][AAlgh-3] << endl;
          cout << Mlist[0][AAlgh-2] << endl;
          cout << Mlist[0][AAlgh-1] << endl;
    cout << "Mlist" << endl;
    cin.get();
    
    
    }
    Here is the end of the Matchlist function, cout will print out said above correctly.
    1
    10
    10
    10
    0

    next comes the ActionSelection function

    Code:
    int ActionSelection(int &ActSel) // explore/exploit scheme
    {
        cout << "Action Selection" << endl;
        cin.get();
        
        //Explore
        if (ActSel == 1)
        {
            
            ActSel = 0;
        }
        else // Exploit
        {
            ActSel = 1;
        }
        
    }
    from what i can tell nothing affect Mlist here.

    and Last is the beginning of ActionList function.

    Code:
    int ActionList(int &M, int &A, int Alist[100][AAlgh], int Mlist[100][AAlgh], int &ActSel, int &ActChoice)
    {
        int index, i, j;
        int PopSort[AAlgh]; 
        A = 0;
        cout << "Action List" << endl;
        cin.get();
        cout << Mlist[0][AAlgh-5] << endl;
        cout << Mlist[0][AAlgh-4] << endl;
          cout << Mlist[0][AAlgh-3] << endl;
          cout << Mlist[0][AAlgh-2] << endl;
          cout << Mlist[0][AAlgh-1] << endl;
          
          cin.get();
    here the output is:
    0
    0
    0
    0
    0

    Somehow the stored integers have disappeared and i'am baffled how. no doubt something simple.

    Mario i appreciate the help, but you come on a bit aggressive at times, I'am new at this so i would be thankful if you wouldn't kill me the first chance you get .

    Regards Wolfe

    and regarding debugging, well i don't quite know what you mean, my debugging as you can see is just adding cout to see if the code does what i think it should, perhaps not to effective

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    At a glance, when you call ActionList, you pass Mlist then Alist:
    Code:
    ActionList(M, A, Mlist, Alist, ActSel, ActChoice);
    But the prototype for Actionlist is this:
    Code:
    int ActionList(int &M, int &A, int Alist[100][AAlgh], int Mlist[100][AAlgh], int &ActSel, int &ActChoice)
    Here Alist comes first. At the very least, it will cause some confusion.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Genious, to be honest i'd never thought passing parameters and the order ever mattered, I had never read otherwise.

    Anyway thank you very much JaWiB

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Cdrwolfe
    Genious, to be honest i'd never thought passing parameters and the order ever mattered, I had never read otherwise.
    You can read a bit more about such things here. Keep in mind that the material is for the C language, and not C++, though it should not be any trouble for you.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Quote Originally Posted by Cdrwolfe
    Mario i appreciate the help, but you come on a bit aggressive at times, I'am new at this so i would be thankful if you wouldn't kill me the first chance you get .
    Oh. That's not aggressive. Agressive is... well, better if I don't.

    But I do have a memory. I remember for instance you being told several times to avoid arrays. And I seem to remember you even being given a link to container tutorials. And yet, you always seem to manage comming back with arrays and... guess what... problems deriving from their use. I also see in your code the same mistakes that caused other posts of yours. I have this notion, probably wrong (hopefully wrong) that you really aren't making an effort. You just want us to give you a solution to your problem, whatever the problem may be at that time. Everything else you don't care.

    and regarding debugging, well i don't quite know what you mean, my debugging as you can see is just adding cout to see if the code does what i think it should, perhaps not to effective
    No, it's not effective. In fact, it will not work under many circumstances.

    Search Google for "c++ debug tutorial". You will find heaps. Debugging is the meat of programming. Programming is debugging too. A major part of problem solving and dealing with errors in the way you tried to solve a given problem, and programming is about problem solving. You will not go anywhere in programming if you don't take a break and learn about debugging and some of its techniques.

    The most fundamental are breaks, steps, and watches.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Sorry if my use of arrays offends you Mario, i just find them easier to handle and understand. Moreso then strings at the moment.

    You just want us to give you a solution to your problem, whatever the problem may be at that time. Everything else you don't care.
    I didn't realise asking help was a crime, i'am not forcing anyone to answer it, If you think i just came running here when the error first occured then your wrong.
    I hadn't read that passing of parameters had to be in the same order.

    I thought someone here who is more knowledgable would know an Answer, and be helpful enough to give one. I also didn't think it would be to hard which was probably a mistake.

    Thanks to JaWiB i hope not to make that mistake again.

    I Appreciate any Help given and i'll look over the debugging links given, maybe someday i'll be able to answer a few questions you never know .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 07:38 PM
  3. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 02:06 PM
  4. need hardware advice....
    By forgottenPWord in forum Tech Board
    Replies: 5
    Last Post: 03-23-2003, 09:12 AM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21