/// <summary> /// an agent ask for help, but he can choose different mediums like email, phone, ... /// </summary> /// <returns></returns> public static CommunicationMediums NextMedium(CommunicationMediums mediums) { var count = Count(mediums); if (count == 0) { return(CommunicationMediums.System); } var index = DiscreteUniform.SampleToByte(count - 1); var channels = ToArray(mediums); return((CommunicationMediums)channels[index]); }
/// <summary> /// Get a random knowledge Bit (index)index /// </summary> /// <returns></returns> public byte GetRandomBitIndex() { return(DiscreteUniform.SampleToByte(0, (byte)(Length - 1))); }
/// <summary> /// The agent have received a message that ask for belief in return /// stochastic Filtering agentKnowledge based on MinimumBitsOfKnowledgeToSend/MaximumBitsOfKnowledgeToSend /// Work with non binary KnowledgeBits /// </summary> /// <returns>null if he don't have the belief or the right</returns> /// <returns>a beliefBits if he has the belief or the right</returns> /// <returns>With binary KnowledgeBits it will return a float of 0</returns> /// <example>KnowledgeBits[0,1,0.6] and MinimumKnowledgeToSend = 0.8 => KnowledgeBits[0,1,0]</example> public Bits FilterBeliefToSend(IAgentId beliefId, byte beliefBit, CommunicationTemplate medium) { if (medium is null) { throw new ArgumentNullException(nameof(medium)); } // If don't have belief, can't send belief or no belief asked if (!_messageContent.CanSendBeliefs || !On) { return(null); } // intentionally after Cognitive.MessageContent.CanSendBeliefs test if (Beliefs == null) { throw new NullReferenceException(nameof(Beliefs)); } // If Agent don't have the belief, he can't reply if (!BelievesEnough(beliefId, beliefBit, _messageContent.MinimumBeliefToSendPerBit)) { return(null); } var agentBelief = _actorBeliefNetwork.Edge <ActorBelief>(_agentId, beliefId); if (agentBelief is null) { throw new ArgumentNullException(nameof(agentBelief)); } // Filter the belief to send, via the good communication channel// Filtering via the good channel var minBits = Math.Max(_messageContent.MinimumNumberOfBitsOfBeliefToSend, medium.MinimumNumberOfBitsOfBeliefToSend); var maxBits = Math.Min(_messageContent.MaximumNumberOfBitsOfBeliefToSend, medium.MaximumNumberOfBitsOfBeliefToSend); var minKnowledge = Math.Max(_messageContent.MinimumBeliefToSendPerBit, medium.MinimumBeliefToSendPerBit); // Random knowledgeBits to send if (minBits > maxBits) { minBits = maxBits; } var lengthToSend = DiscreteUniform.SampleToByte(minBits, maxBits); if (lengthToSend == 0) { return(null); } var beliefIndexToSend = DiscreteUniform.SamplesToByte(lengthToSend, agentBelief.Length - 1); // Force the first index of the knowledgeIndex To send to be the knowledgeBit asked by an agent beliefIndexToSend[0] = beliefBit; var beliefBitsToSend = agentBelief.CloneWrittenBeliefBits(_messageContent.MinimumBeliefToSendPerBit); // knowledgeBitsToSend full of 0 except for the random indexes knowledgeIndexToSend for (byte i = 0; i < beliefBitsToSend.Length; i++) { if (!beliefIndexToSend.Contains(i) || Math.Abs(beliefBitsToSend.GetBit(i)) < minKnowledge) { beliefBitsToSend.SetBit(i, 0); } } // Check Length of message // We don't find always what we were looking for return(Math.Abs(beliefBitsToSend.GetSum()) < Tolerance ? null : beliefBitsToSend); }
/// <summary> /// The agent have received a message that ask for knowledge in return /// stochastic Filtering agentKnowledge based on MinimumBitsOfKnowledgeToSend/MaximumBitsOfKnowledgeToSend /// Work with non binary KnowledgeBits /// </summary> /// <returns>null if he don't have the knowledge or the right</returns> /// <returns>a knowledgeBits if he has the knowledge or the right</returns> /// <returns>With binary KnowledgeBits it will return a float of 0</returns> /// <example>KnowledgeBits[0,1,0.6] and MinimumKnowledgeToSend = 0.8 => KnowledgeBits[0,1,0]</example> public Bits FilterKnowledgeToSend(IAgentId knowledgeId, byte knowledgeBit, CommunicationTemplate medium, ushort step, out byte[] knowledgeIndexToSend) { knowledgeIndexToSend = null; if (medium is null) { throw new ArgumentNullException(nameof(medium)); } // If can't send knowledge or no knowledge asked if (!On || !_messageContent.CanSendKnowledge) //|| Expertise == null) { return(null); } if (!KnowsEnough(knowledgeId, knowledgeBit, _messageContent.MinimumKnowledgeToSendPerBit, step)) { return(null); } var agentKnowledge = GetActorKnowledge(knowledgeId); if (agentKnowledge is null) { throw new ArgumentNullException(nameof(agentKnowledge)); } // Filter the Knowledge to send, via the good communication medium knowledgeIndexToSend = null; // Filtering via the good channel var minBits = Math.Max(_messageContent.MinimumNumberOfBitsOfKnowledgeToSend, medium.MinimumNumberOfBitsOfKnowledgeToSend); var maxBits = Math.Min(_messageContent.MaximumNumberOfBitsOfKnowledgeToSend, medium.MaximumNumberOfBitsOfKnowledgeToSend); var minKnowledge = Math.Max(_messageContent.MinimumKnowledgeToSendPerBit, medium.MinimumKnowledgeToSendPerBit); // Random knowledgeBits to send var lengthToSend = DiscreteUniform.SampleToByte(Math.Min(minBits, maxBits), Math.Max(minBits, maxBits)); if (lengthToSend == 0) { return(null); } knowledgeIndexToSend = DiscreteUniform.SamplesToByte(lengthToSend, agentKnowledge.Length - 1); // Force the first index of the knowledgeIndex To send to be the knowledgeBit asked by an agent knowledgeIndexToSend[0] = knowledgeBit; var knowledgeBitsToSend = Bits.Initialize(agentKnowledge.Length, 0F); for (byte i = 0; i < knowledgeIndexToSend.Length; i++) { var index = knowledgeIndexToSend[i]; if (agentKnowledge.GetKnowledgeBit(index) >= minKnowledge) { knowledgeBitsToSend[index] = agentKnowledge.GetKnowledgeBit(index); } } var bitsToSend = new Bits(knowledgeBitsToSend, 0); // Check Length of message // We don't find always what we were looking for return(Math.Abs(bitsToSend.GetSum()) < Tolerance ? null : bitsToSend); }