⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randommvamodelgenerator.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            case CLOSED_MODEL:
                for (int c = 0; c < classes; c++) {
                    classTypes[c] = CLASS_CLOSED;
                }
                break;
            case OPEN_MODEL:
                for (int c = 0; c < classes; c++) {
                    classTypes[c] = CLASS_OPEN;
                }
                break;
            case MIXED_MODEL:
                for (int c = 0; c < classes; c++) {
                    if (randomGenerator.nextDouble() < 0.5) {
                        classTypes[c] = CLASS_CLOSED;
                    } else {
                        classTypes[c] = CLASS_OPEN;
                    }
                }
                break;
        }
    }



    private void calcModelType() {

        closed = open = false;

        for (int c = 0; c < classes; c++) {
            if (classTypes[c] == CLASS_CLOSED) {
                closed = true;
            } else if (classTypes[c] == CLASS_OPEN) {
                open = true;
            }
        }

        if (closed && open) {
            //both open and closed classes -> mixed = true
            mixed = true;
            closed = open = false;
        }

    }


    private void createClassData() {

        classData = new double[classes];


        switch (modelType) {
            case CLOSED_MODEL:
                //only closed classes

                for (int c = 0; c < classes; c++) {
                    //computes total think time (i.e. the sum of all delays for this class)
                    double z = 0.0;
                    double maxD = 0.0;
                    double temp;

                    for (int s = 0; s < stations; s++) {
                        temp = serviceTimes[s][c][0] * visits[s][c];

                        //computes Dmax
                        if (temp > maxD) {
                            maxD = temp;
                        }

                        if (stationTypes[s] == STATION_DELAY) {
                            z += temp;
                        }

                    }

                    //the chosen population is influenced by think time and Dmax
                    classData[c] = Math.floor(50 * randomGenerator.nextDouble() + z / maxD + 1);

                }
                break;

            case OPEN_MODEL:

                //only open classes

                for (int c = 0; c < classes; c++) {
                    //sets lambda from [0,1)
                    classData[c] = randomGenerator.nextDouble();
                }

                //computes the actual Umax
                double Umax = 0.0;

                for (int s = 0; s < stations; s++) {

                    double Ustation = 0.0;

                    //do not consider delay stations
                    if (stationTypes[s] == STATION_LI) {
                        for (int c = 0; c < classes; c++) {
                            Ustation += classData[c] * visits[s][c] * serviceTimes[s][c][0];
                        }

                        //is Umax?
                        if (Ustation > Umax) {
                            Umax = Ustation;
                        }
                    }
                }



                //chooses the wanted Umax*, included in [U_min_bottleneck , U_max_bottleneck] (example: [0.1 , 0.7])
                double Umax_desired = U_min_bottleneck + randomGenerator.nextDouble() * (U_max_bottleneck - U_min_bottleneck);

                //then scales all the service times of LI stations in order to obtain that U*
                double scaleFactor = Umax_desired / Umax;

                for (int s = 0; s < stations; s++) {
                    if (stationTypes[s] == STATION_LI) {
                        for (int c = 0; c < classes; c++) {
                            serviceTimes[s][c][0] *= scaleFactor;
                        }
                    }
                }
                break;

            case MIXED_MODEL:

                //both open and closed classes

                //first of all, sets the lambda
                for (int c = 0; c < classes; c++) {

                    if (classTypes[c] == CLASS_OPEN) {
                        //sets lambda from [0,1)
                        double rand;
                        do {
                            rand = randomGenerator.nextDouble();
                            classData[c] = rand;
                        } while(rand == 0);
                    }
                }

                //computes the actual Umax
                double Umax2 = 0.0;

                for (int s = 0; s < stations; s++) {

                    double Ustation = 0.0;
                    //do not consider delay stations
                    if (stationTypes[s] == STATION_LI) {
                        for (int c = 0; c < classes; c++) {
                            if (classTypes[c] == CLASS_OPEN) {
                                Ustation += classData[c] * visits[s][c] * serviceTimes[s][c][0];
                            }
                        }
                        //is Umax?
                        if (Ustation > Umax2) {
                            Umax2 = Ustation;
                        }
                    }
                }

                //if only closed class are present, Umax2 will be 0
                //therefore there is no need to rescale service times

                //if Umax2 > 0 rescale service times 
                if (Umax2 > 0) {


                    //chooses the wanted Umax*, included in [U_min_bottleneck , U_max_bottleneck] (example: [0.1 , 0.7])
                    double Umax_desired2 = U_min_bottleneck + randomGenerator.nextDouble() * (U_max_bottleneck - U_min_bottleneck);

                    //then scales all the service times in order to obtain that U*
                    double scaleFactor2 = Umax_desired2 / Umax2;

                    for (int s = 0; s < stations; s++) {
                        if (stationTypes[s] == STATION_LI) {
                            for (int c = 0; c < classes; c++) {
                                serviceTimes[s][c][0] *= scaleFactor2;
                            }
                        }
                    }

                }


                //now consider closed classes
                for (int c = 0; c < classes; c++) {
                    if (classTypes[c] == CLASS_CLOSED) {

                        double z = 0.0;
                        double maxD = 0.0;
                        double temp;

                        for (int s = 0; s < stations; s++) {
                            temp = serviceTimes[s][c][0] * visits[s][c];

                            if (temp > maxD) {
                                maxD = temp;
                            }

                            z += temp;
                        }

                        classData[c] = Math.floor(50 * randomGenerator.nextDouble() + z / maxD + 1);

                    }
                }
                break;

        }

    }



	/**
	 * Creates a DOM representation of this object
     *
	 */
	public void createDocument() {
		Document root;
		try {
			DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			root = dbf.newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException pce) {
			throw new RuntimeException(pce);
		}

		/* model */
		Element modelElement = root.createElement("model");
		//TODO: xmlns:xsi ??
        modelElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

        //TODO: xsi:noNamespace... ??
        //modelElement.setAttribute("xsi:noNamespaceSchemaLocation", "JMTmodel.xsd");
        modelElement.setAttribute("xsi:noNamespaceSchemaLocation",
                XSDSchemaLoader.loadSchema(XSDSchemaLoader.JMVA_MODEL_DEFINITION));



        root.appendChild(modelElement);

		/* description */
		if (!description.equals("")) {
			Element descriptionElement = root.createElement("description");
			descriptionElement.appendChild(root.createCDATASection(description));
			modelElement.appendChild(descriptionElement);
		}

		/* parameters */
		Element parametersElement = root.createElement("parameters");
		modelElement.appendChild(parametersElement);

		/* classes */
		Element classes_element = root.createElement("classes");
		parametersElement.appendChild(classes_element);
		classes_element.setAttribute("number", Integer.toString(classes));
		for (int i = 0; i < classes; i++){

             classes_element.appendChild(makeClassElement(root, i));

		}

		/* stations */
		Element stationsElement = root.createElement("stations");
		parametersElement.appendChild(stationsElement);
		stationsElement.setAttribute("number", Integer.toString(stations));
		for(int i = 0; i < stations; i++){
			//TODO: bisogna modificare per tenere conto di LD e DELAY
            //OLD
            //stationsElement.appendChild(makeStationElement(root, i));
            stationsElement.appendChild(makeStationElement(root, i));
		}



		modelDOM = root;


	}

	private Element makeClassElement(Document root, int classNum) {
		Element classElement = null;
		if (classTypes[classNum] == CLASS_CLOSED) {
			classElement = root.createElement("closedclass");
			classElement.setAttribute("population", Integer.toString((int) classData[classNum]));
			classElement.setAttribute("name", classNames[classNum]);

		} else {
			classElement = root.createElement("openclass");
			classElement.setAttribute("rate", Double.toString(classData[classNum]));
			classElement.setAttribute("name", classNames[classNum]);

		}
		return classElement;
	}



    //TODO: nuova per gestire anche LD e DELAY
    private Element makeStationElement(Document root, int stationNum) {

        Element station_element = null;
        Node servicetimes_element;
        Node visits_element;

        switch (this.stationTypes[stationNum]) {

            case STATION_LI:

                station_element = root.createElement("listation");
		        station_element.setAttribute("name", this.stationNames[stationNum]);

                /* create the section for service times */
                servicetimes_element = station_element.appendChild(root.createElement("servicetimes"));
                station_element.appendChild(servicetimes_element);

                /* create the section for visits */
                visits_element = station_element.appendChild(root.createElement("visits"));
                station_element.appendChild(visits_element);

                /* for each customer class */
                for (int j = 0; j < classes; j++) {
                    String class_name = this.classNames[j];
                    /* set service time */
                    Element st_element = root.createElement("servicetime");
                    st_element.setAttribute("customerclass", class_name);;
                    st_element.appendChild(root.createTextNode(Double.toString(this.serviceTimes[stationNum][j][0])));
                    servicetimes_element.appendChild(st_element);
                    /* set visit */
                    Element visit_element = root.createElement("visit");
                    visit_element.setAttribute("customerclass", class_name);
                    visit_element.appendChild(root.createTextNode(Double.toString(this.visits[stationNum][j])));
                    visits_element.appendChild(visit_element);
                }

                break;

            case STATION_DELAY:    //TODO: 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -