Problog互斥性咨询:带特殊箱子的多存储空间分配建模问题
Got it, let's walk through how to model your storage and bin scenario step-by-step in ProbLog. First, let's recap the key rules we need to enforce:
- We have 4 distinct storage spaces (I'll name them
s1tos4for clarity) - Bins
b1-b4can only be assigned to at most one storage space (mutually exclusive), whileb5is special and can be used across multiple storages at once - Each bin has an assignment probability (note: you didn't finish
b4's probability, so I'll useP_b4as a placeholder—swap this with your actual value later; same forb5's probability, marked asP_b5) - Every bin has a weight, which we can use later for queries like expected total weight
1. Define Base Facts
First, let's lay down the foundational facts for our storages, bin weights, and assignment probabilities:
% List out all 4 storage spaces storage(s1). storage(s2). storage(s3). storage(s4). % Define the weight for each bin weight(b1, 4). weight(b2, 6). weight(b3, 5). weight(b4, 6). weight(b5, 5). % Assignment probability facts: bin -> storage % b1 has a 100% assignment chance to any storage 1.0::assign(b1, S) :- storage(S). % b2 has a 60% assignment chance 0.6::assign(b2, S) :- storage(S). % b3 has a 100% assignment chance 1.0::assign(b3, S) :- storage(S). % b4: replace P_b4 with your actual probability value (e.g., 0.8) P_b4::assign(b4, S) :- storage(S). % b5: replace P_b5 with your actual probability value P_b5::assign(b5, S) :- storage(S).
2. Add Mutex Constraints (For b1-b4)
The critical part is enforcing that b1-b4 can't be assigned to multiple storages at once. We do this with ProbLog's constraint rules:
% Prevent b1 from being assigned to two different storages :- assign(b1, S1), assign(b1, S2), S1 \= S2. % Prevent b2 from being assigned to two different storages :- assign(b2, S1), assign(b2, S2), S1 \= S2. % Prevent b3 from being assigned to two different storages :- assign(b3, S1), assign(b3, S2), S1 \= S2. % Prevent b4 from being assigned to two different storages :- assign(b4, S1), assign(b4, S2), S1 \= S2.
Important: We don't add this constraint for b5—that's how we let it be used across multiple storages freely.
3. Example Queries to Test the Model
Once the model is set up, you can run all sorts of useful queries. Here are a few common ones:
Query 1: Probability that storage s1 is filled by b2
query(assign(b2, s1)).
Query 2: Expected total weight of all assigned bins
First, define a rule to calculate total weight, then query its expected value:
total_weight(Total) :- findall(Weight, (assign(Bin, _), weight(Bin, Weight)), Weights), sum_list(Weights, Total). query(total_weight(Total)).
Query 3: Probability that b1 is assigned to any storage space
Since b1 has a 100% assignment chance, this should return 1.0, but it's a good sanity check:
query((assign(b1, s1); assign(b1, s2); assign(b1, s3); assign(b1, s4))).
Optional: Enforce "Every Storage Has At Least One Bin"
If you need to make sure no storage is left empty, add this constraint (it filters out any scenario where a storage has no bins assigned):
:- storage(S), \+ (assign(Bin, S), member(Bin, [b1,b2,b3,b4,b5])).
Just remember to swap out the placeholder probabilities (P_b4, P_b5) with your actual values before running the model!
内容的提问来源于stack exchange,提问作者B.Cetindag

