-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain5.cpp
More file actions
188 lines (162 loc) · 5.54 KB
/
main5.cpp
File metadata and controls
188 lines (162 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <exception>
using namespace std;
#include "randNumbers.h"
#include "GeneticAlgorithmClass.h"
#include "ParameterClass.h"
int main(void)
{
//header to output to the screen
cout << "****************************************************" << endl
<< "G.A. Optimization of Hull Parameters for Car Ferry" << endl
<< "****************************************************" << endl
<< endl
<< "This program takes in a required number of vehicles," << endl
<< "and required speed. An optimization is performed to" << endl
<< "determine the best Length, Beam, Draft, and Block" << endl
<< "Coefficient that satisfy the constraints, while" << endl
<< "achieving minimum resistance." << endl;
int cars = 0; //number of cars set by user
double speed = 0; //speed set by user
double minSpeed = 0; //minimum and maximum speed determined by
// the number of cars selected
double maxSpeed = 0;
int hulls = 0; //number of hulls - one or two
cout.unsetf(ios::floatfield); // set precision for output of speeds
cout.setf(ios::showpoint);
cout.precision(3);
//user menu
cout << endl << "VESSEL CHARACTERISTICS" << endl
<< "Choose (1) Monohull, or (2) Catamaran: ";
cin >> hulls;
//checks user input
while(cin.fail() || hulls != 1 && hulls != 2)
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR! Invalid input." << endl;
cout << "Choose (1) Monohull, or (2) Catamaran: ";
cin >> hulls;
}
cout << "Enter the number of cars required: ";
cin >> cars;
//checks user input
while(cin.fail() || cars < MIN_CARS || cars > MAX_CARS)
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR! Invalid number of cars." << endl;
cout << "Enter the number of cars required: ";
cin >> cars;
}
// Regression to determine velocity range, coefficients used
minSpeed = pow(630.34*cars*AVG_CAR_WEIGHT*WEIGHT_CONSTANT,1.0/6.0)/0.5144;
maxSpeed = pow(4512593.03*cars*AVG_CAR_WEIGHT*WEIGHT_CONSTANT,1.0/6.0)/0.5144;
//if the maximum speed is greater than the maximum allowable speed, set the
// maximum speed to the maximum allowable speed
if(maxSpeed > MAX_SPEED)
{
maxSpeed = MAX_SPEED;
}
cout << "Enter the desired speed, in the range: " << minSpeed
<< " to " << maxSpeed << " (knots): ";
cin >> speed;
//checks user input
while(cin.fail() || speed < minSpeed || speed > maxSpeed)
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR: Invalid speed entered." << endl;
cout << "Enter the desired speed, in the range: " << minSpeed
<< " to " << maxSpeed << " (knots): ";
cin >> speed;
}
double mutationPct = 0;
double crossoverPct = 0;
int generations = 0;
int populationSize = 0;
int defaultChoice = 0;
cout << endl << "GENETIC ALGORITHM PARAMETERS" << endl
<< "Input population size: ";
cin >> populationSize;
//checks user input
while(cin.fail())
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR: Invalid type entered for population size." << endl;
cout << "Input population size: ";
cin >> populationSize;
}
cout << "Input number of generations: ";
cin >> generations;
//checks user input
while(cin.fail())
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR: Invalid type entered for generation size." << endl;
cout << "Input number of generations: ";
cin >> generations;
}
cout << "Crossover, Mutation Percentages: " << endl
<< "1. Use Defaults (C-80%, M-0.1%) " << endl
<< "2. Choose percentages" << endl
<< "Your Choice: ";
cin >> defaultChoice;
while(cin.fail() || defaultChoice < 1 || defaultChoice > 2)
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR: Invalid menu choice." << endl;
cout << "Your choice: ";
cin >> defaultChoice;
}
if(defaultChoice == 2)
{
cout << "Input Crossover Percent (0 to 1): ";
cin >> crossoverPct;
//checks user input
while(cin.fail())
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR: Invalid type entered for crossover percent." << endl;
cout << "Input Crossover Percent (0 to 1): ";
cin >> crossoverPct;
}
cout << "Input Mutation Percent (0 to 0.1): ";
cin >> mutationPct;
//checks user input
while(cin.fail())
{
cin.clear();
cin.ignore(200, '\n');
cout << "ERROR: Invalid type entered for Mutation Percent." << endl;
cout << "Input Mutation Percent (0 to 0.1): ";
cin >> mutationPct;
}
}
else
{
crossoverPct = 0.8; // Default values
mutationPct = 0.002;
}
GeneticAlgorithmClass GA;
GA.setMutation(mutationPct);
GA.setCrossover(crossoverPct);
GA.setGenerations(generations);
ParameterClass param(speed,cars,hulls);
GA.initializePopulation(populationSize,param);
setSeed(0);
int i;
GA.runGeneticAlgorithm(param);
cout << "Feasible Variable Ranges Tested:" << endl
<< "Length Range (m): " << param.getMinLength() << " "
<< param.getMaxLength() << endl
<< "Beam Range (m): " << param.getMinBeam() << " "
<< param.getMaxBeam() << endl
<< "Draft Range (m): " << param.getMinDraft() << " "
<< param.getMaxDraft() << endl;
return(0);
}