Snippets
Created by
Riccardo Sibani
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | class car {
name;
slow_turn: integer;
fast_turn: integer;
acceleration: integer;
max_speed: integer;
tyres_consumption: integer;
push: integer;
mutable total_time: integer = 0;
}
class tyre_wear {
key car;
mutable tyres;
mutable current_wear: integer;
}
class circuit {
name;
}
class sector {
circuit;
number: integer;
slow_turns_rate: integer;
fast_turns_rate: integer;
acceleration_rate: integer;
max_speed_rate: integer;
tyre_consumption: integer;
fuel_consumption: integer;
}
class tyres {
key name;
length: integer;
degradation: integer;
multiplier: integer;
}
function generate_cars(): list<car> {
val ferrari = create car(
.name = "Ferrari",
.slow_turn = 70,
.fast_turn = 80,
.acceleration = 70,
.max_speed = 80,
.tyres_consumption = 100,
.push = 100
);
val ferrari2 = create car(
.name = "Ferrari2",
.slow_turn = 70,
.fast_turn = 80,
.acceleration = 70,
.max_speed = 80,
.tyres_consumption = 100,
.push = 100
);
val mercedes = create car(
.name = "Mercedes",
.slow_turn = 80,
.fast_turn = 70,
.acceleration = 80,
.max_speed = 70,
.tyres_consumption = 100,
.push = 100
);
val mercedes2 = create car(
.name = "Mercedes2",
.slow_turn = 80,
.fast_turn = 70,
.acceleration = 80,
.max_speed = 70,
.tyres_consumption = 100,
.push = 100
);
return [ferrari, ferrari2, mercedes, mercedes2];
}
function generate_circuit() {
val monza = create circuit("Monza");
create sector(
monza,
number = 0,
slow_turns_rate = 20,
fast_turns_rate = 25,
acceleration_rate = 25,
max_speed_rate = 30,
tyre_consumption = 3,
fuel_consumption = 33
);
create sector(
monza,
number = 1,
slow_turns_rate = 25,
fast_turns_rate = 35,
acceleration_rate = 30,
max_speed_rate = 10,
tyre_consumption = 30,
fuel_consumption = 32
);
create sector(
monza,
number = 2,
slow_turns_rate = 15,
fast_turns_rate = 25,
acceleration_rate = 20,
max_speed_rate = 40,
tyre_consumption = 37,
fuel_consumption = 33
);
}
function generate_tyres() {
create tyres("soft", .length = 350, .degradation = 30, .multiplier = 80);
create tyres("hard", .length = 450, .degradation = 20, .multiplier = 110); // 8 laps total in this simulation
}
function change_tyre(car, tyres) {
create_or_update_tyre_wear(car, tyres);
car.total_time += 20000;
}
function create_or_update_tyre_wear(car, tyres) {
val car_tyre_wear = tyre_wear@?{car};
if(car_tyre_wear != null) {
car_tyre_wear.tyres = tyres;
car_tyre_wear.current_wear = tyres.length;
} else {
create tyre_wear(car, tyres, tyres.length);
}
}
function calculate(sector, car): integer {
val sector_standard_time = 22000; //in milliseconds
val tyre_wear = tyre_wear@{car};
val tyre_performance = tyre_wear.tyres.multiplier;
val tyre_finishing = if(tyre_wear.current_wear < sector.tyre_consumption) 120 else 100;
val tyre_finished = if(tyre_wear.current_wear < 0) 120 else 100;
val fuel_variation = car.push;
val sector_value = sector.slow_turns_rate + sector.fast_turns_rate + sector.acceleration_rate + sector.max_speed_rate;
val slow_turns = sector.slow_turns_rate * car.slow_turn;
val fast_turns = sector.fast_turns_rate * car.fast_turn;
val acceleration = sector.acceleration_rate * car.acceleration;
val max_speed = sector.max_speed_rate * car.max_speed;
val sector_performance_multiplier = (10*10*10*10) - (slow_turns + fast_turns + acceleration + max_speed);
val sector_car_time = sector_standard_time * (10*10*10*10 +sector_performance_multiplier) / (10*10*10*10);
val sector_car_tyre_time = sector_car_time * tyre_finishing * tyre_finished * tyre_wear.tyres.multiplier / 100 / 100 / 100;
tyre_wear.current_wear -= sector.tyre_consumption;
car.total_time += sector_car_tyre_time;
return sector_car_tyre_time;
}
operation main(nonce:integer) {
val cars = generate_cars();
generate_circuit();
generate_tyres();
create_or_update_tyre_wear(cars[0], tyres@{"soft"});
create_or_update_tyre_wear(cars[1], tyres@{"soft"});
create_or_update_tyre_wear(cars[2], tyres@{"hard"});
create_or_update_tyre_wear(cars[3], tyres@{"hard"});
for(lap in range(8)) {
// for(iSector in range(3)){
print("LAP: " + (lap+1) + "\t\t|\t SECTOR: 1 \t|\t SECTOR: 2 \t|\t SECTOR: 3" );
print("-----------------------------------------------------------------------------------------------");
for(car in cars) {
val sector1 = calculate(sector@{.number == 0}, car);
val sector2 = calculate(sector@{.number == 1}, car);
val sector3 = calculate(sector@{.number == 2}, car);
if(car.name.size() <8) { // Just for right formatting
print(car.name + "\t\t|\t "+ sector1 + "\t|\t " + sector2 + "\t|\t " + sector3 );
} else {
print(car.name + "\t|\t "+ sector1 + "\t|\t " + sector2 + "\t|\t " + sector3 );
}
}
// }
print("\n\n");
if(lap == 4) {
change_tyre(cars[0], tyres@{"hard"});
change_tyre(cars[1], tyres@{"soft"});
}
if(lap == 6) {
change_tyre(cars[1], tyres@{"soft"});
change_tyre(cars[2], tyres@{"soft"});
change_tyre(cars[3], tyres@{"hard"});
}
}
print("RESULT");
val arrival_result = car@*{}(sort .total_time, .name);
for(i in range(arrival_result.size())) {
print((i+1) + ") " + arrival_result[i].name + ": " + arrival_result[i].total_time);
}
print("EXIT(0)");
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.