Пример #1
0
        private void fillColumns()
        {
            currentRow = 0;

            while (currentRow < nbrOfSamples)
            {
                int[][] orderArray = null;
                if (!allowDiffVoterOrder)
                {
                    int[,] availOrders = SupportFunction.GetPermutations(nbrOfNominees);
                    int[] voterPreferenceDistributon = SupportFunction.CreatePreferenceDistribution(availOrders.GetLength(0), nbrOfVoters, rnd);
                    orderArray = SupportFunction.CreateOrderArray(availOrders, voterPreferenceDistributon, RandomOrder(nbrOfVoters));
                }

                // Fill current row
                int[] order = null;
                for (int currentVoter = 0; currentVoter < nbrOfVoters; currentVoter++)
                {
                    if (!allowDiffVoterOrder)
                    {
                        order = orderArray[currentVoter];
                    }
                    fillRankingColumns(nbrOfNominees * currentVoter, order);
                }
                currentRow++;

                // Add last row again with differenet sorting of voters
                if (allowDiffVoterOrder)
                {
                    AddExtraRows();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns an array of arrays with all the possible permutations of the input
        /// </summary>
        /// <param name="nbrOfNominees"></param>
        public static int[,] GetPermutations(int numbers)
        {
            int[] numberArray = new int[numbers];
            for (int i = 0; i < numbers; i++)
            {
                numberArray[i] = i + 1;
            }

            return(SupportFunction.GetPermutations(numberArray));
        }
Пример #3
0
        private bool orderIsOkay()
        {
            int[,] availOrders = SupportFunction.GetPermutations(nbrOfNominees);
            int[] voterPreferenceDistributon = new int[availOrders.GetLength(0)];
            for (int prefID = 0; prefID < voterPreferenceDistributon.Length; prefID++)
            {
                voterPreferenceDistributon[prefID] = 0;
            }
            // Loop through current row, and count the different preference orders
            int[][] currentOrderArray = new int[nbrOfVoters][];
            for (int voterID = 0; voterID < nbrOfVoters; voterID++)
            {
                // Create Order Array from current Row
                currentOrderArray[voterID] = new int[nbrOfNominees];
                for (int nomineeID = 0; nomineeID < nbrOfNominees; nomineeID++)
                {
                    currentOrderArray[voterID][nomineeID] = data[currentRow, nbrOfNominees *voterID + nomineeID];
                }
                // Find the ID of the matching order
                for (int orderID = 0; orderID < availOrders.GetLength(0); orderID++)
                {
                    bool isMatch = true;
                    for (int nomineeID = 0; nomineeID < nbrOfNominees; nomineeID++)
                    {
                        if (currentOrderArray[voterID][nomineeID] != availOrders[orderID, nomineeID])
                        {
                            isMatch = false;
                            break;
                        }
                    }
                    if (isMatch)
                    {
                        voterPreferenceDistributon[orderID]++;
                        break;
                    }
                }
            }
            // Get the allowed order array
            int[][] allowedOrderArray = SupportFunction.CreateOrderArray(availOrders, voterPreferenceDistributon, RandomOrder(nbrOfVoters));
            // Check whether the two order array match
            bool isOkay = true;

            for (int orderID = 0; orderID < allowedOrderArray.Length; orderID++)
            {
                for (int nomineeID = 0; nomineeID < allowedOrderArray[orderID].Length; nomineeID++)
                {
                    if (allowedOrderArray[orderID][nomineeID] != currentOrderArray[orderID][nomineeID] - 1)
                    {
                        isOkay = false;
                        break;
                    }
                }
            }
            return(isOkay);
        }
Пример #4
0
        /// <summary>
        /// Returns an array of arrays with all the possible permutations of the input
        /// </summary>
        /// <param name="nbrOfNominees"></param>
        public static int[,] GetPermutations(int[] numbers)
        {
            int[,] output = new int[SupportFunction.Factorial(numbers.Length), numbers.Length];

            if (numbers.Length == 1)
            {
                output[0, 0] = numbers[0];
            }
            else
            {
                int[] lessNumbers = new int[numbers.Length - 1];
                for (int i = 0; i < numbers.Length - 1; i++)
                {
                    lessNumbers[i] = numbers[i + 1];
                }
                int[,] lessOutput = SupportFunction.GetPermutations(lessNumbers);

                int rowCounter = 0;
                for (int i = 0; i < lessOutput.GetLength(0); i++)
                {
                    // Loop through the positions of the new number
                    for (int j = 0; j < lessOutput.GetLength(1) + 1; j++)
                    {
                        output[rowCounter, j] = numbers[0];
                        // Copy the other numbers in the remaining spaces
                        for (int k = 0; k < lessOutput.GetLength(1); k++)
                        {
                            if (k < j)
                            {
                                output[rowCounter, k] = lessOutput[i, k];
                            }
                            if (k >= j)
                            {
                                output[rowCounter, k + 1] = lessOutput[i, k];
                            }
                        }
                        rowCounter++;
                    }
                }
            }

            return(output);
        }
Пример #5
0
        private void saveFiles(string extension)
        {
            StreamWriter currentWriter;

            try
            {
                string filename = Properties.Settings.Default.MainPath + Properties.Settings.Default.ResultPath;
                filename += timeStamp()
                            + nbrOfNominees.ToString().PadLeft(2, '0') + "jelolt_"
                            + nbrOfVoters.ToString().PadLeft(2, '0') + "szavazo_"
                            + nbrOfSamples.ToString() + "minta_"
                            + inputSeed.ToString() + "seed"
                            + txtDescription.Text;

                currentWriter = new StreamWriter(filename + "." + extension, false, Encoding.Default);

                for (int row = 0; row < data.GetLength(0); row++)
                {
                    int currentNbrOfColumns;
                    if (!isBinary)
                    {
                        currentNbrOfColumns = data.GetLength(1);
                    }
                    else
                    {
                        currentNbrOfColumns = binaryData.GetLength(1);
                    }
                    for (int col = 0; col < currentNbrOfColumns; col++)
                    {
                        if (col != 0)
                        {
                            currentWriter.Write(";");
                        }
                        if (!isBinary)
                        {
                            currentWriter.Write(data[row, col].ToString());
                        }
                        else
                        {
                            currentWriter.Write(binaryData[row, col].ToString());
                        }
                    }
                    if (extension != "noncond")
                    {
                        for (int col = 0; col < result.GetLength(1); col++)
                        {
                            currentWriter.Write(";" + result[row, col].ToString());
                        }
                    }
                    currentWriter.Write("\n");
                }
                currentWriter.Close();
                if (rdbSum.Checked)
                {
                    SupportFunction.ConvertToSum(filename, "." + extension, nbrOfNominees, nbrOfVoters);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error with saving the files!");
            }
        }
Пример #6
0
        // ONLY WORKS FOR BINARY CODING!!!!!!!!!!!!!!!!!!
        private double[,] getCurrentData(string fileName)
        {
            Console.WriteLine(fileName);

            StreamReader sr    = new StreamReader(resultPath + fileName, Encoding.Default);
            StreamReader srSum = null;

            if (fileName.Contains("_SUM"))
            {
                string sumFileName = fileName.Substring(0, fileName.IndexOf("_SUM")) + ".noncond";
                srSum = new StreamReader(resultPath + sumFileName, Encoding.Default);
            }

            int rows = 0;
            int cols = (nbrOfVoters * nbrOfNominees * (nbrOfNominees - 1) / 2) + nbrOfNominees;

            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine(); rows++;
            }
            sr.BaseStream.Position = 0;
            sr.DiscardBufferedData();

            rows           = Math.Min(1000, rows);
            double[,] data = new double[rows, cols + nbrOfNominees * nbrOfVotingRules];
            permutations   = SupportFunction.GetPermutations(nbrOfNominees);

            int currentRow = 0;

            while (!sr.EndOfStream)
            {
                if (currentRow == rows)
                {
                    break;
                }
                int      currentCol    = cols;
                double[] currentValues = new double[data.GetLength(1)];
                string[] currentLine   = sr.ReadLine().Split(';');
                for (int i = 0; i < currentLine.Length; i++)
                {
                    currentValues[cols - currentLine.Length + i] = Convert.ToDouble(currentLine[i]);
                }
                if (srSum != null)
                {
                    currentLine = srSum.ReadLine().Split(';');
                    for (int i = 0; i < currentLine.Length; i++)
                    {
                        currentValues[i] = Convert.ToDouble(currentLine[i]);
                    }
                }

                createComparsionMatrices(currentValues);
                getCondrocet(ref currentValues, currentCol); currentCol       += nbrOfNominees;
                getCopeland(ref currentValues, currentCol); currentCol        += nbrOfNominees;
                getBorda(ref currentValues, currentCol); currentCol           += nbrOfNominees;
                getPlurality(ref currentValues, currentCol); currentCol       += nbrOfNominees;
                getKemeny(ref currentValues, currentCol); currentCol          += nbrOfNominees * 2;
                getPluralityRunOff(ref currentValues, currentCol); currentCol += nbrOfNominees;
                getNApproval(ref currentValues, ref currentCol);
                getUnanimity(ref currentValues, currentCol); currentCol += nbrOfNominees;
                getPareto(ref currentValues, currentCol); currentCol    += nbrOfNominees;

                for (int i = 0; i < currentValues.Length; i++)
                {
                    data[currentRow, i] = currentValues[i];
                }
                currentRow++;
            }

            sr.Close();
            if (srSum != null)
            {
                srSum.Close();
            }

            //return data;

            double[,] newData = new double[rows, nbrOfNominees *(nbrOfVotingRules + 1)];

            for (int i = 0; i < newData.GetLength(0); i++)
            {
                for (int j = 0; j < newData.GetLength(1); j++)
                {
                    newData[i, j] = data[i, j + cols - nbrOfNominees];
                }
            }

            return(newData);
        }