Теория Принятия Решений Лабораторная 1
.pdf
Рисунок 3. Изменение уступки.
Кроме этого, если в программу ввести заведомо неразрешимую систему функций цели, то программа сообщит пользователю об ошибке (рисунок 4)
Рисунок 4. «Нет решения»
ВЫВОД
В результате решения поставленной задачи был реализован алгоритм метода последовательных уступок, а также графический пользовательский интерфейс к нему. Главным преимуществом программы является удобный интерфейс, скорость работы, а также возможность в любое время посмотреть расчеты и сравнить их благодаря файлу-журналу.
СПИСОК ИСПОЛЬЗОВАННЫХ ИСТОЧНИКОВ
1.ГОСТ Р 7.0.97-2016. Система стандартов по информации,
библиотечному и издательскому делу. Организационно-распорядительная
документация. Требования к оформлению документов от 01.07.2018
2.ГОСТ 7.32-2017. Система стандартов по информации,
библиотечному и издательскому делу. Отчет о научно-исследовательской
работе. Структура и правила оформления от 01.07.2018 |
|
|||
3. |
Информация |
с |
сайта. |
URL: |
https://math.semestr.ru/simplex/concessions.php |
|
|
||
4. |
Информация |
с |
сайта. |
URL: |
https://kpfu.ru/staff_files/F32587409/Reshenie_simpleks_metodom_zadachi_LP_p rimer_i_algoritm.pdf
5. Информация с сайта. URL:
https://github.com/Developonz/SimplexMethod
ПРИЛОЖЕНИЯ
Приложение А. исходный код программы
Файл Main.Java
public class Main{
public static void main(String[] args) { Interface.show();
}
}
Файл Interface.Java
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.scene.Group;
import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.stage.Stage;
public class Interface extends Application {
double [][] aimInput;
String[] maxMin = new String[5]; double [][] restrictInput;
//double[][] restrictRightInput = new double[5][1]; double compromiseInput;
@Override
public void start(Stage stage){
Group mainGroup = new Group();
Group inputGroup = new Group();
Label aimLabel = new Label("Введите коэффициенты функций цели:"); aimLabel.setLayoutX(10);
aimLabel.setLayoutY(10); inputGroup.getChildren().add(aimLabel);
ObservableList<String> striving = FXCollections.observableArrayList("max", "min");
Label[][] aimLabels = new Label[5][5];
TextField[][] aimTF = new TextField[5][5];
ComboBox<String>[][] aimCB = new ComboBox[5][1];
for (int i=0;i<aimLabels.length;i++){
for (int n=0;n<aimLabels[0].length;n++){ aimTF[i][n]= new TextField("0"); aimTF[i][n].setLayoutX(10+(50*n)); aimTF[i][n].setLayoutY(30+(30*i));
aimTF[i][n].setMaxHeight(20);
aimTF[i][n].setMaxWidth(30);
aimLabels[i][n] = new Label(); aimLabels[i][n].setLayoutX(10+30+(50*n)); aimLabels[i][n].setLayoutY(35+(30*i));
if (n!=4) {
aimLabels[i][n].setText("x" + (n + 1) + "+"); }else{
aimLabels[i][n].setText("x" + (n + 1) + " -> ");
}
inputGroup.getChildren().addAll(aimTF[i][n],aimLabels[i][n]);
}
aimCB[i][0] = new ComboBox<String>(striving); aimCB[i][0].setLayoutX(270); aimCB[i][0].setLayoutY(30+(30*i)); aimCB[i][0].setValue("max"); aimCB[i][0].setMaxWidth(75); aimCB[i][0].setMinWidth(75); inputGroup.getChildren().add(aimCB[i][0]);
}
Label restrictLabel = new Label("Введите коэффициенты ограничений:"); restrictLabel.setLayoutX(375);
restrictLabel.setLayoutY(10); inputGroup.getChildren().add(restrictLabel);
Label[][] restrictLabels = new Label[5][5];
TextField[][] restrictTF = new TextField[5][6];
for (int i=0;i<restrictLabels.length;i++){
for (int n=0;n<restrictLabels[0].length;n++) { restrictLabels[i][n] = new Label(); restrictLabels[i][n].setLayoutX(375+30+(50*n)); restrictLabels[i][n].setLayoutY(35+(30*i));
if (n!=4) {
restrictLabels[i][n].setText("x" + (n + 1) + "+"); }else{
restrictLabels[i][n].setText("x" + (n + 1) + " ≥");
}
inputGroup.getChildren().add(restrictLabels[i][n]);
}
}
for (int i=0;i<restrictTF.length;i++){
for (int n=0;n<restrictTF[0].length;n++) { restrictTF[i][n] = new TextField("0"); if (n!=5) {
restrictTF[i][n].setLayoutX(375 + (50 * n)); }else{
restrictTF[i][n].setLayoutX(375 + 10 + (50 * n));
}
restrictTF[i][n].setLayoutY(30+(30*i));
restrictTF[i][n].setMaxHeight(20);
restrictTF[i][n].setMaxWidth(30);
inputGroup.getChildren().add(restrictTF[i][n]);
}
}
Label compromiseLabel = new Label(); compromiseLabel.setLayoutX(10); compromiseLabel.setLayoutY(210); compromiseLabel.setText("Введите размер уступки:"); inputGroup.getChildren().add(compromiseLabel);
TextField compromiseTF = new TextField("0"); compromiseTF.setLayoutX(150); compromiseTF.setLayoutY(205); compromiseTF.setMaxWidth(75); inputGroup.getChildren().add(compromiseTF);
Label resultLabel = new Label(" ");// resultLabel.setLayoutX(375); resultLabel.setLayoutY(210); inputGroup.getChildren().add(resultLabel);
TextArea textArea = new TextArea(); textArea.setLayoutX(10); textArea.setLayoutY(250); textArea.setMinHeight(300); textArea.setMaxHeight(300); textArea.setMinWidth(765); textArea.setMaxWidth(765);
Button enterBtn = new Button("Рассчитать!"); enterBtn.setLayoutX(260); enterBtn.setLayoutY(205); enterBtn.setMinWidth(95); enterBtn.setMaxWidth(95);
enterBtn.setOnAction(new EventHandler<ActionEvent>() { @Override
public void handle(ActionEvent actionEvent) { Log.initialEntry();
resultLabel.setText(" ");
double[][] aimTemp = new double[5][5]; double[][] restrictTemp = new double[5][6];
//double[][] restrictRightTemp = new double[5][1]; for (int i=0; i<aimTF.length; i++){
for (int n=0;n<aimTF[0].length;n++){ aimTemp[i][n]=Double.parseDouble(aimTF[i][n].getText());
}
}
for (int i=0; i<restrictTF.length; i++) {
for (int n = 0; n < restrictTF[0].length; n++) {
//-1 because right side of equation in restrictRightInput restrictTemp[i][n]=Double.parseDouble(restrictTF[i][n].getText());
}
}
for(int i = 0;i<maxMin.length;i++){
maxMin[i]= String.valueOf(aimCB[i][0].getValue());
//System.out.println(maxMin[i]);
}
compromiseInput=Double.parseDouble(compromiseTF.getText()); aimInput=Backend.arrayOptimization(aimTemp,false);
if (aimInput==null){
resultLabel.setText("Ошибка! Нет функций цели."); return;
}
restrictInput=Backend.arrayOptimization(restrictTemp,true); if (restrictInput==null){
resultLabel.setText("Ошибка! Нет ограничений."); return;
}
//Matrix.printmat(aimInput);
//Matrix.printmat(restrictInput);
//Matrix.printmat(restrictRightInput);
Backend.solve(aimInput,restrictInput,maxMin,compromiseInput); String[] text=Log.getText();
int len=text.length; int startPoint=0;
if (len>1000) startPoint=len-1000; textArea.setText(" ");
if (text!=null){
for (int i=startPoint;i<text.length;i++) { textArea.appendText(String.valueOf(text[i]));
//System.out.println(text[i]);
}
textArea.setFont(Font.font("Consolas", 14));
}
textArea.requestFocus();
textArea.end();
}
}); inputGroup.getChildren().add(enterBtn);
mainGroup.getChildren().addAll(inputGroup,textArea);
Scene scene = new Scene(mainGroup, Color.SNOW); stage.setScene(scene);
stage.setTitle("TPR");
stage.setWidth(800);
stage.setHeight(600);
stage.setResizable(false);
stage.show();
}
public static void show(){ Application.launch();
}
}
Файл Backend.java
import javafx.scene.paint.Color; import javafx.scene.shape.Line;
import java.util.Arrays; import java.util.Random;
public class Backend extends Interface { public static int rng(int low, int high) {
Random r = new Random();
return r.nextInt(high - low) + low;
}
public static double[][] arrayOptimization(double[][] array, boolean fromRestrict){
//System.out.println("_______________"); double[][] result;
int minRowIndex=999; int minColumnIndex=999; int maxRowIndex=-1; int maxColumnIndex=-1;
for (int i=0;i<array.length;i++){
for (int n=0;n<array[0].length;n++){ if(array[i][n]!=0){
if (minRowIndex > i) minRowIndex = i;
if (minColumnIndex > n) minColumnIndex = n; if (maxRowIndex < i) maxRowIndex = i;
if (maxColumnIndex < n) maxColumnIndex = n;
}
//System.out.println(minRowIndex+" "+minColumnIndex+" "+maxRowIndex+" "+maxColumnIndex);
}
}
if (maxRowIndex==-1 || maxColumnIndex==-1) return null; if (minRowIndex==999 || minColumnIndex==999) return null;
if (fromRestrict && minColumnIndex<5){ maxColumnIndex=array[0].length-1;
} else if (fromRestrict) { return null;
}
if (fromRestrict) { minColumnIndex = 0;
maxColumnIndex = array[0].length-1;
}
result = new double[maxRowIndex-minRowIndex+1][maxColumnIndex-minColumnIndex+1]; for (int i=0;i<(maxRowIndex-minRowIndex+1);i++){
for(int n=0;n<(maxColumnIndex-minColumnIndex+1);n++){ result[i][n]=array[minRowIndex+i][minColumnIndex+n];
}
}
//Matrix.printmat(result); return result;
}
public static int solve(double[][] aimInput, double[][] restrictInput, String[] maxMin,double compromiseInput) { double ans;
String fullAns = new String();
int restrictInputRows = restrictInput.length;
int restrictInputColumns = restrictInput[0].length;
SimplexMethod.inpMatrixManual(aimInput[0], restrictInput, maxMin[0]); try {
ans=SimplexMethod.solveTask(); fullAns+="1) "+String.valueOf(ans)+" ";
} catch (Exception e) { System.out.println(e); return 1;
}
if (aimInput.length - 1 < 1) return 1;
for (int i = 1; i < aimInput.length; i++) {
double[][] newRestricted = new double[restrictInputRows + 1][restrictInputColumns]; for (int n = 0; n < restrictInput.length; n++) {
for (int j = 0; j < restrictInput[0].length; j++) { newRestricted[n][j]=restrictInput[n][j];
}
}
for(int n=0;n<aimInput[0].length;n++){ newRestricted[newRestricted.length-1][n]=aimInput[i-1][n];
}
Matrix.printmat(newRestricted);
double ans1; double ans2;
if (maxMin[i-1].equals("max")) {
newRestricted[newRestricted.length - 1][newRestricted[0].length - 1] = ans-compromiseInput; SimplexMethod.inpMatrixManual(aimInput[i], newRestricted, maxMin[i]);
try { ans1=SimplexMethod.solveTask();
} catch (Exception e) { System.out.println(e); return 1;
}
newRestricted[newRestricted.length - 1][newRestricted[0].length - 1] = ans; SimplexMethod.inpMatrixManual(aimInput[i], newRestricted, maxMin[i]);
try { ans2=SimplexMethod.solveTask();
} catch (Exception e) { System.out.println(e); return 1;
}
if (maxMin[i].equals("max")) { ans = Math.max(ans1, ans2);
}else{ ans=Math.min(ans1,ans2);
}
}else{
newRestricted[newRestricted.length - 1][newRestricted[0].length - 1] = ans+compromiseInput; SimplexMethod.inpMatrixManual(aimInput[i], newRestricted, maxMin[i]);
try { ans1=SimplexMethod.solveTask();
} catch (Exception e) { System.out.println(e); return 1;
}
newRestricted[newRestricted.length - 1][newRestricted[0].length - 1] = ans; SimplexMethod.inpMatrixManual(aimInput[i], newRestricted, maxMin[i]);
try { ans2=SimplexMethod.solveTask();
} catch (Exception e) { System.out.println(e); return 1;
}
if (maxMin[i].equals("max")) { ans = Math.max(ans1, ans2);
}else{
ans=Math.min(ans1,ans2);
}
}
fullAns+=(i+1)+") "+ans+" ";
}
Log.writeLog("------- ОТВЕТ: "+fullAns+" -------",true); return 0;
}
}
Файл Matrix.java
import java.util.Arrays;
public class Matrix {
public static double[][] multiply(double[][] m1, double[][] m2){ if (m1[0].length!= m2.length)
{
System.out.println("DIFFERENT SIZE"); return null;
}
double[][] result=new double[m1.length][m2[0].length]; for (int i=0;i<m1.length;i++){
for (int n=0;n<m2[0].length;n++){
for (int m=0; m<m1[0].length;m++){ result[i][n]+=m1[i][m]*m2[m][n];
}
}
}
return result;
}
private static double determinant(double[][] mat){ int n = mat.length;
if (n == 2)
return (mat[0][0] * mat[1][1]) - (mat[0][1] * mat[1][0]); int det = 0;
for (int i = 0; i < n; i++) {
int sign = ((i & 1) == 0) ? +1 : -1;
det += sign * mat[0][i] * determinant( createSubMatrix(mat ,0 , i) ); }// end of for
return det;
} // end of determinant
private static double[][] createSubMatrix(double[][] mat , int excludingRow , int excludingCol){ int n = mat.length;
double[][] newMatrix = new double[n - 1][n - 1]; int rPtr = -1;
for (int i = 0; i < n; i++) {
if (i == excludingRow)continue; ++rPtr;
int cPtr = -1;
for (int j = 0; j < n; j++) {
if (j == excludingCol)continue; newMatrix[rPtr][++cPtr] = mat[i][j];
}// end of inner loop
}// end of outer loop
return newMatrix;
