【问题标题】:prolog logic gates aggregation recursion optimisationprolog逻辑门聚合递归优化
【发布时间】:2014-06-12 18:35:36
【问题描述】:

我正在尝试实现逻辑门类型的聚合操作。而且我无法编写在合理时间内执行计算的实现。我认为我所拥有的在逻辑上是有效的,但它非常缓慢,我认为没有必要这样做。我认为应该可以在不使用许多“findall's or cut”的情况下做到这一点。

我有一个大约 10,000 列和 70 行的表。行对应于样本,列对应于探针。表中的每个值要么为 1,要么为 0(样品中探针的状态)。

多个探针编码一个蛋白质。 (多对一关系)所以我想通过逻辑 OR 操作将探针列聚合到蛋白质列。

除此之外,一些蛋白质是蛋白质复合物或蛋白质组的一部分。除了含有蛋白质外,蛋白质复合物和蛋白质组都可以反过来含有蛋白质复合物或蛋白质组。所以它们可以是一种递归关系。我想将蛋白质集建模为 OR 门,将蛋白质复合物建模为 AND 门。我将蛋白质、蛋白质组和复合物统称为“实体”。

所以总的来说,我想要一个谓词,我可以在其中询问蛋白质或实体在快速起作用的样本中是打开还是关闭。

如果其他一些谓词不清楚,那么我可以告诉你它们的作用。

protein(Sample, Reactome_Id, State):-
    setof(Sample, Probe^samples(Sample, Probe, ProbeValue), Samples), 
    %sample/3 is a set of facts that correspond to the described table
    member(X, Samples), %used to generate Sample Id's %this seems wasteful 
    protein_reactome_Id_to_Uniprot_Id(Reactome_Id, UniprotId), % a set of facts matching two types of id
    %used to generate uniprot ids
    findall(Value, uniProt_Sample_Probes(UniprotId,X,_,Value),Vs),
    Vs = [_|_],     %Check list is not empty already
    delete(Vs,0,ListOfOnes),
    (ListOfOnes=[]-> (State is 0, write('OFF'));(State is 1,write('ON'))).
    %As this is an or I think I should just be able to find a single 1 and cut for the  on case and if this is not possible to say it is off.

%if a (simple) entity is a protein set and its state is on
%this is a base case where an entity does not have complexs or sets inside it
state_of_entity(Entity,State,Sample):-
    all_children_proteins(Entity), %checks that all children are of type protein
    type(Entity, protein_set),
    child_component(Entity,Child), %generates the children of an entity
    protein(Sample,Child,1),
    State is 1,!.

 %if a (simple) entity is a protein set and it's state if off
 %this is a base case where an entity does not have complexs or sets inside it
 %I find all proteins for a sample, this is a list of values, I delete all the
 %zeros and the remaining list will unify with the empty list.
 state_of_entity(Entity,State,Sample):-
     all_children_proteins(Entity),
     type(Entity, protein_set),
     child_component(Entity,Child),
 bagof(Value, Value^protein(Sample,Child,Value),Vs),
 delete(Vs,0,ListOfOnes),ListOfOnes=[],
 State is 0,!.

%if a (simple) entity is a complex and is off
%this is a base case where an entity does not have complexs or sets inside it
state_of_entity(Entity,State,Sample):-
    all_children_proteins(Entity),
    type(Entity, complex),
    child_component(Entity,Child),
    protein(Sample,Child,0),
    State is 0,!.

%if a (simple) entity is a complex and is on.
%this is a base case where an entity does not have complexs or sets inside it
%I find all protein in a sample, this is a list of values, I delete all the
%zeros and the remaining list will unify with the empty list.
state_of_entity(Entity,State,Sample):-
    all_children_proteins(Entity),
    type(Entity, complex),
    child_component(Entity,Child),
    bagof(Value, Value^protein(Sample,Child,Value),Vs),
    delete(Vs,1,ListOfZeros),ListOfZeros=[],
    State is 1,!.

