What type of event or tool should I use to model customer orders using pre-existing data? - anylogic

I am building a model where trucks leave main distributor to make deliveries to several customers along a route before returning back to the distributor when it is empty. I have been given pre-existing data in the form of a CSV of the exact location of all of the orders, the order weight, and how many orders there were at that location -- therefore, it's not a stochastic process that I'm modeling. My goal is to see how many deliveries the truck can make within 300 miles and a 20-hour round trip driving time, with the truck's total weight being decremented by the order weight at each stop. The initial truck capacity was 40,000 pounds.
Currently, I've created a standard "order" event, though I'm concerned my code is not correctly doing what I want it to. My order trigger type is "Timeout," mode is "cyclic," the first occurrence is 0.999 days, and the recurrence time is every 1 days. Below is my code on the "Action" module:
for( Truck truck: main.trucks){
double freeCapacity = truck.truckMaxCapacity - truck.truckCapacity;
if( distanceTo(truck, MILE) < 300 && totalWeight < freeCapacity && findFirst(main.trucks, tr->tr.customers.contains(this)) == null){
truck.customers.add(this);
truck.truckCapacity += totalWeight;
}
}
Since I'm not passing in the direct parameters of the orders in my code, I'm concerned the code I've written so far is generating orders rather than using what I already have. I have the order identification number, weight, and location all loaded in from a database within my "Customer" agent, which is also where the "order" event is located.
I'm new to StackOverflow, so thanks in advance for any help.

Related

Calling an element inside a collection(Demand) of collection class ArrayList and Element class shop

There are 4 main Agents: Distributors-Shops-Trucks-Orders
We have 4 distributors- 145 shops- N Trucks
I added an event to generate demand of constant amount(2 pallets).
The max capacity of the Truck is 20 ("center" parameter is of type Distributor \ see the image below)
I want to make multiple deliveries for several neighbor retailers using one Truck, so the truck will start from the distributor, the truck has to know which retailers to visit to know which pallets should be loaded.
So I need a way to find the group or sequence of retailers that the trucks has to deliver to based on DistanceByRoute() in order to decrease the number of Travelsenter image description here
I was trying to generate random orders inside the shop agent with amount 2
ProductOrder order= new ProductOrder(2,this); // "this" refers to the shop
I added this to a collection called Demand
Demand.add(this);
I wanted to make a loop, while Truck.load<20 .... I get from the collection(Demand) the Nearest shop by Route, like this I load and sequence the orders in an area, and I send the truck.
I cant find the Demand collection when trying to call it (Demand is a collection in shop agent).
Any ideas?
Also I appreciate notes about the algorithm/way of grouping the retailers orders in one Truck.

Access paramters of Anylogic agent population created from db

