Snippets

chromawallet f1_manager

Created by Riccardo Sibani
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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.