%if a complex with components is off
%recursive case
state_of_entity(Entity,State,Sample):-
    type(Entity, complex),
    child_component(Entity,Child),
    (state_of_entity(Child,0,Sample);
    protein(Sample,Child,0)), %if it has any proteins as input as well as other      components
    State is 0,!.

%if a complex with components is on
%recursive case
state_of_entity(Entity,State,Sample):-
    type(Entity, complex),
    child_component(Entity,Child),
    bagof(Value, Value^state_of_entity(Child,Value,Sample),Vs),%if it has component inputs
    bagof(Value2, Value2^protein(Sample,Child,Value2),Vs2),%if it has protein inputs
    append(Vs, Vs2, Vs3),
    delete(Vs3,1,ListOfZeros),ListOfZeros=[],%delete all the ones, the list of zeros will be empty if all inputs are on
  State is 1,!.

%if a protein set with components is on
%recursive case
state_of_entity(Entity,State,Sample):-
    type(Entity, protein_set),
    child_component(Entity,Child),
    (state_of_entity(Child,1,Sample);
    protein(Sample,Child,1)), %if it has any proteins as input as well as other entities
    State is 1,!.

%if a protein set with components is off
%recursive case
state_of_entity(Entity,State,Sample):-
    type(Entity, protein_set),
    child_component(Entity,Child),
    bagof(Value, Value^state_of_entity(Child,Value,Sample),Vs), %if it has entity inputs
    bagof(Value2, Value2^protein(Sample,Child,Value2),Vs2), %if it has protein inputs
    append(Vs, Vs2, Vs3), %join the list of inputs together
    delete(Vs3,0,ListOfOnes),ListOfOnes=[], %delete all the zeros, the list of 1's will be empty if all inputs are off
    State is 0,!.

更新 我最终得到了这个,让蛋白质位按我的意愿工作。

samples(Samples):-
    setof(Sample_in, Probe^samples(Sample_in, Probe, ProbeValue), Samples).
sample(Sample):-
    once(samples(Samples)), %why do I need this?!
    member(Sample, Samples).

protein_stack(Sample, Reactome_Id, State):-
        (
            protein_reactome_Id_to_Uniprot_Id(Reactome_Id, UniprotId),
            uniProt_Sample_Probes(UniprotId, Sample, Probe, 1),
            !,
            State is 1
        ;
            State is 0
        ).

protein_good(Sample, Reactome_Id,State):-
    sample(Sample), 
    protein_reactome_Id_to_Uniprot_Id(Reactome_Id, _),
    protein_stack(Sample, Reactome_Id,State).

【问题讨论】:

    标签: prolog query-optimization aggregation


    【解决方案1】:

    让我们采用protein/3 的第一条规则。

    • Reactome_IdUniprotId 之间的关系是否唯一?如果是,请将其移至setof(Sample ...), member(X, Samples) 之前并在其后放置一个切口。否则,您将尝试满足 setof(...), member(X, Samples) 的每个结果。不仅如此,绿色削减还有助于提高性能。

    • 该规则有一个目的,即查看Vs 中是否至少有一个值为 1。您不应该生成Vs 的所有成员然后搜索值1,而是在满足uniProt_Sample_Probes(UniprotId, X, _, Value) 时找到第一个成员时停止。

      protein(Sample, Reactome_Id, State):-
              (
                  protein_reactome_Id_to_Uniprot_Id(Reactome_Id, UniprotId),
                  setof(Sample, Probe^samples(Sample, Probe, ProbeValue), Samples), 
                  member(X, Samples),
                  uniProt_Sample_Probes(UniprotId, X, _, 1),
                  !,
                  State is 1, write('ON))
              ;
                  State is 0, write('OFF')
              ).
      

    其他规则可以使用相同的模式进行优化:

    state_of_x(X, State) :- Goal, !, State = 1.
    state_of_x(X, State) :- State = 0.
    

    或者,更简洁,

    state_of_x(X, 1) :- Goal, !.
    state_of_x(X, 0).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 2012-12-12
      • 2012-08-02
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      相关资源
      最近更新 更多