Nora Seinfield
Nora Seinfield

Reputation: 89

Call python script in Julia

I have a Gurobi model that I have implemented in Julia. This model is dependent on data input missed bags:

Function deterministic_model_test(Path_to_data)
    include(Path_to_data)
    model = Model(Gurobi.Optimizer);

# constraints...
    end

I want to run a stochastic model, using the deterministic model (the structure you can see in the code below).

My problem is that all of the data preprocessing and adds stochasticity to data that is necessary is made in python (as it is easier using numpy and vectorization), so is it possible to call a python scripts that does all the preprocessing save the output it in a csv file and load the new data into Julia?

best_missed_bags = missed_bags
for h=1:H # number of iterations
    x = deterministic_model_test(best_missed_bags)
    missed_bags_pr_flight[s,fa] = []
    missed_bags_day[s] = []
    for s=1:S
        # stochastic arrival times

        RUN PYTHON SCRIPT WITH DATA PREPROCESSING --> update CSV file
        Missed_bags = LOAD NEW CSV FILE INTO JULIA

        # Compute number of missed bags for simulation given the assignment
    
    end

    # Update z if fewer less bags with the given assignment (asve the assignment)
    if best_missed_bag>missed_bags_day[h]
        best_missed_bag = missed_bags_day[h]
        
        # update missed_bag matrix
        for fa in Fa 
            best_missed_bags = mean(old_miss_{f,i}, new_miss_{f,i})
        end
    end
end

I hope you can help :-)

Upvotes: 1

Views: 80

Answers (1)

Dan Getz
Dan Getz

Reputation: 18217

This documentation link should be of help: Running External Programs

For example, suppose test.csv has the following content:

Name,Hieght
Alice, 1.69
Bob, 1.77

Then you can use it within Julia with:

julia> using CSV

julia> using DataFrames

julia> df = CSV.read(`cat test.csv`, DataFrame)
2×2 DataFrame
 Row │ Name     Hieght  
     │ String7  Float64 
─────┼──────────────────
   1 │ Alice       1.69
   2 │ Bob         1.77

In your case, you would want instead of cat test.csv to run python some-script.py which outputs the CSV to standard output. If this is a problem, the process can be done in two stages, writing the CSV in python with run(...) command and reading it as above.

Hope this helps.

Upvotes: 1

Related Questions