0

I have two variables: long[] nextState and List<long[]> TabuList. I use this code to add items to TabuList:

TabuList.add(nextState);

or this:

TabuList.insert(Index, nexState);

but the problem is that after either of these operations, all of TabuList’s items automatically convert to the current value of nextState. my complete code is:

class TabuSearch
    {
        private long[] current { get; set; }
        private double Delta;
        private Random rnd = new Random();
        int foundLists = 0;

        public TabuSearch()
        {
            current = new long[Convert.ToInt32(num_list1)];
        }

        public long[] TabuMOSA3Objectives(long[] c)
        {
            assign(current, c);
            long[] nextState = new long[Convert.ToInt32(num_list1)];
            List<long[]> TabuList = new List<long[]>();
            double proba;
            double alpha = 0.969;
            double temperature = 500.0;
            double epsilon = 0.0001;
            short domination_st;
            int iter = 0;

            while (temperature > epsilon)
            {
                iter++;
                Delta = 1;
                assign(nextState, GenerateNextState(primaryList, current));
                domination_st = CheckDomination3Objective(nextState, current);
                try { var tmp = TabuList.Find(x => x == nextState); if (tmp == null) foundLists = 0; else foundLists = tmp.Count(); }
                catch { }
                    if (foundLists == 0)
                    {
                        if (domination_st > 0)
                        {
                            assign(current, nextState);
                        }
                        else // domination_st < 0
                        {
                            proba = rnd.NextDouble();
                            if (proba < 1 / (1 + Math.Exp(Delta * temperature)))
                            {
                                assign(current, nextState);
                            }
                            else
                            {
                                if (TabuList.Count == 10)
                                    TabuList.RemoveAt(0);
                                assign(nextState, TabuList);
                            }
                        }

                }
                //cooling proces on every iteration
                temperature *= alpha;
            }

            return current;
        }
   } 
          static void assign(long[] c, long[] n)
            {
                for (int i = 0; i < c.Length; i++)
                    c[i] = n[i];
            }
            static void assign(long[] item, List<long[]> list)
            {
                list.Add(item);
            }
  • 1
    didn't get the question, please elaborate, is your data heterogeneous – A.T. Jun 30 '17 at 04:26
  • 1
    You’re probably storing a bunch of references to the same object instead of new objects. See https://stackoverflow.com/questions/1323133/changing-value-of-one-item-in-a-collection-affects-all-duplicate-items, https://stackoverflow.com/questions/26063027/why-is-the-original-object-changed-after-a-copy-without-using-ref-arguments – Ry- Jun 30 '17 at 04:29
  • for example Item is {10, 5, 3}, and Archive is <{0, 10, 1}>, then I run Archive.add(Item). Archive will be change to <{10, 5, 3}, {10, 5, 3}> – user1383711 Jun 30 '17 at 04:29
  • i read the links you sent. but my problem is different from them. i have to run these codes in a function of my class, I mean the "Archive.add" is applied to variable in my class not out of it. I will put complete code of my class and function here. – user1383711 Jun 30 '17 at 05:06
  • What is `GenerateNextState(primaryList, current)`? Provide the contents of this function. – Ahmar Jun 30 '17 at 05:52
  • @user1383711: It’s still the same problem. You’re adding `nextState` to `TabuList` a bunch of times, but it’s always the same array. A minimal fix is `assign((long[])nextState.Clone(), TabuList)`, but it’s important to understand the problem. – Ry- Jun 30 '17 at 05:52
  • @user1383711 You should rather try to `Debug` your program first and see how your values change or do not change. Then you will be able to identify the problem yourself. – Ahmar Jun 30 '17 at 05:55
  • Tank you Ryan. I am not expert in object-oriented programming. I am interested to learn about OOP. I didn't know clone() function, and now I don't know what does it do, but it solved my problem. I will study about it. Thanks – user1383711 Jun 30 '17 at 06:44

0 Answers0