Posts

Algorithms: Backpack problem, Greedy , Dynamic Programming techniques (C++)

This example shows an OO approach to the backpack (rumsack) problem, it implements different techniques (greedy, dynamic programming), it also generates random items and backpack's capacity. backpack.h #ifndef BACKPACK_H_INCLUDED #define BACKPACK_H_INCLUDED #include #include #include #include using namespace std; //A backpack Item class Item { public: Item(double w, double b):_weight(w),_benefit(b){ if(_weight==0) throw -100; } virtual ~Item() {} void setWeight(double w) { _weight = w; } void setBenefit(double b) { _benefit = b;} double getRatio(){ return _benefit/_weight; } double _weight; double _benefit; double _ratio; }; class Backpack { public: Backpack(double mw) { _maxWeight = mw; _items = new vector ; _totBenefit = 0; _currentWeight = 0; } virtual ~Backpack() {} virtual void computeAddItems(vector &items) { //In this case we use raw algorithm double delt...

Probabilities in R (Bayes Rule example)

This code shows an example that implements Bayes Rule. In this case it calculates a posteriori probabilities for a case of a components based system failure. (code is documented) #Probability #Bayes Rule #Problem2: # C1:"Components of type 1 in a system", # C2="Components of type 2 in a system", # C3="Components of type 3 in a system" # F ="A component of the system fails" # F given C1 = "System fails because a component of type 1" # F given C2 = "System fails because a component of type 2" # F given C3 = "System fails because a component of type 3" # We want to know (posteriori): If system fails what are: P(C1| F) , P(C2|F), P(C3|F) # Priori probs pC1 P(A given B) = P(A and B) / P(B) intersections

Design patterns (Observer, Singleton, Factory Method and Adapter) implemented in Dojo Toolkit JS

This is a list of design patterns, implemented in Dojo JS I'v found difficult to work with some restrictions as the use of static variables, in this kind of implementation Observer //Observer pattern dojo.declare("com.dev.dojo.Observer",[], { state: "observer", obs: null, constructor:function(obl){ this.obs = obl; }, update: function(sta){ this.state = sta; status = this.obs.updateObserver(this); console.log("udate observer result for "+ this + ": " + status); } }); dojo.declare("com.dev.dojo.Observable",[], { state: "observable", observers: [], Observervable:function(obss){ this.observers = obss; }, addObserver: function(o){ idx = dojo.indexOf(this.observers,o); if(idx!=null ...

Design Patterns code examples

Design Patterns code examples Index Gof Patterns code examples You can download code from GitHub Updated You can find implementation in Java of the following design patterns: Design Patterns Decorator Car decorator example Observer Subject, suscribers example Singleton Concurrent access example Abstract Factory Abstract factory that creates virtual robots example Command Invoker, command, receiver (simulates a cars race) Adapter International facilities adapter Facade Mobile Clinic facade example MVC Compound pattern: Model as Observer, Controller as Strategy, View as Composite (ref:HFDesign Patterns) GitHub: https://github.com/Leonarden/gofPatternsExamples

Proxy pattern example

Image
Proxy pattern Index Proxy pattern Includes code that you can download from GitHub Other proxy pattern examples Theory The proxy pattern Provide a surrogate or placeholder for another object to control access to it. One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it. In this example we have simulated a remote communication as you will see. The UML model is Figure-1 Code of the application Comm Interface package com.patterns.proxy; public interface Comm { public String receive(); public boolean reset(); } CommImpl Class package com.patterns.proxy; public class CommImpl implements Comm { private static int INFOLENGTH = 10; private boolean isAlive = false; private String information; public String receive() { // TODO Auto-generated ...

Java 9 Concurrency new CompletonStage and CompletableFuture

Java 9 CompletableFuture Example Index Java 9 concurrency CompletableFuture Example More Examples! Introduction Many Java applications require of concurrent operations. When using concurrency there are common risk that have to be avoided such as memory leaking, race condition, callback hell, disjointed error handling… In version 8, Java provides a new class CompletableFuture . This class has about 50 methods mainly for: compose, combine and execute asychronous steps and also to handle errors. Concurrency classes provided by Java as  Future  and  ExecutorService  interfaces since version 5 to handle the asynchronous operations. The Future interface is a placeholder for a result from an asynchronous operation which can be a Runnable or Callable instance. The  ExecutorService  interface submits an asynchronous operation and returns a...

Interface inheritance in Java 8 an example

Java 8 interface allows default implementations, static method declaration and static final attributes declarations: Here we show an example (just to show the concept) of interface declaration and inheritance. We can add that with the default code, inherited attributes are hidden and we will show the final solution. Example: In the code bellow we had to redeclare the inherited default method in each interface because the static attributes "from" and "to" were hidden by the super interface Locationable. You can try to remove the methods in subinterfaces to verify. public interface Locationable { float from = 0; float to = 0; public default int closeTo( float other) { System.out.println(from + " and " + to ); if( other>=from && other return 1; return -1; } } public interface Southern extends Locationable { float from = 100; float to = 1000; public default int closeTo( float other) { System.out.println(from + " and ...