//  -*-  C++  -*-
//
//======================================================================
//  File:         dx.h
//  Author:       Thomas G. Dietterich
//  Description:  Declares dx classes for decision-theoretic diagnosis
//
//  Copyright (c) 1995 by Thomas G. Dietterich.  All Rights Reserved.
//       may be reproduced by any non-commercial purpose.
//======================================================================
//

#ifndef DX_H
#define DX_H

#include <iostream.h>
#include "clstring.h"
#include "pair.h"
#include "bayes.h"

class dxInfo;
class dxState;
class dxProblem;

istream & operator >> (istream & str, dxInfo & info);

class dxInfo
{
public:
  dxInfo(string nameArg,
	 int isObservableArg,
	 int observationCostArg,
	 int isRepairableArg,
	 int repairCostArg):  
    name(nameArg),
    isObservable(isObservableArg),
    observationCost(observationCostArg),
    isRepairable(isRepairableArg),
    repairCost(repairCostArg) {};

  dxInfo(): name(string("none")), isObservable(0), observationCost(0),
     isRepairable(0), repairCost(0) {};

  string name;
  int isObservable;
  int observationCost;
  int isRepairable;
  int repairCost;

  friend istream & operator >> (istream & str, dxInfo & info);

};


class dxState
{
public:
  dxState(int n = 0): nvars(n), state(n,-1), probability(n,0), bestAction(-1) {};
  dxState(const dxState & other);

  int nvars;
  vector<int> state;  // operating status of each device. 0 = ok, 1 =
		      // broken, -1 = unknown
  vector<float> probability;  // probability of each unknown
                              // variable being faulted (i.e. = 1)
  int bestAction;     // index of variable affected by best action

  // compute the value of the optimal repair-only policy from this state
  float value(dxProblem & problem, bayesNet & net);  
  // compute the probability that var is faulty given that all other
  // not-yet-observed repairable components are not faulty.
  float singleFaultProbability(dxProblem & problem, bayesNet & net,
			       int var);
  // compute the value of a policy consisting of at most one
  // observation followed by a repair-only policy from this state
  float lookaheadValue(dxProblem & problem, int var);  
};



istream & operator >> (istream & str, dxProblem & problem);

class dxProblem
{
public:
  // constructors
  dxProblem(int n = 0, bayesNet * network = 0, int pdf = -1): variables(n),
    net(network), problemDefiningVariable(pdf) {};
  dxProblem(char netFilename[], char problemFilename[]);

  // data fields
  int problemDefiningVariable;
  vector<dxInfo> variables;
  bayesNet * net;

  // member functions
  int solve();       // perform the diagnosis, returns cost

  // friends
  friend istream & operator >> (istream & str, dxProblem & problem);
};


// used for sorting things
class varAndRatio: public pair<int,float>
{
public:
  varAndRatio(int v, float r): pair<int,float>(v,r) {};
  varAndRatio(): pair<int,float>(-1,0.0) {};

  int operator < (varAndRatio & other);
};


#endif
