====== Browsable API Example ====== This simplified example aims to demonstrate how you can easily implement and solve complex problems using SOLVER-AI. We will be looking at a simplified design problem for an electric vehicle (EV), considering various components such as the motor, battery, and body of the EV. Each component has different parameters and prices, and we aim to find a combination that minimizes cost while maximizing range. ---- ===== Problem Elements ===== Following is the data and equations we will be using for this example. __**Files**__ The csv and python for this example can be downloaded form [[https://github.com/solver-ai-ltd/client/tree/master/example_files/getting_started|here]]. __**Motors Table**__ Let's imagine we have different motors to chose from. ^ motor_model ^ motor_power ^ motor_efficiency ^ motor_price ^ voltage_compatibility ^ | Motor1 | 100 | 0.85 | 1000 | High Voltage | | Motor2 | 150 | 0.9 | 1500 | Low Voltage | | Motor3 | 200 | 0.95 | 2000 | High Voltage | | Motor4 | 250 | 0.8 | 2500 | Low Voltage | | Motor5 | 300 | 0.85 | 3000 | High Voltage | The Motors Table lists different models of motors, each with its power, efficiency, price, and voltage compatibility. __**Batteries Table**__ Now let's consider we have a number of batteries to chose from. ^ battery_model ^ battery_capacity ^ battery_efficiency ^ battery_price ^ voltage_compatibility ^ | Battery1 | 50 | 0.9 | 5000 | High Voltage | | Battery2 | 75 | 0.92 | 7500 | Low Voltage | | Battery3 | 100 | 0.95 | 10000 | High Voltage | | Battery4 | 125 | 0.88 | 12500 | Low Voltage | | Battery5 | 150 | 0.9 | 15000 | High Voltage | The Batteries Table lists different models of batteries, each with its capacity, efficiency, price, and voltage compatibility. __**Bodies Table**__ The bodies table contains the list of bodies available. ^ body_model ^ body_weight ^ body_price ^ | Body1 | 1500 | 10000 | | Body2 | 1400 | 15000 | | Body3 | 1300 | 20000 | The Bodies Table lists different models of car bodies, each with its aerodynamics, weight, and price. __**Machine Learning Table**__ Let's now immagine we have data relative to previous builds for which we know the range per unit of energy, a parameter that allows us to estimate the range of a vehicle. ^ motor_power ^ battery_capacity ^ body_weight ^ range_unit_energy ^ |100|50|1500|3.4| |150|75|1400|3.6| |200|100|1300|3.8| The Machine Learning Table is used to train a model that predicts the range per unit of energy (''range_unit_energy'') given the power of the motor, the capacity of the battery, and the weight of the body. __**Total Cost Equation**__ The ''total_cost'' is calculated by adding up the price of the motor ''motor_price'', the price of the battery ''battery_price'', and the price of the body ''body_price'': total_cost = motor_price + battery_price * efficiency_price_factor + body_price This equation is used to calculate the total cost of an EV given its design parameters. __**Range Estimate Equation**__ The range estimate ''range'' is calculated by multiplying the power of the motor ''motor_power'', the capacity of the battery ''battery_capacity'', and the factor ''range_unit_energy'': range = motor_power * battery_capacity * range_unit_energy This equation is used to estimate the range of an EV given its design parameters and the predicted range per unit of energy (''range_unit_energy'') from the machine learning model. __**Battery Efficiency Price Factor**__ Let's consider that the ''efficiency_price_factor'' has a value of 1.2 for a battery with efficiency above 0.9 and 1.0 otherwise. ---- ===== Setup ===== Download the csv files from github as specified in the introduction. __**Modules Setup**__ Once logged in let's create the //HardData// for the csv files: * Batteries.csv * Bodies.csv * Motors.csv On page [[https://www.solver-ai.com/api|API page]] in "2 - Module Management" click on the //HardDatas// button and for each file do the following: - Specify a relevant name for the //HardData// module (must be unique). - Click on //Choose File// and select the csv file. - Leave //VectorizationIndices// empty. - Click on //POST//. This is all that is required for setting up the //HardData//. Let's now set up the ML data for the range_unit_energy.csv. On page [[https://www.solver-ai.com/api|API page]] in "2 - Module Management" click on the //SoftDatas (ML)// button and for each file do the following: - Specify a relevant name for the //SoftData// module (must be unique). - Click on //Choose File// and select the csv file. - For //VariablesStringIn// specify: ''motor_power'', ''battery_capacity'', ''body_weight'' - For //VariablesStringOut// specify: ''range_unit_energy'' - Leave //VectorizationIndices// empty. - Click on //POST//. By doing so we have specified that ''range_unit_energy'' will be predicted by applying ML, given ''motor_power'', ''battery_capacity'' and ''body_weight''. We can now set up the two equations for ''total_cost'' and ''range''. On page [[https://www.solver-ai.com/api|API page]] in "2 - Module Management" click on the //Equations// button and for each equation do the following: - Specify a relevant name for the //Equation// module (must be unique). - Specify the //EquationString//: * for the total_cost: ''total_cost = motor_price + battery_price * efficiency_price_factor + body_price'' * for the range: ''range = motor_power * battery_capacity * range_unit_energy'' - For //VariablesString// specify: * for the total_cost: ''motor_price'', ''battery_price'', ''body_price'', ''efficiency_price_factor'' * for the range: ''motor_power'', ''battery_capacity'', ''range_unit_energy'' - Leave //VectorizationIndices// empty. - Click on //POST//. Finally, let's set up the computation of the ''efficiency_price_factor'' for file efficiency_price_factor.py. On page [[https://www.solver-ai.com/api|API page]] in "2 - Module Management" click on the //Codes// button and do the following: - Specify a relevant name for the //Code// module (must be unique). - Click on //Choose File// and select the Python v3.9 file. - For //VariablesStringIn// specify: ''battery_efficiency'' - For //VariablesStringOut// specify: ''efficiency_price_factor'' - Leave //VectorizationIndices// empty. - Click on //POST//. __**Problem Setup**__ On page [[https://www.solver-ai.com/api|API page]] in "3 - Problem Management" click on the //Problems// button and: - Specify a relevant name for the //Problem// (must be unique). - Specify the //Equations// by selecting, using Ctrl/Command or Shift, the two //Equation// modules you have just created for computing ''total_cost'' and ''range''. - Specify the //Codes// by selecting the //Code// module you have just created for computing the ''efficiency_price_factor''. - Specify the //Harddatas// by selecting, using Ctrl/Command or Shift, the three //HardData// modules you have just created for the Batteries, Bodies and Motors. - Specify the //Softdatas// by selecting the //SoftData// module you have just created for predicting the ''range_unit_energy''. __**Selecting the Problem**__ On page [[https://www.solver-ai.com/api|API page]] in "4 - Problem Setup" select the problem you have just created from the drop-down menu. By doing so three tables are displayed: Inputs, Constraints and Objectives. Note that: - The //Inputs// table has only headers but no rows, this is because no variables were found which require any input. For this particular case all of the inputs are the table raws and are therefore implicitly defined. - Constraints and objectives are the outputs of the //Equation//, //HardData// and //SoftData// modules which contain numerical values. - Parameter ''range_unit_energy_std'' in Objectives and Constraints refers to the standard deviation of the predicted value for the parameter ''range_unit_energy''. In the context of machine learning, a prediction is an estimate of a value based on the data uploaded via the csv file to the //SoftData// module. ---- ===== Solver Setup and Execution ===== Following are a number of different setups and the expected results. For each, on page [[https://www.solver-ai.com/api|API page]], in "4 - Problem Setup" setup the problem Objectives and Constraints. Then, click on the Submit button in section "5 - Problem Execution" and view the results displayed in section "6 - Results". __**Execution With No Setup**__ Executing the problem will display a table displaying a number of different solutions, each on a different row of the table, with Input and Output values as columns. __**Experiment With Objectives and Constraints**__ Try experimenting with changing the setup and executing the problem. For example, by setting in "4 - Problem Setup": * Constraint ''range'' as greater than 200000. * Objective ''range'' as //maximise//. * Objective ''total_cost'' as //minimize// you will obtain a range of optimal solutions, from one which has the lowest ''total_cost'' possible to one that has maximum ''range'', and all the best compromise solutions found in between. ---- ===== Modifying the Problem ===== The problem is automatically updated when any of the modules are updated. Let's consider that rather than one battery, we can have up to three. We can then consider the following equations. total_cost = motor_price + battery_num * battery_price * efficiency_price_factor + body_price range = motor_power * battery_num * battery_capacity * range_unit_energy __**Modifying the Equations**__ On page [[https://www.solver-ai.com/api|API page]] in "2 - Module Management": - Click on the //Equations// button. - Click on the URL of the equation. Update the two equations by: - Specifying the //EquationString//: * for the ''total_cost'': ''total_cost = motor_price + battery_num * battery_price * efficiency_price_factor + body_price'' * for the ''range'': ''range = motor_power * battery_num * battery_capacity * range_unit_energy'' - For both the ''total_cost'' and ''range'' equations add to //VariablesString//: ''battery_num'' - Leave //VectorizationIndices// empty. - Click on //PUT//. __**Solver Setup and Execution**__ After selecting the problem on page [[https://www.solver-ai.com/api|API page]], in "4 - Problem Setup" you will find the ''battery_num'' is now available in the inputs. Specify the //Min// and //Max// values as 1 and 3, respectively and check the //Integer// checkboxClick on the Submit button in section "5 - Problem Execution" and view the results displayed in section "6 - Results". Note that by having modified the //Equation// modules, which are already part of the //Problem//, the Problem is automatically updated. This is true for any of the modules, allowing for example, to easily update ML data (in //SoftData// modules) on the fly, without requiring the //Problem// to have to be redefined. This can be particularly useful when SOLVER-AI is being used by a microcontroller for which new data is collected from sensors and fed to //SoftData// modules.