OK... let me retry the question.
I'll just walk through the steps I (wrongly) assumed would work.
Create a data table in excel with passenger info (id, flight_time, type, class, qty...), note that the table is sorted by id... not flight_time.
Import this database into anylogic.
Create a population of agents (Passenger/Passengers) from the data table (one/row).
Create a schedule that addresses the Start Column as flight_time and Value column as qty (for this job I'm only sending one passenger at a time therefore qty = 1 for each row).
Set the pedSource to arrive according to schedule and to use the Passenger Agent as the New pedestrian.
So here is where I'm losing it. When I run this model the new Passengers do not have any parameters associated from the data table. The pedestrian id is some weird number (say 3000 or so). I can click on the Passengers icon during run time and scroll through the created agents (all of the parameter data is there and correctly assigned), but I'm not sure how to associate the new Agents in the run-time model with the population of Passengers agents.
Am I missing a step here? I was thinking that if I import a population of agents from a data table and then have each show up at a particular time in the model that I could then do some calculations with regard to each such as ped.exitTime = time() - ped.flightTime - ped.bufferTime.
I'm just not understanding why the table data is not available for use during run-time through ped? Is there another mapping step that must be performed to push the data to the ped agnets?
I'm at an impasse at this point. I hope this question is described more clearly and your feedback is appreciated.
Several things are wrong here.
You do not create agents in a pop first and then try to reuse them in a PedSource. The latter creates agents itself.
You don't seem to create pedestrians but just agents
you are not mapping the data to parameters
Quick guide to help:
create custom agent type "MyPed" . Make sure its "use in flowchart" property is set to "Pedestrian"
add 1 param into "MyPed" for each dbase table column
set your PedSource to "calls of inject()" function for its arrivals
delete your schedule, you cannot get the data you need
USe a DynamicEvent. Include 1 parameter argument for each dbase column. On Startup of main, loop across all dbase entries and create a dynamic event with the current row-data.
in the action of the DynEvent, call myPedSource.inject(1) and then manually fill that ped with the data from the arguments
This is not straightforward, especially the DynEvent stuff. So do more research in the AL help to understand these and how they work, check example models...

Django Signals to update a different model

Say I have two models:
class Product(models.Model):
product = model.CharField()
quantity = model.IntegerField()
class sale(models.Model)
product = models.ManyToManyField(Product)
number_sold = model. IntegerField()
When a sale is made, I'd like the product quantity to be updated. Are signals the best way of going about this?
I was thinking about having sale admit a post_save signal when an object is created or updated. If updated, I would have to know the old and the new values to adjust the quantity properly. The product table would have to be listening for this signal.
I was also thinking about having the product table send a custom signal after it updates the quantity. If quantity < 0 item is on back order, else "success".
I am new to django and signals, but is this an appropriate use for signals or is their a better way to do this? I have read the docs, and watched a pycon video on signals, and read a few examples. I have yet to see signals used in this way, but I can't think of any other way to accomplish this task.
This is how I ended up accomplishing my goal, I am very interested to know if this is the proper way of doing this. I apologize for the models not matching the code below, I simplified my models to make it easier.
#receiver(pre_save, sender=Customer_Order_Products)
def update_quantity(sender, **kwargs):
obj = kwargs['instance']
updated_product = Inventory.objects.get(title = obj.product_id)
if not obj.id:
updated_product.quantity -= obj.quantity
else:
updated_product.quantity -= (obj.quantity - Customer_Order_Products.objects.get(id = obj.id).quantity)
Inventory.save(updated_product)

Best Way to Sequentially Parse Through a Table in T-SQL

I'm writing a stored procedure in SQL Server and hoping someone can suggest a more computationally efficient way to handle this problem:
I have a table of Customer Orders (i.e., "product demand") data that contains 3000 line items. Each record expresses the Order Qty for a specific product.
I also have another table of Production Orders (i.e., "product supply") data that contains about 200 line items. Each record expresses the Qty Available for each specific product.
The problem is that there is typically less supply than demand and, therefore, the Custom Order table contains an Allocation Priority value that shows each Customer Order's position in line to receive product.
What's the best way to allocate Qty Available in Production Orders to the Order Qty in Customer Orders? Note that you can't allocate more to each Customer Order than has been ordered.
I can do this by creating a WHILE loop and doing the allocation product-by-product, line-by-line but it is very slow.
Is there a faster set-based way to approach this problem?
I don't have data to test against. This would not try and fill partial qty.
select orders.custID, orders.priority, orders.prodID, orders.qty, SUM(cumu.qty) as 'cumu'
from orders
join orders as cumu
on cumu.prodID = orders.prodID
and cumu.priority <= orders.priority
join available
on availble.prodID = orders.prodID
group by orders.custID, orders.priority, orders.prodID, orders.qty
having SUM(cumu.qty) < availble.qty
order by orders.custID, orders.priority, orders.prodID

Calculate Coredata result approach

Lets say I'm trying to RECORD as well as calculate results based on values stored in core data.
Lets take Enery equation : E=mc(square)
I have three entities, Parent/Child/RecentRecords
A Category's entity
Category
-Name : Einstein's equation
-E = mc2
Child Entity:
- Name: Mass
another entry:
- Name:LightSpeed (for the sake of example, lets say the speed of light is not constant.
Recent Entity: The purpose of this entity is to store and track the Child Entities and so it has:
-NSDate TimeStamp
-NSNumber: Value
-Relationship <---->>ChildEntity
I'll explain from a User's point of view,
If I tap "Einstain's equation" (Category/Parent Entity), the detail view will bring out 2 textfields by iterating through the Child entity. When I record my entries of mass, light speed, it should be saved in the Recent entity. SO that overtime i can have a bunch of recordings.
Now image a similar setup for another formula, v=U+at. where Velocity would be parent. U, a, t would be in CHild entity. And in one recording a user would add three Recent Entity objects.
My question is I want to find out if theres an efficient way to calculate the results/entries at the Parent/Category entity level.
Because, EinstainsEquation stores the formula of its child entities, E=mc2 and V=u+at. The thing thats making this complicated is fetching the entries from the recent Entity based on timestamp (recording) and calculating it and showing the result based on the most recent entries.
Parent<-------->>Child<-------->>Recent
-Einstein <---->>Mass <----------->>Multiple Mass recordings based on time
-mc2 LightSpeed <------>>Multiple Lighspeed records based on time
-Velocity<------>>U <--------->>Multiple entries based on time of all 3.
-u+at Aceleration
TimeTaken
Based on the graph above, I need to get einstein result FROM the most recent entries of mass/lightspeed and calculate each of those entries with its formula mc2.
I thought of having a transient Property in Parent entity, how would i go about accomplishing this? How can i use the formula and calculate the child values of recent entity.
I've read multiple tutorials but most of them have to do with basic setup.. I hope ill learn more each day