jeudi 31 octobre 2019

Randomizing the order of an arrayList

The goal of this project is to take in a matrix of courses with prerequisites and print out a schedule with no prereq conflicts. I have gotten as far as adding the courses to the ArrayList of courses and printing it all out, however, im having issues doing a random restart on the ArrayList. I have tried swapping the indexes of the items in the array (although i don't know if im doing it correct), and I have also tried doing a collections.sort() on the ArrayList.

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

// Class DelivC does the work for deliverable DelivC of the Prog340

public class DelivC {

    File inputFile;
    File outputFile;
    PrintWriter output;
    Graph g;
    Node n;
    Edge e;
    int conflicts = -1;
    int counter = 0;
    int minConflicts = Integer.MAX_VALUE;
    String sem1 = "20203: ";
    String sem2 = "20205: ";
    String sem3 = "20211: ";
    String sem4 = "20213: ";
    String sem5 = "20215: ";
    String sem6 = "20221: ";
    String sem7 = "20223: ";
    ArrayList<Node> courseArray = new ArrayList<Node>();

    public DelivC(File in, Graph gr) {
        inputFile = in;
        g = gr;

        // Get output file name.
        String inputFileName = inputFile.toString();
        String baseFileName = inputFileName.substring(0, inputFileName.length() - 4); // Strip off ".txt"
        String outputFileName = baseFileName.concat("_out.txt");
        outputFile = new File(outputFileName);
        if (outputFile.exists()) { // For retests
            outputFile.delete();
        }

        try {
            output = new PrintWriter(outputFile);
        } catch (Exception x) {
            System.err.format("Exception: %s%n", x);
            System.exit(0);
        }

        /**
         * DELIVERABLE C.
         */

        // ADDING COURSES TO COURSEARRAY
        for (int i = 0; i < g.nodeList.size(); i++) {
            courseArray.add(g.nodeList.get(i));
        }

        while (counter < 10) {// conflicts != 0) {
            conflicts = 0;
            // SETTING SEMNUM AND SEM STRING
            for (int i = 0; i < g.nodeList.size(); i++) {
                if (i < 3) {
                    g.nodeList.get(i).setSemNum(1);
                    ;
                    sem1 += g.nodeList.get(i).getName() + " ".toString();
                } else if (i < 6) {
                    g.nodeList.get(i).setSemNum(2);
                    ;
                    sem2 += g.nodeList.get(i).getName() + " ".toString();
                } else if (i < 9) {
                    g.nodeList.get(i).setSemNum(3);
                    ;
                    sem3 += g.nodeList.get(i).getName() + " ".toString();
                } else if (i < 12) {
                    g.nodeList.get(i).setSemNum(4);
                    ;
                    sem4 += g.nodeList.get(i).getName() + " ".toString();
                } else if (i < 15) {
                    g.nodeList.get(i).setSemNum(5);
                    ;
                    sem5 += g.nodeList.get(i).getName() + " ".toString();
                } else if (i < 18) {
                    g.nodeList.get(i).setSemNum(6);
                    ;
                    sem6 += g.nodeList.get(i).getName() + " ".toString();
                } else if (i < 21) {
                    g.nodeList.get(i).setSemNum(7);
                    ;
                    sem7 += g.nodeList.get(i).getName() + " ".toString();
                }
            }

            // CHECKING FOR CONFLICTS IN EDGE
            for (int k = 0; k < g.edgeList.size(); k++) {

                String edgeLabel = g.getEdgeList().get(k).getLabel();
                if (edgeLabel == ">") {
                    if (g.getEdgeList().get(k).getHead().getSemNum() >= g.getEdgeList().get(k).getTail().getSemNum()) {
                        conflicts++;
                    }
                } else if (edgeLabel == ">=") {
                    if (g.getEdgeList().get(k).getHead().getSemNum() > g.getEdgeList().get(k).getTail().getSemNum()) {
                        conflicts++;
                    }
                }
            }

            // PRINTING OUT SCHEDULE
            // if (minConflicts > conflicts) {
            System.out.println(courseArray);
            System.out.println(" ");
            System.out.println("There are currently " + conflicts + " conflicts.");
            System.out.println(sem1);
            System.out.println(sem2);
            System.out.println(sem3);
            System.out.println(sem4);
            System.out.println(sem5);
            System.out.println(sem6);
            System.out.println(sem7);
            System.out.println(" ");
            System.out.println(" ");
            // minConflicts = conflicts;
            // }

            // RESETS THE SEMESTER STRINGS
            sem1 = "20203: ";
            sem2 = "20205: ";
            sem3 = "20211: ";
            sem4 = "20213: ";
            sem5 = "20215: ";
            sem6 = "20221: ";
            sem7 = "20223: ";

            // RESETS THE CONFLICTS
            if (conflicts == 0) {
                conflicts = 0;
            } else {
                conflicts = -1;
            }
            counter++;
            // conflicts = 0;

            // RANDOMLY ASSIGNS NEW SEMESTERS TO EACH COURSE
            // randomRestart();
            Collections.sort(courseArray);
        }

    }

    public void randomRestart() {
        Random r = new Random();
        for (int i = 0; i < courseArray.size(); i++) {
            int nodeIndex = courseArray.indexOf(i);
            int swapIndex = r.nextInt(courseArray.size());
            Node currentNode = courseArray.get(i);
            Node swapNode = courseArray.get(swapIndex);
            Node tempNode = currentNode;
            currentNode = courseArray.get(swapIndex);
            swapNode = tempNode;

        }
    }

}



Aucun commentaire:

Enregistrer un commentaire